Cakephp 3:如何在get方法中给出条件?

Ali*_*rim 1 cakephp cakephp-3.0

我试图在cakephp3 get方法中给出一个条件,其中数据将通过外部id获取而不是主键.这里我尝试了下面的代码:

$eventPasswordAll = $this->EventPasswordAll->get($id, [
            'conditions'  => ['event_id'=>$id],
            'contain'     => ['Events']
]);
Run Code Online (Sandbox Code Playgroud)

但它显示我的数据id(primary key),而不是event_id.我如何在get方法中添加这个条件event_id=my get id

Hol*_*olt 6

不要使用get,使用find.根据CakePHP 3.0 Table API,该get方法:

通过主键查找后返回单个记录,如果没有找到记录,则此方法抛出异常.

你需要使用find:

$eventPasswordAll = $this->EventPasswordAll->find('all', [ // or 'first'
    'conditions' => ['event_id' => $id],
    'contain'    => ['Events']
]);
// or
$eventPasswordAll = $this->EventPasswordAll->find()
    ->where(['event_id' => $id])
    ->contain(['Events']) ;
Run Code Online (Sandbox Code Playgroud)