What is difference b/w get or find in cakephp 3. Why we use get or find in cakephp 3 .
what is difference between get or find in cakephp 3
1.4k views Asked by sourabh singh AtThere are 3 answers
On
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
On
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()
Get is generally used for getting a single entity by primary key .
In find we fine the data .
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