许多关系设定者 [英] Many to Many Relationship Setter
问题描述
我的实体之间有多对多的关系: PurchaseOrder
和供应商
。当我想要在我的Symfony项目的订单中添加一个供应商
时,我总是收到以下错误消息:
I have a Many to Many relationship between my Entities: PurchaseOrder
and Supplier
. When I want do add a Supplier
to an order in my Symfony project, I always get this error message:
属性supplier在类
Acme\AppBundle\Entity\PurchaseOrder中不公开。也许你应该创建
方法setSuppliers()?
Property "suppliers" is not public in class "Acme\AppBundle\Entity\PurchaseOrder". Maybe you should create the method "setSuppliers()"?
当我创建一个 setSuppliers ()
功能自己在 PurchaseOrder
实体:
When I make a setSuppliers()
function by myself in the PurchaseOrder
Entity:
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行
Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /var/www/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 519 and defined in /var/www/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php line 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的某些部分
和供应商
实体:
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) ;
}
}
推荐答案
问题:
-
语法不正确的方法名称及其参数:
Grammatically incorrect method name and its argument:
public function addSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
方法表示addSupplier(单数),但您接受供应商(复数)
method says addSupplier (singular) but you are accepting supplierS (plural)
您需要重构此方法为:
public function addSupplier(Supplier $supplier)
{
$this->suppliers->add($supplier) ;
}
还:
public function removeSupplier(Supplier $supplier)
{
$this->suppliers->removeElement($supplier) ;
}
如果你这样做,Getter和setter方法就像我的回答一样: a href =https://stackoverflow.com/questions/16539244/set-multiple-false-in-a-form-in-a-many-to-many-relation-symfony2/16564513?noredirect=1#comment23800026_16564513 >以多对多关系symfony2 的形式设置multiple ='false'。
Getter and setter method will work if you do it like my answer on: set multiple='false' in a form in a many to many relation symfony2
Symfony将找到add ...()和remove ...()方法本身。所以如果关系是供应商,它会发现addSupplier。或者如果关系是类别,它将会找到addCategory()和removeCategory()。
Symfony will find add...() and remove...() methods by itself. So if relation is "suppliers", it will find addSupplier. Or if relation is "categories", it will find addCategory() and removeCategory().
这篇关于许多关系设定者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!