cakePHP:使用除主键之外的其他字段在数据库中查找一行?

时间:2014-08-23 15:13:12

标签: cakephp controller

我要检查我的数据库中是否存在其中一个字段与自定义值匹配的行。 例如考虑包含字段的表许可证:(id,serial,validity)。 我要检查控制器中的两个条件:

  1. 使用serial' xyz'的许可证在db
  2. 中出现
  3. 使用serial' xyz'的许可证有效字段值有效'
  4. 我应该如何完成此代码的$选项:

    public function validity($serial = null) {
        $this->autoRender = false; // We don't render a view in this example
        $options = ?????;
        $license = $this->License->find('first', $options);
        if ($license){
            // it is valid and present
            $data = array('validity' => 'valid');
        );
        }else{
            //not present actions
            $data = array('validity' => 'invalid');
        }
    
        $this->response->body(json_encode($data));
    }
    

1 个答案:

答案 0 :(得分:0)

options参数有很多可以使用的参数,如条件,顺序和字段。在您的情况下,您需要条件

$options=array('conditions'=>array('License.serial'=>'xyz', 'License.validity'='valid'));

(默认情况下,它是条件之间的AND)