Doctrine多对多连接表没有填充

时间:2015-10-11 18:07:14

标签: mysql xml symfony doctrine-orm

我有以下学说.ORM.XML文件。我已经更新了模式,可以看到数据库中的所有表和连接表,我可以实例化 Product Order 的实体。

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>

但是,当我flush()数据库的实体时,它只是在 Product / Order 表中单独创建,并且没有相应的密钥记录如我所料,在 products_orders 连接表中生成。下面是我在控制器中对flush()到数据库的代码 - 我是否需要对此进行扩展以处理 products_orders 连接表?

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();

        $products = $em->getRepository('AppBundle:Product')
            ->findAllOrderedByName();

        $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(),
        ));

    }
}

修改

根据Cerad的建议,我已经在我添加addProduct()功能的地方添加了我的 Order 实体定义的代码。我也在我的 Product 实体代码中添加了相应的addOrder()方法:

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

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


    public function addProduct($product) {
        $product->addOrder($order);
    }
    #....
}

然而,当我尝试更新doctrine实体时,我收到一个错误:

  

编译错误:无法重新声明AppBundle \ Entity \ Order :: addProduct

1 个答案:

答案 0 :(得分:0)

我猜你的Order :: addProduct看起来像这样:

class Order {
  addProduct($product) {
    $this->products[] = $product;

如果是,那么您不会告诉产品新订单。你需要添加

    $product->addOrder($order);

这将完成您的双向引用,并应允许persist按预期工作。如果您已经拥有上述代码,那么其他问题就不对了。