PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

[FIXED] Cakephp 3 : How to give condition in get method?

 December 31, 2021     cakephp, cakephp-3.0     No comments   

Issue

I am trying to give a condition in cakephp 3 get method, where data will fetch by foreign id not primary key. Here I have tried below code:

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

But it showing me data according to id(primary key), not by event_id. How I add this condition in get methods like where event_id=my get id ?


Solution

Don't use get, use find. According to CakePHP 3.0 Table API, the get method:

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

You need to use 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']);


Answered By - Holt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing