I came across a problem recently that I have to make a find(all) query in cakephp.
Problem is I was unable to call model function to cakephp controller & add a command in function that would allow me to make a find(all) function in cakephp and retrieve all rows in a database that I was looking for.
I would like to make a simple query to make a call to database table that is in database that retrieves rows in table that are matching query.
public function display(){ $query = $HouseParty->find('all', [ 'conditions' => ['HouseParty.popular >' => 0], 'contain' => ['title', 'image', 'popular'], 'limit' => 6 ]); $results = $query->all(); var_dump($results); }
Also what is code to make call to model in Cakephp Controller. So controller knows that we are making calls to models from controller. So we can interact with models from controller because I intend to make multiple calls to different models from same controller in future. Please be helpful looking for a response been stuck on this problem for days just making a question here that is relevant & resourceful for others as well. I would like to make a question come handy to other community members I would just need to get code apart from what I have mentioned above that what code should I write in order to make a call for calling models in one controller – call to multiple models from one controller. Is the above code correct and is so what makes it from giving an error as below. So what is code to make calls to models from database.
Error i get is below:
Call to a member function find() on null
Advertisement
Answer
I think you should start with a simple query based on your model.
I will assume your model is HouseParties (a database table named house_parties).
Then in your HousePartiesController, you can query:
$query = $this->HouseParties->find('all', [ 'conditions' => ['popular >' => 0], 'limit' => 6 ]);
I think you may be misusing ‘contain’ as a select. ‘contain’ is for related tables, not fields in the table you are querying.
If you go through the CMS tutorial in full https://book.cakephp.org/4/en/tutorials-and-examples.html it should be clear.