在我继续保存记录之前,我试图查明数据库中是否已经存在类似的记录。我已经用谷歌搜索并发现它看起来应该有效但不幸的是没有。我是 cakephp 的新手,无法找出正确的查询。
$this->Tape->recursive = -1;
$tapeexists = $this->Tape->find('all', array('condition'=>array('Tape.name LIKE'=>'blondie%')));
$this->set('output', $tapeexists);
如果我 print_r() View 中的结果,我可以看到它只是运行并获取该表中的所有结果,其中没有一个名字像“金发女郎”!
最佳答案
我认为您只是在查找时拼错了 conditions
:
$tapeexists = $this->Tape->find('all', array('conditions'=>array('Tape.name LIKE'=>'blondie%')));
这应该会给你预期的结果。
对于不允许重复的特定目标,我建议在 Tape
模型上创建验证规则。有一个特定的内置规则旨在避免重复,因此您最好还是使用它。查看有关数据验证的更多信息 here - 查看 isUnique
核心验证规则。
关于php - 在 cakephp 的查询中使用 LIKE 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281996/