官术网_书友最值得收藏!

Adding Containable to all models

The Containable behavior is a part of the CakePHP core, and is probably one of the most important behaviors we have to help us deal with model bindings.

Almost all CakePHP applications will benefit from its functionalities, so in this recipe we see how to enable it for all models.

How to do it...

Create a file named app_model.php and place it in your app/ folder, with the following contents. If you already have one, make sure that either you add the actsAs property shown as follows, or that your actsAs property includes Containable.

<?php
class AppModel extends Model {
public $actsAs = array('Containable');
}
?>

How it works...

The Containable behavior is nothing more and nothing less than a wrapper around the bindModel() and unbindModel() methods, defined in the CakePHP's Model class. It is there to help us deal with the management of associations without having to go through a lengthy process of redefining all the associations when calling one of these methods, thus making our code much more readable and maintainable.

This is a very important point, because a common mistake CakePHP users make is to think that Containable is involved in the query-making process, that is, during the stage where CakePHP creates actual SQL queries to fetch data.

Containable saves us some unneeded queries, and optimizes the information that is fetched for each related model, but it will not serve as a way to change how queries are built in CakePHP.

See also

  • Limiting the bindings returned in a find
  • Modifying binding parameters for a find
  • Modifying binding conditions for a find
主站蜘蛛池模板: 沈阳市| 定南县| 和林格尔县| 通海县| 甘洛县| 淮北市| 屏东市| 通许县| 改则县| 宁波市| 陇南市| 张家界市| 登封市| 金堂县| 高陵县| 烟台市| 陆丰市| 那曲县| 紫金县| 兖州市| 蒙自县| 黑龙江省| 林口县| 临洮县| 建宁县| 根河市| 任丘市| 武城县| 莎车县| 榆林市| 天水市| 肥东县| 和顺县| 文成县| 诸暨市| 芦溪县| 南宁市| 通海县| 玉溪市| 芜湖市| 阿克陶县|