2014-01-05 24 views
1

我與教條2實體:俱樂部和用戶。在用戶權限我創建了一個manytomany表:Doctrine EM - 如何插入列

/** 
* @ORM\ManyToMany(targetEntity="Club", inversedBy="users") 
* @ORM\JoinTable(name="sms_followers") 
**/ 
private $smsfollower; 

現在我wanne添加數據到表中。我用這個:

//Get the club (no error handling created 
$getclub = $this->em->getRepository('Club')->findOneBy(array('id' => $clubid)); 

//Get the user by login ID 
$getuser = $this->em->getRepository('User')->findOneBy(array('id' => '7')); 

//Insert the userID and ClubID to the table sms_followers 
$getuser = $getclub->addSmsfollower(); 

我無法做到這一點......任何人都可以幫助我嗎?

請保持英語儘可能容易,我可是從荷蘭

回答

0

你最有可能想要的是一個SmsFollower添加到您已有的集合。

// in your User enitity 
public function addClub(Club $club) 
{ 
    $this->clubs->add($club); 
} 

// in your Club entity 
public function addSmsFollower(User $user) 
{ 
    $this->sms_followers->add($user); 
} 

然後在您的實體管理器中,您可以堅持對象然後刷新以保存它們。

提示:我將字段命名爲sms_followers和clubs(複數,meervoud),因爲它是多對多的關係。

+0

你有請給我一個例子嗎? –

+0

在你的情況下它會是:$ getclub-> addSmsfollower($ getuser); $這 - > EM->堅持($ getclub); $這 - > EM->沖洗();您應該閱讀關於此的Doctrine2文檔:http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html#persisting-entities –

+0

謝謝!這幫了我很多。 –