2011-06-29 48 views
0

我終於成立了我的映射我的兩個表,我現在可以加入通過QueryBuilder的表學說2插入數據/協會映射..的Zend -

然而

,我不能將數據添加到連接列,其保持說空。

我的賬戶實體:

namespace Entities\Users; 

/** 
* @Entity(repositoryClass="\Entities\Users\Account") 
* @Table(name="account") 
* @HasLifecycleCallbacks 
*/ 
class Account extends \Entities\AbstractEntity { 

    /** 
    * @Id @Column(name="accid", type="bigint",length=15) 
    * @GeneratedValue(strategy="AUTO") 
    */ 
    protected $accid; 

    /** @Column(name="name", type="string", length=255) */ 
    protected $name; 

    /** @Column(name="profileid", type="integer", length=255) */ 
    protected $profileid; 


    /** @Column(name="acc_added_date", type="datetime", columnDefinition="datetime", nullable=true) */ 
    private $acc_added_date; 

    /** 
    * @ManyToOne(targetEntity="Profiledetails") 
    * @JoinColumn(name="profileid", referencedColumnName="pid") 
    */ 
    private $account; 

和我profiledetails實體:

namespace Entities\Users; 

/** 
* @Entity(repositoryClass="\Entities\Users\Profiledetails") 
* @Table(name="profiledetails") 
* @HasLifecycleCallbacks 
*/ 
class Profiledetails extends \Entities\AbstractEntity { 

    /** 
    * @Id @Column(name="pid", type="bigint",length=15) 
    * @GeneratedValue(strategy="AUTO") 
    */ 
    protected $accid; 
    /** @Column(name="name", type="string", length=255) */ 
    protected $name; 
    /** @Column(name="profileid", type="integer", length=255) */ 
    protected $profileid; 
    /** @Column(name="acc_added_date", type="datetime", columnDefinition="datetime", nullable=true) */ 
    private $acc_added_date; 
    /** 
    * @OneToMany(targetEntity="Account", mappedBy="account") 
    * @JoinColumn(name="pid", referencedColumnName="pid") 
    */ 
    private $stances; 

我用用:

$postdata array ('name'=>'jason'); 
    $entity =new \Entities\Users\Account; 
    $obj->setData($postdata); 

    $this->_doctrine->persist($obj); 
    $this->_doctrine->flush(); 
    $this->_doctrine->clear(); 

和它不添加..什麼的方式來增加數據添加到所有鏈接表更新的父表中?因爲之前我可以輸入一個配置文件,現在它爲空,因爲我用它作爲加入列。

+0

你不需要用於OneToMany的joincolumn,那麼你使用'setData'到$ obj,它不清楚它做了什麼,因爲你不顯示什麼是$ obj,但似乎問題在那裏。 – venimus

回答

1

如果您在關係定義中設置cascade=[persist],則可以「更新」鏈接的對象。您還需要爲關係的兩側設置@mappedBy和@inversedBy。基本上@mappedBy設置爲一對多側(稱爲逆側)和@inversedBy到多對一側(稱爲持有端)

http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional

正確的方法是基本上

//assume $this->_doctrine is instance of EntityManager 
$user = new User(); 
$user->setEmail('[email protected]'); 
$account = new Account(); 
$account->setName('john'); 
$account->setUser($user); 
$user->addAccount($account); //if no cascade set 
$this->_doctrine->persist($account); 
$this->_doctrine->persist($user); //if no cascade set 
$this->_doctrine->flush(); 

http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html#establishing-associations

+0

你如何刪除呢? –

+0

'$ em-> remove($ object_to_delete);' – venimus