如何在Doctrine 2中使用OneToMany关系更新记录?
|
我有一个用户实体,我正在尝试从UserService更新它。当我尝试更新设置为数组集合的属性时,问题就来了。
/**
*
* @param \\Doctring\\Common\\Collections\\Collection $property
* @OneToMany(targetEntity=\"Countries\",mappedBy=\"user\", cascade={\"persist\", \"remove\"})
*/
private $countries;
我不确定是否应该在重新设置$ countries之前以某种方式删除所有$ countries,或者是否有办法选择要删除的$ countries并设置不同的$ countries ....这就是到目前为止,我的updateUser方法看起来:
public function updateUser($user) {
$entity = $this->getUser($user[\'id\']);
if (!$entity)
throw new Exception(\'Error saving user!\');
$countries = $this->getCountriesArray($user); //countries already set in the db
$tempCountries = array();
$delete = array();
foreach ($countries as $country) {
$found = false;
if (in_array($country, $user[\'countries\'])) {
$tempCountries[] = $country;
} else {
$delete[] = $country;
}
}
$tempCountries = array_unique(array_merge( //combine the countries from the db we want to leave
$tempCountries, //with those the user selected
$user[\'countries\']));
...
//here I need something to remove the countries in $delete...right?
...
$entity->setEmail($user[\'email\']);
$entity->setResponsable($user[\'responsable\']);
$entity->setPassword($this->createPass($user[\'password\']));
$entity->setUrl($user[\'url\']);
$entity->setRole($user[\'role\']);
$modelCountries = array();
foreach($tempCountries as $c) {
$p = new Countries();
$p->setCountryName($c);
$p->setUser($entity);
$modelCountries[] = $p;
}
$entity->setCountries($modelCountries);
$this->em->persist($entity);
$this->em->flush();
}
请stackOverflow ...给我一些帮助。
没有找到相关结果
已邀请:
2 个回复
浅镁
泻伴墓荒
因此,当我在通话中传递新层时,我只是从规则映射器中删除该角色的所有层
然后打电话往平常
希望能有所帮助