Symfony 2实体存储库find()返回空数组



从现在开始感谢大家的回答。这就是问题所在。我有一个带有两个实体(任务和产品)的symfony 2应用程序。当我试图查找(findBy,findOneBy,findAll)产品时,它返回一个空数组。

任务实体

<?php
namespace pabloUserBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
/**
* Task
*
* @ORMTable(name="tasks")
* @ORMEntity(repositoryClass="pabloUserBundleRepositoryTaskRepository")
* @ORMHasLifecycleCallbacks()
*/
class Task
{
/**
* @ORMManyToOne(targetEntity="User", inversedBy="tasks")
* @ORMJoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
/**
* @ORMManyToOne(targetEntity="Product", inversedBy="task")
*  @ORMJoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $product;
public function __construct()
{
$this->tasks = new ArrayCollection();
}

/**
* Set product
*
* @param pabloUserBundleEntityProduct $product
*
* @return Task
*/
public function setProduct(pabloUserBundleEntityProduct $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return pabloUserBundleEntityProduct
*/
public function getProduct()
{
return $this->product;
}
/**
* @return ArrayCollection
*/
public function getTasks()
{
return $this->tasks;
}
/**
* @param ArrayCollection $tasks
*/
public function setTasks($tasks)
{
$this->tasks = $tasks;
}

和产品实体

<?php
namespace pabloUserBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* Products
*
* @ORMTable(name="products")
* @ORMEntity(repositoryClass="pabloUserBundleRepositoryProductsRepository")
* @UniqueEntity("productsName")
*/
class Product
{
/**
* @ORMOneToMany(targetEntity="Task", mappedBy="product")
*/
protected $task;
/**
* @ORMOneToMany(targetEntity="Publicaciones", mappedBy="product")
*/
protected $publicaciones;
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="products_name", type="string", length=255)
* @AssertNotBlank()
*/
private $productsName;
public function __construct()
{
$this->task = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set productsName
*
* @param string $productsName
*
* @return Product
*/
public function setProductsName($productsName)
{
$this->productsName = $productsName;
return $this;
}
/**
* Get productsName
*
* @return string
*/
public function getProductsName()
{
return $this->productsName;
}
public function __toString() {
return $this->productsName;
}
/**
* Get task
*
* @return DoctrineCommonCollectionsArrayCollection
*/
public function getTask()
{
return $this->task;
}
/**
* Set task
*
* @param DoctrineCommonCollectionsArrayCollection $typeSponsor
*
* @return Task
*/
public function setTask($task)
{
$this->task = $task;
}
/**
* @return mixed
*/
public function getPublicaciones()
{
return $this->publicaciones;
}
/**
* @param mixed $publicaciones
*/
public function setPublicaciones($publicaciones)
{
$this->publicaciones = $publicaciones;
}
}

现在,当我试图从控制器中找到一个乘积时,它返回一个空数组({})。我看不出这是怎么回事。

$productId = '18';
$product = $this->get('doctrine.orm.default_entity_manager')->getRepository('pabloUserBundle:Product')->find($productId);

实际上您有一个结果,它只是一个空对象,因为您还没有定义应该打印哪些属性。

最好的解决方案是让您的实体实现JsonSerializable。

class Product  implements JsonSerializable
{
...
public function jsonSerialize()
{
return [
"id"=> $this->getId(),
"name" => $this->getProductsName()
];
}

现在它知道在将类转换为json对象时应该打印什么了。

如果还需要任务集合,请为任务实体实现JsonSerializable,并添加产品实体:

public function jsonSerialize()
{
return [
"id"=> $this->getId(),
"name" => $this->getProductsName(),
"task" => $this->getTask()->toArray()
];
} 

JsonSerializable接口

相关内容

  • 没有找到相关文章

最新更新