推进:如何删除通过多对多关系建立的链接 [英] Propel: how to remove link made via many-to-many relation
问题描述
(链接到上一个问题,以防万一:在管理员形式中与一对多关系作斗争)
(link to previous question just in case: Struggling with one-to-many relation in an admin form)
在我的Symfony-1.3/Propel-1.4项目中,我与 User 和 Partner 之间存在这种多对多关系.保存用户时,如果它具有确定的布尔标志为true,则我想清除所有指向合作伙伴的链接.这是我目前正在执行的操作,但不起作用:
I have this many-to-many relation in my Symfony-1.3 / Propel-1.4 project between User and Partner. When the User is being saved, if it has certain boolean flag being true, I want to clear all the links to the partners. Here is what I do at the moment and it doesn't work:
// inside the User model class
public function save(PropelPDO $con = null) {
if ($this->getIsBlaBla()) {
$this->setStringProperty(NULL);
$this->clearUserPartners();
}
parent::save($con);
}
将字符串属性设置为NULL即可;查看数据库清楚地表明了这一点.但是,USER_PARTNER表仍然保留了用户和合作伙伴之间的关系.所以我想我必须一个个地清除链接,就像这样:
Setting the string property to NULL works; looking at the DB clearly shows it. Thing is however, the USER_PARTNER table still holds the relations between the users and the partners. So I figured I have to clear the links one by one, like this:
foreach($this->getUserPartners() as $user_partner) {
$user_partner->delete();
//UserPartnerPeer::doDelete($user_partner); // tried that too
}
两个都不做.
正如我在上一个问题中提到的那样,我只是通过反复试验来学习Symfony,因此我显然错过了一些非常明显的事情.请指出正确的方向!
As I mentioned in my previous question, I am just monkey-learning Symfony via trial and error, so I evidently miss something very obvious. Please point me in the right direction!
这是我的工作方式:
将代码移至Form类,如下所示:
Moved the code to the Form class, like so:
public function doSave(PropelPDO $con = null) {
parent::doSave($con);
if ($this->getObject()->getIsSiteOwner()) {
$this->getObject()->setType(NULL);
$this->getObject()->save();
foreach($this->getObject()->getUserPartners() as $user_partner) {
$user_partner->delete();
}
}
return $this->getObject();
}
public function updateObject($values = null) {
$obj = parent::updateObject($values);
if ($obj->getIsSiteOwner()) {
$obj->clearUserPartners();
}
return $this->object;
}
这是什么:
- 当布尔标志"is_site_owner"打开时,它会清除"type"字段并保存"对象(可惜我已经很久没弄清楚了).
- 删除所有现有的UserPartner多对多链接对象.
- 清除(通过DoubleList)新建立的UserPartner关系.
这是我所需要的.感谢所有参加的人.
Which is what I need. Thanks to all who participated.
推荐答案
好吧,现在您有了一个多对多关系,在数据库中将其实现为三个表(User,Parter和UserPartner).在Symfony和Propel上也会发生同样的事情,因此您需要在应该在UserForm中声明的doSave方法上执行类似的操作:
Okey so now you have a many-to-many relation where in database terms is implemented into three tables (User , Parter and UserPartner). Same thing happens on Symfony and Propel, so you need to do something like this on the doSave method that should declare in UserForm:
public function doSave($con = null)
{
parent::doSave($con); //First all that's good and nice from propel
if ($this->getValue('please_errase_my_partners_field'))
{
foreach($this->getObject()->getUserPartners() as $user_partner_relation)
{
$user_partner_relation->delete();
}
}
return $this->getObject();
}
检查应在BaseUser.class.php(lib/model/om/BaseUser.class.php)上声明的方法名称"getUserPartners"
Check the method name "getUserPartners" that should be declared on the BaseUser.class.php (lib/model/om/BaseUser.class.php)
这篇关于推进:如何删除通过多对多关系建立的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!