Here's a quick little tidbit which while may be obvious to more seasoned coders, will really help out your database and make your model code more robust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php /** * AppModel * * Base application model. * * @author Joe Beeson */ class AppModel extends Model { /** * Number of associations to recurse through * during find calls. * * @var int * @access public * @see http://book.cakephp.org/view/1063/recursive */ public $recursive = -1; /** * Behaviors. * * @var array * @access public * @see http://book.cakephp.org/view/1071/Behaviors */ public $actsAs = array( 'Containable' ); }
Your standard AppModel more or less but with two entries: recursive is being set to -1 and we're adding ContainableBehavior.
Setting the recursive value to -1 tells CakePHP that we don't want it to bring in any associations when we run a query. Off the cuff this sounds like a bad idea, especially if you've never used ContainableBehavior before because you'd have to do some extra leg work to get associated records. That's the point. You should be explicitly asking the model to bring in the data you need and nothing more.
The ContainableBehavior is in there to help facilitate getting the associated data when we need to, instead of running two queries or manually writing our own ad-hoc joins.
While this all seems like extra work, your code will be cleaner and better off because of it -- being verbose with your database is a good thing.
