2016-12-20 6 views
1

symfony2 응용 프로그램에 양식 모음을 포함하려고합니다. 나는 간단한 엔티티가 : ShopAddress 숍이 여러 주소가 있습니다. 나는 Symfony2 documentation 따라하지만 오류 얻을 : ") (주소"Symfony2에 양식 모음을 포함 할 때 오류가 발생했습니다.

재산 "주소"나 방법 "getAddress에()"하나 둘, 를, "isAddress()", "hasAddress() ","__get() "이 (가) 있고"AppBundle \ Entity \ Address "클래스에 공개 액세스 권한이 있습니다. 500 내부 서버 오류 - NoSuchPropertyException은

Address Entityaddress preoperty에 액세스하려고 나에게 보인다. 여기

은 여기 내 Shop Entity

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\Common\Collections\ArrayCollection; 
use AppBundle\Entity\Address; 
use UserBundle\Entity\Seller; 

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

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

    /** 
    * @var string 
    * @ORM\Column(name="description", type="text", nullable=true) 
    */ 
    private $description; 

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

    /** 
    * @ORM\ManyToOne(targetEntity="UserBundle\Entity\Seller", cascade={"refresh"}, fetch="EAGER") 
    * @ORM\JoinColumn(nullable=false, onDelete="NO ACTION") 
    * @Assert\Valid() 
    */ 
    private $owner; 

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Address", mappedBy="shop") 
    * @Assert\Valid() 
    */ 
    private $address; 

    /** 
    * @ORM\Column(type="string", nullable=true) 
    * @Assert\Length(
    *  min = 9, 
    *  max = 10, 
    *  minMessage = "Le numéro siret doit contenir 10 chiffres", 
    *  maxMessage = "Le numéro siret doit contenir 10 chiffres" 
    *) 
    */ 
    private $siret; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Image") 
    * @ORM\JoinColumn(nullable=true) 
    */ 
    private $image; 


    public function __construct() 
    { 
     $this->address = new ArrayCollection(); 
    } 

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

    /** 
    * Set shopName 
    * 
    * @param string $shopName 
    * @return Shop 
    */ 
    public function setShopName($shopName) 
    { 
     $this->shopName = $shopName; 

     return $this; 
    } 

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

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

     return $this; 
    } 

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

    /** 
    * Set ownerName 
    * 
    * @param string $ownerName 
    * @return Shop 
    */ 
    public function setOwnerName($ownerName) 
    { 
     $this->ownerName = $ownerName; 
     return $this; 
    } 

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

    /** 
    * Set siret 
    * 
    * @param string $siret 
    * @return Shop 
    */ 
    public function setSiret($siret) 
    { 
     $this->siret = $siret; 
     return $this; 
    } 

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


    /** 
    * Set address 
    * 
    * @param ArrayCollection $address 
    * 
    * @return Address 
    */ 
    public function setAddress(ArrayCollection $address) 
    { 
     $this->address = $address; 
     return $this; 
    } 

    /** 
    * Get address 
    * 
    * @return \AppBundle\Entity\Address 
    */ 
    public function getAddress() 
    { 
     return $this->address; 
    } 

    /** 
    * Set owner 
    * 
    * @param \UserBundle\Entity\Seller $owner 
    * 
    * @return Owner 
    */ 
    public function setOwner(Seller $owner = null) 
    { 
     $this->owner = $owner; 
     return $this; 
    } 

    /** 
    * Get owner 
    * 
    * @return \UserBundle\Entity\Sellers 
    */ 
    public function getOwner() 
    { 
     return $this->owner; 
    } 


    /** 
    * 
    * @param Image $image 
    * @return \AppBundle\Entity\Shop 
    */ 
    public function setImage(Image $image) 
    { 
     $this->image = $image; 
     return $this; 
    } 

    /** 
    * 
    */ 
    public function getImage() 
    { 
     return $this->image; 
    } 

} 

입니다 내 내 기관 관리하기 위해 두 formType을 만들어 Address Entity

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 


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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="street", type="string", length=255) 
    * @Assert\Length( min = 5 , 
    *     max = 200, 
    *     minMessage = "L'adresse doit faire au minimum {{ limit }} caractères.", 
    *     maxMessage = "L'adresse doit faire au maximum {{ limit }} caractères.") 
    * 
    */ 
    private $street; 

    /** 
    * @ORM\Column(type="string", length=5) 
    * @Assert\Regex(
    *  pattern="/^\d{4,5}$/", 
    *  match=true, 
    *  message="Le format n'est pas correct" 
    *) 
    */ 
    private $postalCode; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    */ 
    private $city; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="country", type="string", length=255 , nullable=true) 
    * @Assert\Length( min = 3 , 
    *     max = 50, 
    *     minMessage = "Le pays doit faire au minimum {{ limit }} caractères.", 
    *     maxMessage = "L'adresse doit faire au maximum {{ limit }} caractères.") 
    * 
    */ 
    private $country; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Shop", inversedBy="address") 
    * @ORM\JoinColumn(name="shop_id", referencedColumnName="id") 
    */ 
    private $shop; 

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

    /** 
    * Set street 
    * 
    * @param string $street 
    * 
    * @return Address 
    */ 
    public function setStreet($street) 
    { 
     $this->street = $street; 

     return $this; 
    } 

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

    /** 
    * Set postalCode 
    * @return Address 
    */ 
    public function setPostalCode($postalCode) 
    { 
     $this->postalCode = $postalCode; 
     return $this; 
    } 

    /** 
    * Get postalCode 
    */ 
    public function getPostalCode() 
    { 
     return $this->postalCode; 
    } 

    /** 
    * 
    * @param string 
    * @return \AppBundle\Entity\Address 
    */ 
    public function setCity($city = null) 
    { 
     $this->city = $city; 
     return $this; 
    } 

    /** 
    * 
    */ 
    public function getCity() 
    { 
     return $this->city; 
    } 

    /** 
    * 
    * @param string 
    * @return \AppBundle\Entity\Address 
    */ 
    public function setCountry($country = null) 
    { 
     $this->country = $country; 
     return $this; 
    } 

    /** 
    * 
    */ 
    public function getCountry() 
    { 
     return $this->country; 
    } 

    /** 
    * 
    * @param Shop 
    * @return \AppBundle\Entity\Address 
    */ 
    public function setShop($shop = null) 
    { 
     $this->shop = $shop; 
     return $this; 
    } 

    /** 
    * 
    */ 
    public function getShop() 
    { 
     return $this->shop; 
    } 

    public function __toString() { 
     return $this->street." ".$this->postalCode." ".$this->city; 
    } 
} 

:

<?php 

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\CollectionType; 

use AppBundle\Form\AddressType; 
use AppBundle\Entity\Shop; 
use AppBundle\Entity\Address; 

class ShopType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('shopName', TextType::class, array('label' => 'Nom du magasin *', 'required' => true, 'error_bubbling' => true)) 
      ->add('ownerName', TextType::class, array('label' => 'Nom du gérant *', 'required' => true, 'error_bubbling' => true)) 

      ->add('address', CollectionType::class, array( 'entry_type' => AddressType::class, 
                  'allow_add' => true, 
                  'label' => 'Adresse *', 
                  'required' => true 
      )); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
       'data_class' => Shop::class, 
     )); 
    } 
} 
ShopType.php

AddressType.php

내 컨트롤러에서
<?php 

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

use AppBundle\Entity\Address; 

class AddressType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('address', TextType::class, array('label' => 'Adresse*', 'required' => true)) 
      ->add('CodePostal', TextType::class, array('label' => 'Code postal*', 'required' => true, 'error_bubbling' => true)) 
      ->add('Ville', TextType::class, array('label' => 'Ville', 'required' => false, 'error_bubbling' => true)) 
      ->add('Pays', 'choice', array(
        'choices' => array(
          'FR' => 'France', 
          'SU' => 'Suisse', 
          'BE' => 'Belgique' 
        ) 
      )) 
     ; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
       'data_class' => Address::class, 
     )); 
    } 

    public function getName() 
    { 
     return 'addresse'; 
    } 
} 

, 나는 다음과 같은 내 양식을 instanciating 해요 :

이 오류는 AddressType 클래스에서이 라인으로 인해 발생되는
$shop = $shopRepo->findOneByOwner($user); 
if ($shop == null){ 
    $shop = new Shop(); 
} 
$form = $this->createForm(ShopType::class , $shop); 

답변

3

:

->add('address', TextType::class, array('label' => 'Adresse*', 'required' => true))

address 속성에 Address 엔터티에 액세스하려고 시도했지만, 하지만 그것을 포함하지 않습니다. 같은 다른 분야에 관련이있다 : CodePostal =>postalCode

당신의 Shop 기업에서 또한

당신이 address 필드를 확인하고, 동시에 당신이 setAddress(ArrayCollection $address) 방법을 가지고 있지만, 대신에 당신이해야 OneToMany 관계로서 주석이 있습니다 :

public function addAddress(Address $address) 
{ 
    $this->address->add($address); 

    return $this; 
} 

및 옵션

public function removeAddress(Address $address) 
{ 
    $this->address->remove($address); 

    return $this; 
} 

는 Btw은 내가로 재산 address의 이름을 변경하는 것이 좋습니다은 콜렉션을 포함해야하지만 단일 엔티티는 포함하지 않아야 함을 강조합니다.

+0

설명해 주셔서 감사합니다. 내 OneToMany 관계를 "유효하게"했기 때문에 기쁩니다. 확실하지 않았습니다. 또한 내 Shop 엔터티를 수정 해 주셔서 감사합니다! – Gauthier

관련 문제