Symfony表单没有在Doctrine多对多关联

时间:2015-10-12 22:01:49

标签: xml symfony doctrine-orm many-to-many

我有两个学说实体 Product Order ,我在XML中设置了一个many-to-many关联。

我的/newProduct路由和表单有效,我可以flush()将实体添加到数据库中的表中 - 不过我认为这只是因为没有关联的订单以这种形式定义。

相反,我的/neworder路线(下方)生成表单确定但是当我提交时我得到:

  

捕获致命错误:传递给Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()的参数1必须是类型数组,给定对象,在/ vendor / doctrine / orm / lib / Doctrine / ORM / UnitOfWork中调用。 php在第605行并定义了

我尝试在订单和产品中评论__construct(),但我仍然收到相同的错误消息。我想知道它是否可以来自addProduct(\AppBundle\ProductOrder $product)方法(参见下面的order.php),但这是由Doctrine自动生成的。我已经尝试过重新生成实体并更新学说模式,希望有些东西可以解决它,但到目前为止我还没有运气。

我的控制器:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\FOSRestController;
use AppBundle\Entity\Product;
use AppBundle\Entity\Order;

class DefaultController extends FOSRestController
{
    /**
     * @Route("/neworder", name="new_order")
     */
    public function newOrderAction(Request $request)
    {
        $order = new Order();
        $em = $this->getDoctrine()->getManager();

        $form = $this->CreateFormBuilder($order)
            ->add('products', 'entity', array(
                'class' => 'AppBundle:Product',
                'choice_label' => 'Description',
            ))
            ->add('Quantity', 'integer')
            ->add('Units', 'text')
            ->add('Save', 'submit', array('label' => 'Create Order'))
            ->getForm();


        $form->handleRequest($request);
        if ($form->isValid()) {
            $em->persist($order);
            $em->flush();
            return $this->redirectToRoute('homepage');
        }

        return $this->render('AppBundle:Default:new.order.form.html.twig', array(
            'form' => $form->createView(),
        ));

    }
}

我的Order.php实体:

namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;

class Order
{
    public $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }


    /**
     * Add Product
     *
     * @param \AppBundle\ProductOrder $product
     *
     * @return Order
     */
    public function addProduct(\AppBundle\ProductOrder $product)
    {
        $product->addOrder($order);
       #$this->products[] = $product;

        return $this;
    }
}

Products.orm.xml:

<entity name="Product" table="product"
        repository-class="AppBundle\Entity\ProductRepository">

    <many-to-many target-entity="Order" inversed-by="Product" field="orders">
        <join-table name="products_orders">
            <join-columns>
                <join-column name="Product_id" referenced-column-name="id"/>
            </join-columns>
            <inverse-join-columns>
                <join-column name="Orders_id" referenced-column-name="id"/>
            </inverse-join-columns>
        </join-table>
    </many-to-many>
</entity>

Orders.orm.xml:

<entity name="Order" table="order"
        repository-class="AppBundle\Entity\OrderRepository">

   <many-to-many field="products" mapped-by="orders" target-entity="Product" />
</entity>

0 个答案:

没有答案