0

我在弄清楚以下问题时遇到了一些问题:当使用表单制作新咖啡店时,如何让 Symfony 插入新菜单?(菜单中的外键是shopid)

在此先感谢,代码如下。

菜单实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;


/**
 * Menu
 *
 * @ORM\Table(name="menu")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\MenuRepository")
 *
 */
class Menu
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Coffeeshop")
     * @ORM\JoinColumn(name="coffeeshop_id", referencedColumnName="id")
     */

    private $shopId;

    /**
     * @var \DateTime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */

    private $updated;



    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set shopId
     *
     * @param integer $shopId
     *
     * @return Menu
     */
    public function setShopId($shopId)
    {
        $this->shopId = $shopId;

        return $this;
    }

    /**
     * Get shopId
     *
     * @return int
     */
    public function getShopId()
    {
        return $this->shopId;
    }

    /**
     * Set lastUpdated
     *
     * @param \DateTime $lastUpdated
     *
     * @return Menu
     */
    public function setLastUpdated($lastUpdated)
    {
        $this->lastUpdated = $lastUpdated;

        return $this;
    }

    /**
     * Get lastUpdated
     *
     * @return \DateTime
     */
    public function getLastUpdated()
    {
        return $this->lastUpdated;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     *
     * @return Menu
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }
}

咖啡店实体:

    <?php
/// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="coffeeshop")
 */
class Coffeeshop
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $streetName;

    /**
     * @ORM\Column(type="string", length=6)
     */
    private $houseNumber;

    /**
     * @ORM\Column(type="string", length=7)
     */
    private $zipcode;

    /**
     * @ORM\Column(type="text")
     */

    private $description;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Coffeeshop
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return Coffeeshop
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set streetName
     *
     * @param string $streetName
     *
     * @return Coffeeshop
     */
    public function setStreetName($streetName)
    {
        $this->streetName = $streetName;

        return $this;
    }

    /**
     * Get streetName
     *
     * @return string
     */
    public function getStreetName()
    {
        return $this->streetName;
    }

    /**
     * Set houseNumber
     *
     * @param string $houseNumber
     *
     * @return Coffeeshop
     */
    public function setHouseNumber($houseNumber)
    {
        $this->houseNumber = $houseNumber;

        return $this;
    }

    /**
     * Get houseNumber
     *
     * @return string
     */
    public function getHouseNumber()
    {
        return $this->houseNumber;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Coffeeshop
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set zipcode
     *
     * @param string $zipcode
     *
     * @return Coffeeshop
     */
    public function setZipcode($zipcode)
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * Get zipcode
     *
     * @return string
     */
    public function getZipcode()
    {
        return $this->zipcode;
    }

    /**
     * Set menu
     *
     * @param \AppBundle\Entity\Menu $menu
     *
     * @return Coffeeshop
     */
    public function setMenu(\AppBundle\Entity\Menu $menu = null)
    {
        $this->menu = $menu;

        return $this;
    }

    /**
     * Get menu
     *
     * @return \AppBundle\Entity\Menu
     */
    public function getMenu()
    {
        return $this->menu;
    }

    /**
     * Set coffeeshopmenu
     *
     * @param \AppBundle\Entity\Menu $coffeeshopmenu
     *
     * @return Coffeeshop
     */
    public function setCoffeeshopmenu(\AppBundle\Entity\Menu $coffeeshopmenu = null)
    {
        $this->coffeeshopmenu = $coffeeshopmenu;

        return $this;
    }

    /**
     * Get coffeeshopmenu
     *
     * @return \AppBundle\Entity\Menu
     */
    public function getCoffeeshopmenu()
    {
        return $this->coffeeshopmenu;
    }
}

咖啡店表单生成器:

<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 23-9-2016
 * Time: 14:20
 */

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;


class CoffeeshopType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('phone')
            ->add('streetName')
            ->add('houseNumber')
            ->add('zipcode')
            ->add('description')
            ->add('save', SubmitType::class, array('label' => 'Add shop'))
        ;
    }
}
4

1 回答 1

0

很多方面:

  • 您可以将其定义Coffeeshoptype为服务,然后将 () 注入ManagerRegistry@doctrine__construct()仅注入EntityManager),为 event 设置事件侦听器FormEvents::POST_SUBMIT。像这样的东西:

    $this->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {/*...*/});

  • 在控制器中,您可以在其中持久保存更改Coffeeshoptype

  • 使用 Doctrine 的事件侦听器(或创建实体侦听器,Doctrine 的功能)。通过教条事件,您可以找到实体 ( Coffeeshop) 是否在持续或更新,并根据情况创建新菜单。

以上所有方法都可以访问 Doctrine(感谢依赖注入),其中一些方法也是不好的方法。我建议将 EventListener(或 EventSubscriber)附加到 Doctrine Events 之一,然后坚持新菜单。但是如果你只需要在Coffeeshop通过表单提交时才需要创建新菜单,那么就创建表单类型的事件监听器。

于 2016-10-03T22:02:59.930 回答