gpt4 book ai didi

php - 基于唯一字段的 Doctrine 保存方法更新/插入

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:33 24 4
gpt4 key购买 nike

我读了很多帖子,我发现了很多关于如何保存到插入与更新的变体,但找不到适合我的东西。

我猜测如果 preSave() 由 save() 自动执行(如果存在),preSave 将是可行的方法。

columns:
email:
type: string(255)
unique: true
email: true

我需要 save() 来检查一个文件是否被设置为唯一的如果是这样,检查在这种情况下电子邮件地址的字段数据是否是唯一的。根据该信息决定插入或更新已更改的已发布字段。

最佳答案

只需添加更多逻辑,可能会有更好的方法,但这对我有用。

public function  preInsert($event) {
if ($this->state() == Doctrine_Record::STATE_TDIRTY) {
$r == findRecordInDatabaseUsingThis();
$this->assignIdentifier($r->id);
$this->refresh();
$this->state(Doctrine_Record::STATE_CLEAN); // STATE_DIRTY to do an update, this will just use the existing record without updating
$event->skipOperation();
}
}
}

关于php - 基于唯一字段的 Doctrine 保存方法更新/插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com