2016-10-03 22 views
0

我有一個問題搞清楚以下幾點: 如何讓Symfony插入一個新的菜單,當一個新的咖啡店已與一個窗體? (菜單中的外鍵是shopid)Symfony One-To-One單向關係如果創建新的咖啡店,我該如何讓Doctrine創建一個新菜單?

在此先感謝,代碼如下。

菜單實體:

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

咖啡廳FormBuilder:

<?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')) 
     ; 
    } 
} 

回答

0

在許多方面:

  • 您可以將Coffeeshoptype定義爲服務,然後將ManagerRegistry@doctrine)注入__construct()(或只是EntityManager),爲事件FormEvents::POST_SUBMIT設置事件偵聽器。類似的東西:

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

  • 在控制器

    ,在那裏你堅持從Coffeeshoptype

  • 改變使用原則的事件監聽器(或創建一個實體監聽器,從學說功能)。有了學說事件,你可以發現實體(Coffeeshop)是否持續或更新並取決於情況,創建新的菜單。

以上所有方法都可以訪問Doctrine(歸功於依賴注入),但其中一些方法也是不好的方法。我建議將EventListener(或EventSubscriber)附加到Doctrine Events之一,然後堅持新的菜單。但是,如果僅當Coffeeshop按表單提交時才需要創建新菜單,請在表單類型中創建事件偵聽器。

相關問題