Reputation: 168
What is difference b/w get or find in cakephp 3. Why we use get or find in cakephp 3 .
Upvotes: 0
Views: 1532
Reputation: 753
You can call find()
on table instance which constructs Query object and returns it and you can continue chaining other methods like where()
, ->select()
and Query objects are lazy, and will not be executed unless you call all()
, toArray()
, first()
, firstOrFail()
etc , more explanation here https://book.cakephp.org/3.0/en/orm/query-builder.html
Whereas get($primaryKey, $options = [])
will return record if found or throws not found exception if no record, it internally constructs query and calls firstOrFail()
Upvotes: 0
Reputation: 8496
When you use find()
method then you can pass condition as you want for retrieving data after filtering from db source. find('first')
or find('all')->first()
is behaving same.
BUT
get($id)
is only apply condition on primary key field of table. It means give result for one record only due to condition on primary key.
When use get()
method, if record is not found from db source then CakePHP throws NOT Found Exception. So this is very useful when give response as NOT FOUND - 404 For example Profile page, Blog detail page..etc
Upvotes: 1
Reputation: 2244
Get is generally used for getting a single entity by primary key .
$this->Models->get($id);//here $id is your tables primary key
In find we fine the data .
$this->Models->find('all');//get all the data
$this->Models->find('all')->first(); //get the first row
It can be used to find both the all data as well as first data . Here we can also put conditions
Follow this link for more info
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html
Upvotes: 0