感谢您们从现在开始提供的所有答案。问题是这样的:我有一个Symfony 2应用程序,其中包含两个实体(Tasks和Products)。当我尝试查找(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);