谢谢大家的回答。这就是问题所在。我有一个symfony 2应用程序,有两个实体(任务和产品)。当我试图找到(findBy,findOneBy,findAll)产品时,它返回一个空数组。
任务实体
<?php
namespace pablo\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Task
*
* @ORM\Table(name="tasks")
* @ORM\Entity(repositoryClass="pablo\UserBundle\Repository\TaskRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Task
{
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="tasks")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="task")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $product;
public function __construct()
{
$this->tasks = new ArrayCollection();
}
/**
* Set product
*
* @param \pablo\UserBundle\Entity\Product $product
*
* @return Task
*/
public function setProduct(\pablo\UserBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \pablo\UserBundle\Entity\Product
*/
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 pablo\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Products
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="pablo\UserBundle\Repository\ProductsRepository")
* @UniqueEntity("productsName")
*/
class Product
{
/**
* @ORM\OneToMany(targetEntity="Task", mappedBy="product")
*/
protected $task;
/**
* @ORM\OneToMany(targetEntity="Publicaciones", mappedBy="product")
*/
protected $publicaciones;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="products_name", type="string", length=255)
* @Assert\NotBlank()
*/
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 \Doctrine\Common\Collections\ArrayCollection
*/
public function getTask()
{
return $this->task;
}
/**
* Set task
*
* @param \Doctrine\Common\Collections\ArrayCollection $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);
答案 0 :(得分:4)
实际上你有一个结果,它只是一个空对象,因为你还没有定义应该打印哪些属性。
最佳解决方案是让您的实体实施 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()
];
}