- CakePHP 1.3 Application Development Cookbook
- Mariano Iglesias
- 251字
- 2021-04-09 22:04:17
This recipe shows how to use Containable
to change the conditions used to fetch data related to a model through a binding.
We need to have Containable
added to our models, and we also need some sample models and data to work with. Follow the recipe, Adding Containable to all models, and the Getting ready section of the recipe, Modifying binding parameters for a find.
If we want to obtain the first User
record together with the published Article
records that user owns, but ordered by latest articles first, and limiting some of the returned fields, we use the conditions
binding setting:
$user = $this->User->find('first', array(
'contain' => array(
'Article' => array(
'fields' => array('Article.title'),
'conditions' => array( 'Article.published' => 1 )
)
)
));
Using our sample data, the preceding query will result in the following array structure:
array( 'User' => array( 'id' => '1', 'name' => 'John Doe', 'email' => 'john.doe@example.com', ), 'Article' => array( array( 'title' => 'John\'s Post 1', 'user_id' => '1' ), array( 'title' => 'John\'s Post 2', 'user_id' => '1' ), array( 'title' => 'John\'s Post 4', 'user_id' => '1' ) ) )
The conditions binding setting is another binding parameter, such as those shown in the recipe, Modifying binding parameters for a find. As such, the Containable
behavior uses the built-in bindModel()
method defined in CakePHP's Model
class to alter the binding conditions defined in the contain
find operation.
- 突破平面Photoshop UI界面設計與制作剖析
- AI繪畫教程:Midjourney使用方法與技巧從入門到精通
- Painter繪畫實例教程
- VR、AR與MR項目開發實戰
- Scratch 1.4: Beginner’s Guide
- Photoshop CC 實戰入門
- AIGC輔助數據分析與挖掘:基于ChatGPT的方法與實踐
- SolidWorks2014基礎實例教程
- SolidWorks快速入門教程(2022中文版)
- 從零開始:AutoCAD 2010中文版建筑制圖基礎培訓教程(第2版)
- Backbone.js Cookbook
- 從零開始:Illustrator CC中文版基礎培訓教程
- 玩轉電子設計:基于Altium Designer的PCB設計實例(移動視頻版)
- 中文版3ds Max 2020基礎教程
- 這樣學Excel數據處理與分析更高效(視頻版)