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

Cakephp 3 : How to give condition in get method?

我试图在 cakephp 3 get 方法中给出一个条件,其中数据将通过外部 ID 而不是主键获取。在这里,我尝试了以下代码:

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

但它根据 id(primary key) 而不是 event_id 向我显示数据。我如何在 get 方法中添加此条件,例如 where event_id=my get id ?

不要使用get, use find. According to CakePHP 3.0 Table API, the get方法:

Returns a single record after finding it by its primary key, if no record is found this method throws an exception.

您需要使用 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']);

有时候你想得到id和userid..

$this->loadModel('Jobapplications');
$application = $this->Jobapplications->find('all', [ 
  'conditions' => ['id' => $id, 'user_id' => $user_id]
]);
$application = $application->first();