2016-10-03 4 views
0

У меня есть проблема: выясните следующее: Как я могу сделать Symfony вставить новое меню, когда новый кофейник был сделан с формой? (внешний ключ в меню - shopid)Symfony One-To-One Однонаправленные отношения Как я могу заставить Doctrine создать новое меню, если будет создан новый Coffeeshop?

Заранее спасибо, код ниже.

Меню Entity:

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

Coffeeshop Entity:

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

Coffeeshop 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) сохраняется или обновляется и зависит от ситуации, создает новое меню.

Все вышеуказанные методы могут иметь доступ к Доктрине (благодаря инъекции зависимостей), также некоторые из этих методов являются плохими подходами. Я предлагаю присоединить EventListener (или EventSubscriber) к одному из событий Doctrine Events, а затем продолжать работать в новом меню. Но если вам нужно создать новое меню, только когда Coffeeshop отправляется по форме, создайте прослушиватель событий в виде формы.

 Смежные вопросы

  • Нет связанных вопросов^_^