2017-12-01 23 views
0

작동하지 영속화 내가 ManyToMany 단방향 관계와 billingAddresses로 엔티티 사용자가 :심포니 ManyToMany 단방향 캐스케이드 심포니 3.3으로

엔티티 \ 사용자 내가 cascade={"persist"}을 추가

class User extends EntitySuperclass implements AdvancedUserInterface, \Serializable 
{ 
/** 
    * @ORM\ManyToMany(targetEntity="Address", cascade={"persist"}) 
    * @ORM\JoinTable(name="User_BillingAddress", 
    *  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="billing_address_id", referencedColumnName="id")} 
    *) 
    * @Assert\Count(
    *  min = 1, 
    *  minMessage = "user.billing_addresses.min", 
    *) 
    * @Assert\Valid 
    */ 
    private $billingAddresses; 

/** 
    * Add billingAddress 
    * 
    * @param \AppBundle\Entity\Address $billingAddress 
    * 
    * @return User 
    */ 
    public function addBillingAddress(\AppBundle\Entity\Address $billingAddress) 
    { 
     $this->billingAddresses[] = $billingAddress; 

     return $this; 
    } 

    /** 
    * Remove billingAddress 
    * 
    * @param \AppBundle\Entity\Address $billingAddress 
    */ 
    public function removeBillingAddress(\AppBundle\Entity\Address $billingAddress) 
    { 
     $this->billingAddresses->removeElement($billingAddress); 
    } 

    /** 
    * Get billingAddresses 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getBillingAddresses() 
    { 
     return $this->billingAddresses; 
    } 
} 

하지만을의 "billingAddresses은"입니다 엔티티 사용자를 유지할 때 유지되지 않거나 저장되지 않습니다. 또한 양방향 관계로 시도했지만 주소는 여전히 유지되지 않습니다.

답변

0

당신은 수동으로 개체 간의 연결을 확립 할 필요가 명시 적으로 계속 호출하기 전에 위하고 있다면

$user->addBillingAddress($address); 

확인 및

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

플러시 다음을 수행해야합니다. 이 문서에 여기

http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#working-with-associations

감사합니다 설명입니다!

+0

이는 단방향 관계되는 생성자를 추가, 그래서 방법 "adduser 명령을()"가 없습니다. 교리 문서에서이 단방향 관계도 언급된다. 위와 같이 양방향 관계로 시도했지만 여전히 동일한 문제가 있습니다. 왜 각 "주소"를 개별적으로 유지해야합니까? 이것은'cascade = { "persist"}'로하지 않아야합니까? – user2625247

+0

단방향의 경우 $ user-> addBillingAddress ($ address); & $ em-> persist ($ user); 이 기능이 작동하는지 알려 주시면 답변도 편집 해 드리겠습니다. 감사합니다. –

+0

사용자 만 지속 할 때 작동하지 않으며 주소도 유지해야합니다. 그리고 나는 "사용자"가'cascade = { "persist"}'를 설정했기 때문에 이것을 이해하지 못합니다. – user2625247

0

봅니다 사용자 클래스

public function __construct() 
{ 
    $this->billingAddresses = new ArrayCollection(); 
} 
+0

나는 아직도 이것을 가지고있다. – user2625247