Symfony2 - 如何在doctrine中使用一个表单将值插入两个不同的表中?

时间:2015-08-12 15:51:12

标签: mysql symfony doctrine-orm doctrine

我确信之前已经问过这个问题,但我发现很难找到与我的确切问题相关的解决方案,所以我希望有人可以帮助我。

基本上,我有两个表,一个叫做“pet”,另一个叫做“customer_pet”。这两个表是链接的,这样我就可以将宠物分配给特定的人(客户)。但是我发现,如果我将客户ID添加到表单中(这不是宠物表中的字段),它不会持久存在。我认为我在学说中对不同类型的表和列关联有困难。

在我的Pet实体中,我有以下内容:

/**
 * @var integer
 *
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Oc49Customer", inversedBy="pet", cascade={"persist"})
 * @ORM\JoinTable(name="customer_pet",
 *   joinColumns={
 *     @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="pet_id", referencedColumnName="id")
 *   }
 * )
 */
private $customer_id;

但我不确定如何将其映射回我的customer_pet实体。 customer_pet表只包含pet_id和customer_id。添加宠物并在隐藏字段中通过表单传递customer_id时生成pet_id。

我对教义中的表关联并不过分熟悉,所以对任何帮助表示赞赏。如果有人需要任何其他代码段,请询问。

这是我的addPet()方法:

/**
     * Add pet
     *
     * @param \AppBundle\Entity\Pet $pet
     * @return Customer
     */
    public function addPet(\AppBundle\Entity\Pet $pet)
    {
        $this->pet[] = $pet;

        return $this;
    }

提前谢谢你 迈克尔

2 个答案:

答案 0 :(得分:1)

1)声明所有表格。 2)创建表单。 3)发送到多个表。 4)坚持数据。

use AppBundle\Entity\site;
use AppBundle\Entity\nba;

1)声明所有表格。

 $site = new site;
 $nba = new nba;

2)创建表单

$form = $this->createFormBuilder($site)




    ->add('site_id', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('category', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $output))
    ->add('team', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $nbat))

3)插入多个表格。

        $site_id = $form['site_id']->getData();
        $category = $form['category']->getData();
        $team = $form['team']->getData();




        $site->setSiteId($site_id);
        $site->setCategory($category);
        $nba->setWinner($team);

4)坚持数据

            $em = $this->getDoctrine()->getManager();
            $em->persist($site);
            $em->persist($nba);
            $em->flush();

答案 1 :(得分:0)

您只需要配置两个实体:Pet和Customer。另一个表格customer_pet - 是所谓的join-table。 Doctrine不需要此表的实体映射。您需要配置关联映射,以使Doctrine了解Customers和Pets之间的关系。在这种特定情况下,您可以使用Many-To-Many, Bidirectional association,这意味着一个顾客可以拥有许多宠物,一个宠物可以拥有许多顾客。
或多或少:

    客户实体
  • /**
     * @ORM\ManyToMany(targetEntity="Pet", inversedBy="customers")
     * @ORM\JoinTable(name="customer_pet")
     **/
    private $pets;
    
    public function __construct() {
        $this->pets = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
  • 宠物实体中的
  • /**
     * @ORM\ManyToMany(targetEntity="Customer", mappedBy="pets")
     **/
    private $customers;
    
    public function __construct() {
        $this->customers = new \Doctrine\Common\Collections\ArrayCollection();
    }
    

然后你的createPetAndSetCustomerAction看起来像这样:

public function createPetAndSetCustomerAction($idCustomer)
{
    $pet = new Pet();
    $pet ->setName('Fluffy');

    $customer = $this->getDoctrine()
        ->getRepository('MyBundle:Customer')
        ->find($idCustomer);

    $pet->getCustomers()->add($customer);

    $em = $this->getDoctrine()->getManager();

    $em->persist($pet);
    $em->flush();

    return new Response('Created pet ' . $pet->getName() . ' and set customer ' . $customer->getDescription());
}

我们在这里谈论一些基本概念,我建议您阅读Symfony Book的Databases and Doctrine章节两到三次,以充分理解这些概念。