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

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
主站蜘蛛池模板: 瓮安县| 龙州县| 鄂托克旗| 万载县| 星子县| 东丽区| 韩城市| 开鲁县| 天等县| 区。| 祁门县| 安泽县| 体育| 巫溪县| 云霄县| 穆棱市| 泾川县| 婺源县| 寿阳县| 桓台县| 商南县| 平湖市| 堆龙德庆县| 隆林| 郴州市| 巴东县| 朝阳区| 东至县| 武穴市| 陇川县| 白沙| 泊头市| 无为县| 阳西县| 昌宁县| 邻水| 威信县| 天等县| 新化县| 东明县| 黑山县|