Skip to content

Instantly share code, notes, and snippets.

@davidyell
Created October 30, 2014 09:48
Show Gist options
  • Save davidyell/c2758609402c981babcc to your computer and use it in GitHub Desktop.
Save davidyell/c2758609402c981babcc to your computer and use it in GitHub Desktop.
These two finds are equal in CakePHP 3, so which is better?
<?php
class ExamplesController extends AppController {
public function example($id) {
$match = $this->Matches->get($id, [
'contain' => [
'Venues',
'Formats'
]
]);
// OR
$match = $this->Matches->find()
->where(['Matches.id' => $id])
->contain([
'Venues',
'Formats'
]);
}
}
@davidyell
Copy link
Author

Using the get() method assumes that the records exists and will throw an exception if the record isn't there.

The find() method will cope if no records are found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment