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

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
主站蜘蛛池模板: 安宁市| 南京市| 庆城县| 喀喇| 德州市| 富平县| 吴桥县| 宝山区| 铜梁县| 吐鲁番市| 抚顺市| 新乡县| 平原县| 华宁县| 贡嘎县| 宕昌县| 延长县| 萨嘎县| 安顺市| 宁明县| 东兴市| 海南省| 泰顺县| 湄潭县| 方城县| 通道| 天长市| 买车| 呼图壁县| 平果县| 西安市| 靖安县| 金秀| 宜章县| 南汇区| 白玉县| 青冈县| 松桃| 高州市| 凉山| 新竹县|