- 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.
- Animate 2022動畫制作:團體操隊形
- 改變思維:菜鳥也能做出震撼PPT(全彩版)
- 跟儲君老師學(xué)Excel極簡思維
- PyTorch深度學(xué)習(xí)簡明實戰(zhàn)
- Photoshop CC摳圖+修圖+調(diào)色+合成+特效實戰(zhàn)視頻教程
- Apache Solr 3.1 Cookbook
- Configuring IPCop Firewalls: Closing Borders with Open Source
- Unity 3 Game Development Hotshot
- Photoshop-CorelDRAW 基礎(chǔ)培訓(xùn)教程
- Photoshop攝影后期必修5項核心技法 基本調(diào)整+光影校正+色彩修飾+局部處理+銳化降噪
- Web Host Manager Administration Guide
- VRP12虛擬現(xiàn)實編輯器標(biāo)準(zhǔn)教程
- Premiere視頻編輯案例教程:Premiere Pro 2020(微課版·第2版)
- 中文版Illustrator CS6基礎(chǔ)培訓(xùn)教程(第2版)
- AI商業(yè)廣告設(shè)計實戰(zhàn)108招:ChatGPT+Photoshop+Firefly+Midjourney