我的实体之间存在多对多关系:PurchaseOrder
和Supplier
. 当我想Supplier
在我的 Symfony 项目中添加一个订单时,我总是收到以下错误消息:
属性“suppliers”在“Acme\AppBundle\Entity\PurchaseOrder”类中不公开。也许您应该创建方法“setSuppliers()”?
当我setSuppliers()
自己在PurchaseOrder
实体中创建一个函数时:
public function setSuppliers(\Acme\AppBundle\Entity\Supplier $suppliers )
{
$this->suppliers = $suppliers;
return $this;
}
我收到此错误消息:
可捕获的致命错误:传递给 Doctrine\Common\Collections\ArrayCollection::__construct() 的参数 1 必须是数组类型,给定对象,在 /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM 中调用/UnitOfWork.php 在第 519 行并在 /var/www/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php 第 47 行中定义
有任何想法吗?
/**
* @Route("order/{id}/supplieradd", name="order_supplieradd")
* @Secure(roles="ROLE_ADMIN")
*/
public function newSupplierAction(Request $request, $id)
{
$purchaseOrder = $this->getDoctrine()
->getRepository('AcmeAppBundle:PurchaseOrder')
->find($id);
if (!$purchaseOrder) {
throw $this->createNotFoundException(
'No order found for id '.$id
);
}
$form = $this->createForm(new AddSupplierType(), $purchaseOrder);
// process the form on POST
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($purchaseOrder);
$em->flush();
return new Response('Added Supplier to Order with ID '.$articleOrder->getId());
}
}
return $this->render('AcmeAppBundle:BasicData:newSupplier.html.twig', array(
'form' => $form->createView(),
'id' => $id,
));
}
和我的AddSupplierType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('suppliers', 'entity', array(
'class' => 'AcmeAppBundle:Supplier',
'property' => 'name',
));
}
PurchaseOrder
和Supplier
实体的某些部分:
class PurchaseOrder{
...
/**
* @ORM\ManyToMany(targetEntity="Supplier", mappedBy="purchaseOrders")
*/
private $suppliers;
public function __construct()
{
$this->suppliers = new ArrayCollection();
}
/**
* Add suppliers
*
* @param \Acme\AppBundle\Entity\Supplier $suppliers
* @return PurchaseOrder
*/
public function addSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
{
$this->suppliers[] = $suppliers;
return $this;
}
/**
* Remove suppliers
*
* @param \Acme\AppBundle\Entity\Supplier $suppliers
*/
public function removeSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
{
$this->suppliers->removeElement($suppliers);
}
/**
* Get suppliers
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSuppliers()
{
return $this->suppliers;
}
}
class Supplier{
...
/**
* @ORM\ManyToMany(targetEntity="PurchaseOrder", inversedBy="suppliers")
* @ORM\JoinTable(name="suppliers_purchaseOrders")
*/
private $purchaseOrders;
}
新的添加删除设置方法:
/**
* Add supplier
*
* @param \Acme\AppBundle\Entity\Supplier $supplier
* @return PurchaseOrder
*/
public function addSupplier(\Acme\AppBundle\Entity\Supplier $supplier)
{
$this->suppliers->add($supplier);
return $this;
}
/**
* Remove supplier
*
* @param \Acme\AppBundle\Entity\Supplier $supplier
*/
public function removeSupplier(\Acme\AppBundle\Entity\Supplier $supplier)
{
$this->suppliers->removeElement($supplier);
}
public function setSuppliers($supplier)
{
if ( is_array($supplier) ) {
$this->suppliers = $supplier ;
} else {
$this->suppliers->clear() ;
$this->suppliers->add($supplier) ;
}
}