2011-06-21 66 views
1

如何保存一條記錄(來自服務),並將其中一個屬性設置爲與另一個實體的OneToMany關係?如何使用Doctrine 2保存OneToMany關係字段的記錄?

我有以下服務類爲我的用戶:

namespace Federico\Entity; 
use Federico\Entity\User; 
/** 
* Description of UserService 
* 
* @author fiodorovich 
*/ 
class UserService { 
protected $em; 

public function __construct ($em) { 
    $this->em = $em; 
} 

public function saveUser($user) { 
    if ($user['id'] != null) { //update 
     $entity = $this->getUser($user['id']); 

     //do something 

     if (!$entity) 
      throw new Exception('Error saving user!'); 
    } else { //insert 
     $entity = new User(); 

     foreach ($user as $k => $v) { 
      if ($k !== 'submit') { 
       if ($k == 'password') { 
        //echo $this->_salt;die; 
        $entity->$k = $this->createPass($v); 
       } else { 
        $entity->$k = $v; 
       } 
      } 
     } 
    } 
    $this->em->persist($entity); 
    $this->em->flush(); //save the user 
} 

//do something else... 
} 

但是當我嘗試節省了用戶和國家的財產(這指的是一對多的關係oneUser-manyCountries的集合)設爲我得到一個「類不存在」異常。

編輯:用戶和國家的實體

namespace Federico\Entity; 
use Doctrine\Common\Collections\ArrayCollection; 
/** 
* Description of User 
* @Table(name="users") 
* @Entity 
* @author fiodorovich 
*/ 
class User 
{ 
/** 
* @var integer 
* @Id @Column (name="id", type="integer", nullable=false) 
* @GeneratedValue(strategy="AUTO") 
* 
*/ 
private $id; 

/** 
* @Column(type="string",length=60,nullable=true, unique=true) 
* @var string 
*/ 
private $email; 

/** 
* @Column(type="string",length=60,nullable=true) 
* @var string 
*/ 
private $password; 

/** 
* @Column(type="string",length=60,nullable=true,unique=true) 
* @var string 
*/ 
private $url; 

/** 
* @Column(type="string",length=60,nullable=true,unique=true) 
* @var string 
*/ 
private $responsable; 

/** 
* @Column(type="string",length=20,nullable=true) 
* @var string 
*/ 
private $role; 

/** 
* 
* @var datetime 
* @Column(type="datetime", nullable=false) 
*/ 
private $created; 

/** 
* 
* @param \Doctring\Common\Collections\Collection $property 
* @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"}) 
*/ 
private $countries; 

public function __construct() 
{ 
    $this->created = new \DateTime(date('Y-m-d H:i:s')); 
    $this->countries = new ArrayCollection(); 

} 

public function __get($property) 
{ 
    return $this->$property; 
} 

public function __set($property, $value) 
{ 
    $this->$property = $value; 
} 

public function getCountries() 
{ 
    return $this->countries; 
} 
} 

而且國家實體:

namespace Federico\Entity; 
/** 
* @Table(name="countries") 
* @Entity 
* @author fiodorovich 
* 
*/ 
class Countries { 
/** 
* @var integer 
* @Id @Column (name="id", type="integer", nullable=false) 
* @GeneratedValue(strategy="AUTO") 
* 
*/ 
private $id; 

/** 
* 
* @var string 
* @Column(type="string") 
*/ 
private $countryName; 

/** 
* 
* @var User 
* @ManyToOne(targetEntity="User", inversedBy="id") 
* @JoinColumns({ 
* @JoinColumn(name="user_id", referencedColumnName="id") 
* }) 
*/ 
private $user; 


public function __get($property) 
{ 
    return $this->$property; 
} 

public function __set($property, $value) 
{ 
    $this->$property = $value; 
} 

} 
+0

如果您不發佈模型代碼和調用saveUsers方法的代碼,很難說出問題。 –

+0

@Rene Terstegen實體剛剛發佈在您的請求 –

回答

1

閱讀文檔Working With Associations。特別是協會管理方法部分。這應該給你一個關於如何添加記錄到關聯的想法。你沒有放置User類/實體,所以我假設你正在初始化__construct中的「countries」字段。此外,您的錯誤表明自動加載器無法找到User類或Country類,在您的示例中,我沒有看到您在「use」語句中導入了該類。錯誤可能是因爲這一點,可能是阻止你前進的原因。

+0

我剛剛更新了線程與實體,並試圖找出「協會管理方法」,感謝您的鏈接 –

0

我沒有看到一行代碼,您將國家添加到用戶。如果你想添加一個國家的用戶請記住你的$this->countries是一個集合,你需要以這種方式威脅。你使用的神奇二傳手不適合收藏。

要一個國家添加到用戶,你還必須將用戶添加到該國:

$User->countries->add($CountryObject); 
$CountryObject->user = $User; 

然後將其保存到數據庫:

$EntityManager->persist($User); 
$EntityManager->persist($CountryObject); 
$EntityManager->flush(); 

祝你好運!

@ la_f0ka

您有兩種選擇。在你之前的方法中,你的魔法制定者無法正常工作,但是你的魔法吸氣者很好。您可以繼續使用$user->email而不是$user->getEmail()。兩者都很好。有人說魔法吸氣劑有點慢,但我從來沒有注意到它。

只是不要訪問你的屬性公共。這破壞了你的封裝,這是對象編程最大的功能之一。

+0

我終於添加了所有的setter和getters,我知道了工作...但現在每當我需要訪問用戶'$ user = $ this-> em-> getRepository('Federico \ Entity \ User') - > findOneById($ id);'我需要訪問任何屬性我不再能夠通過'$ user-> email'來完成它,我必須去'$ user-> getEmail()'...我知道這只是一些額外的按鍵操作,但我只是想確保這是每個人都這樣做的方式......可以嗎? –

相關問題