Jon*_*han 1 cakephp exists associations
cakephp 1.3是否存在($ id)函数,以查看表中是否存在记录?我有一个从另一台服务器同步的数据库.我的数据库有2个表,同步表,以及我已经放入同步表扩展的表.我的应用程序现在列出主(同步)表中的所有项目,当他们单击该项目时,它会将它们带到视图以添加与第二个表格不同的信息.它将ID传递给第二个表.
我想要做的是先检查第二个表,查看是否存在具有相应外键的记录,如果存在,则转到第二个表中该记录的编辑屏幕,如果没有,我想确定记录存在于第一个表中,如果存在,则将记录添加到第二个表中,并将给定的$ id作为外键.
现在考虑它,它将正确的id传递给第二个表,我只是不希望用户能够键入一个数字并假设它存在于主表中并将记录添加到第二个表中,这不会实际存在.如果没有检查的功能,我可以使用该关联进行检查吗?喜欢:
if (!$this->Table2->Table1->id) {
//if id does not exist in parent table don't create the record in the second table and print an error
} else {
//id does exist in parent table either add a new record with the foreign key being the id passed from parent or redirect to edit screen for that record in second table
}
Run Code Online (Sandbox Code Playgroud)
使用Model :: exists()函数:
$model->id = 5;
if($model->exists()){
// Record with ID 5 found
}else{
// Record not found
}
Run Code Online (Sandbox Code Playgroud)
你也可以尝试加载并同时做两件事(检查并加载,如果存在)
$model->id = 5;
if($model->read()){
// Record exists and you have in $model->data the data
}else{
// Record not found
}
Run Code Online (Sandbox Code Playgroud)
如果我理解得很好(我的英语不太好),在你的情况下,你可以这样做:
if(!$this->Table2->Table1->id || !$this->Table2->Table1->exists()){
// Table1->id is empty or record not found
}else{
// Table1->id has id and record exists
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3119 次 |
| 最近记录: |