我的实体之间存在多对多的关系:PurchaseOrder
和Supplier
。当我想在我的Symfony项目中为订单添加Supplier
时,我总是会收到以下错误消息:
物业“供应商”在课堂上不公开 “Acme公司\的appbundle \实体\ PurchaseOrder的”。也许你应该创建 方法“setSuppliers()”?
当我在setSuppliers()
实体中自行制作PurchaseOrder
函数时:
public function setSuppliers(\Acme\AppBundle\Entity\Supplier $suppliers )
{
$this->suppliers = $suppliers;
return $this;
}
我收到此错误消息:
捕获致命错误:参数1传递给 Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()必须是 类型数组,给定对象,调用 /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) ;
}
}
答案 0 :(得分:10)
问题:
语法错误的方法名称及其参数:
public function addSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
方法说addSupplier(单数)但你接受供应商S(复数)
您需要对此方法进行重构:
public function addSupplier(Supplier $supplier)
{
$this->suppliers->add($supplier) ;
}
也:
public function removeSupplier(Supplier $supplier)
{
$this->suppliers->removeElement($supplier) ;
}
如果您按照我的答案执行此操作,将使用Getter和setter方法:set multiple='false' in a form in a many to many relation symfony2
Symfony会自行添加...()和删除...()方法。因此,如果关系是“供应商”,它将找到addSupplier。或者如果关系是“类别”,它将找到addCategory()和removeCategory()。
答案 1 :(得分:-1)
您的setSupliers
方法不应该以{{1}}作为第一个参数吗?
ArrayCollection
当你在构造函数中将其作为public function setSuppliers(ArrayCollection $suppliers )
{
$this->suppliers = $suppliers;
return $this;
}
初始化时。使用ArrayCollection
方法
您将setSuppliers
转换为单个ArrayCollection
对象。
您的表单将返回一个选定的Supplier
对象。如果这真的是你想要的,你为什么不使用OnyToMany版权?
否则,您可以在表单字段中添加multiple选项。然后可以将多个Suplier对象作为数组返回。