symfony2 createAction 与 OneToMany
symfony2 createAction with OneToMany
我有 2 个实体。具有 2 列的实体类别:id, caName
。具有 4 列的实体服务:id, seName, sePrice, caId
。服务中的列 caId
包含类别的 id
。
我的目标是为服务创建 createAction、editAction 和 deleteAction。我对 editAction 和 deleteAction 没有问题。但是,我的 createAction returns 500 (Internal Server Error).
日志:
可捕获的致命错误:传递给 AppBundle\Entity\Service::setCaId() 的参数 1 必须是 AppBundle\Entity\Category 的实例,给定整数,在 /Applications/MAMP/htdocs/symfony_luxurysalon/src/AppBundle/Controller/ServiceController.php 中调用在第 67 行定义
类别实体:
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(
* targetEntity="Service",
* mappedBy="caId"
* )
*/
private $id;
...
public function __construct()
{
$this->id = new ArrayCollection();
}
/**
* Get id
*
* @return Service
*/
public function getId()
{
return $this->id;
}
...
}
服务实体:
class Service
{
...
/**
* @var int
*
* @ORM\Column(name="ca_id", type="integer", length=2)
* @ORM\ManyToOne(targetEntity="Category", inversedBy="id", cascade={"persist","remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $caId;
...
/**
* Set caId
*
* @param \AppBundle\Entity\Category $caId
*
* @return Service
*/
public function setCaId(\AppBundle\Entity\Category $caId = null)
{
$this->caId = $caId;
return $this;
}
/**
* Get caId
*
* @return \AppBundle\Entity\Category
*/
public function getCaId()
{
return $this->caId;
}
}
控制器:
$entity = new Service();
$entity
->setSeName("test")
->setSePrice("200");
->setCaId("2");
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
您必须将 Category
实例而不是 id 传递给 setCaId()
方法(顺便说一句。如果您不希望传递 id,则名称会产生误导,例如 setCategory()
会更合适)。因此,您必须首先从存储库中查找 id 为 2 的类别,然后将此类别传递给方法:
$doctrine = $this->getDoctrine();
$category = $doctrine->getRepository('AppBundle:Category')->find(2);
$service = new Service();
$service->setCaId($category);
$em = $doctrine->getManager();
$em->persist($service);
$em->flush();
您可能想要添加一些逻辑来处理 find()
没有 return 结果的情况(例如,当不存在 ID 为 2 的类别时)。
我有 2 个实体。具有 2 列的实体类别:id, caName
。具有 4 列的实体服务:id, seName, sePrice, caId
。服务中的列 caId
包含类别的 id
。
我的目标是为服务创建 createAction、editAction 和 deleteAction。我对 editAction 和 deleteAction 没有问题。但是,我的 createAction returns 500 (Internal Server Error).
日志:
可捕获的致命错误:传递给 AppBundle\Entity\Service::setCaId() 的参数 1 必须是 AppBundle\Entity\Category 的实例,给定整数,在 /Applications/MAMP/htdocs/symfony_luxurysalon/src/AppBundle/Controller/ServiceController.php 中调用在第 67 行定义
类别实体:
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(
* targetEntity="Service",
* mappedBy="caId"
* )
*/
private $id;
...
public function __construct()
{
$this->id = new ArrayCollection();
}
/**
* Get id
*
* @return Service
*/
public function getId()
{
return $this->id;
}
...
}
服务实体:
class Service
{
...
/**
* @var int
*
* @ORM\Column(name="ca_id", type="integer", length=2)
* @ORM\ManyToOne(targetEntity="Category", inversedBy="id", cascade={"persist","remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $caId;
...
/**
* Set caId
*
* @param \AppBundle\Entity\Category $caId
*
* @return Service
*/
public function setCaId(\AppBundle\Entity\Category $caId = null)
{
$this->caId = $caId;
return $this;
}
/**
* Get caId
*
* @return \AppBundle\Entity\Category
*/
public function getCaId()
{
return $this->caId;
}
}
控制器:
$entity = new Service();
$entity
->setSeName("test")
->setSePrice("200");
->setCaId("2");
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
您必须将 Category
实例而不是 id 传递给 setCaId()
方法(顺便说一句。如果您不希望传递 id,则名称会产生误导,例如 setCategory()
会更合适)。因此,您必须首先从存储库中查找 id 为 2 的类别,然后将此类别传递给方法:
$doctrine = $this->getDoctrine();
$category = $doctrine->getRepository('AppBundle:Category')->find(2);
$service = new Service();
$service->setCaId($category);
$em = $doctrine->getManager();
$em->persist($service);
$em->flush();
您可能想要添加一些逻辑来处理 find()
没有 return 结果的情况(例如,当不存在 ID 为 2 的类别时)。