Sunday, May 3, 2015

Re: Preferred way to retrieve data with cake 3.0

The latter is preferred, i.e. the way using the select() and where() methods. The reason is that the is a lot more flexibility in what you can do with the new way.

A quick way to check if there if there is data in a query is:

if ($query->all()->count() > 0)

On Friday, May 1, 2015 at 10:54:43 PM UTC+2, Aaron Bird wrote:
Hi

I am making the move from cake 2.x to 3.x

I am starting a new project so figured this a good time.  I have been reading the book alot and its starting to make more sense.

I have been playing with different ways of retrieving my data and am a little confused as to the best practice method for retrieving data.

Coming from cake 2.0 This method is familiar to me:

// In a controller or table method.  $query = $articles->find('all', [      'conditions' => ['Articles.created >' => new DateTime('-10 days')],      'contain' => ['Authors', 'Comments'],      'limit' => 10  ]);
However, cake 3.0 seems to use this method as well

$query = $articles->find('all')      ->where(['Articles.created >' => new DateTime('-10 days')])      ->contain(['Comments', 'Authors'])      ->limit(10);
Is one "better" than the other?

Another quick question is how do I quickly identify if the above queries did not retrieve any data?  I used to check for an empty array on the returned results.  

Many Thanks!

Aaron

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: