3

我的实体之间存在多对多关系:PurchaseOrderSupplier. 当我想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',
    ));
}

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;
}

新的添加删除设置方法:

 /**
     * 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) ;
    }
}
4

2 回答 2

10

问题:

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

    public function addSupplier(\Acme\AppBundle\Entity\Supplier $suppliers)
    

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

  2. 您需要将此方法重构为:

    public function addSupplier(Supplier $supplier)
    {
        $this->suppliers->add($supplier) ;
    }
    

    还:

    public function removeSupplier(Supplier $supplier)
    {
        $this->suppliers->removeElement($supplier) ;
    }
    

    如果您像我的回答那样做,getter 和 setter 方法将起作用:在多对多关系 symfony2 的表单中设置 multiple='false'

Symfony 会自己找到 add...() 和 remove...() 方法。所以如果关系是“供应商”,它会找到addSupplier。或者如果关系是“类别”,它将找到 addCategory() 和 removeCategory()。

于 2013-05-19T11:18:26.530 回答
-1

您的setSupliers方法不应该将 anArrayCollection作为第一个参数吗?

public function setSuppliers(ArrayCollection $suppliers )
{
    $this->suppliers = $suppliers;

    return $this;
}

当您像ArrayCollection在构造函数中一样初始化它时。使用您的setSuppliers方法,您会将您的对象转换ArrayCollection为单个Supplier对象。

您的表单将返回一个选定的Suplier对象。如果这真的是你想要的,你为什么不使用 OnyToMany 关系呢?否则,您可以将多个选项添加到表单字段。然后可以将多个供应商对象作为数组返回。

于 2013-05-19T06:27:32.323 回答