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

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
主站蜘蛛池模板: 曲松县| 清镇市| 连山| 伊春市| 伊金霍洛旗| 海淀区| 乡宁县| 长春市| 广南县| 名山县| 尼勒克县| 青岛市| 介休市| 五莲县| 苍山县| 洛南县| 三亚市| 北宁市| 永年县| 封开县| 陈巴尔虎旗| 神木县| 蚌埠市| 盱眙县| 搜索| 安图县| 浦城县| 西峡县| 花垣县| 米林县| 澄江县| 贵定县| 渝北区| 西昌市| 太和县| 桂东县| 宜昌市| 安陆市| 宁化县| 辽宁省| 西丰县|