多对多关系制定者

Chr*_*isS 3 mapping setter symfony doctrine-orm

我的实体之间有多对多的关系:PurchaseOrderSupplier.当我想Supplier在我的Symfony项目中添加订单时,我总是会收到以下错误消息:

属性"供应商"在"Acme\AppBundle\Entity\PurchaseOrder"类中不公开.也许你应该创建方法"setSuppliers()"?

当我setSuppliers()PurchaseOrder实体中自己创建一个函数时:

public function setSuppliers(\Acme\AppBundle\Entity\Supplier $suppliers )
{
    $this->suppliers = $suppliers;

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

可捕获致命错误:传递给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,
    ));
}
Run Code Online (Sandbox Code Playgroud)

和我的 AddSupplierType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('suppliers', 'entity', array(
         'class' => 'AcmeAppBundle:Supplier',
         'property' => 'name',
    ));
}
Run Code Online (Sandbox Code Playgroud)

在有些地方PurchaseOrderSupplier单位:

 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;
}
Run Code Online (Sandbox Code Playgroud)

新添加删除集方法:

 /**
     * 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) ;
    }
}
Run Code Online (Sandbox Code Playgroud)

Zel*_*jko 10

问题:

  1. 语法错误的方法名称及其参数:

    public function addSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
    
    Run Code Online (Sandbox Code Playgroud)

    方法说addSupplier(单数)但你接受供应商S(复数)

  2. 您需要对此方法进行重构:

    public function addSupplier(Supplier $supplier)
    {
        $this->suppliers->add($supplier) ;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    也:

    public function removeSupplier(Supplier $supplier)
    {
        $this->suppliers->removeElement($supplier) ;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    Getter和setter方法将起作用,如果你这样做我的回答:set multiple ='false'在一个表单中的多对多关系symfony2

Symfony会自行添加...()和删除...()方法.因此,如果关系是"供应商",它将找到addSupplier.或者如果关系是"类别",它将找到addCategory()和removeCategory().