《PHP應用:CakePHP框架Model關聯對象用法分析》要點:
本文介紹了PHP應用:CakePHP框架Model關聯對象用法分析,希望對您有用。如果有疑問,可以聯系我們。
PHP學習本文實例講述了CakePHP框架Model關聯對象.分享給大家供大家參考,具體如下:
PHP學習CakePHP 提供關聯數據表間的映射,共有4種類型的關聯:
PHP學習hasOne
,hasMany
,belongTo
,hasAndBelongsToMany
.
PHP學習設定了Model間的關聯關系定義,CakePHP就會將基于關系數據庫的數據映射為基于對象的關系模型.
PHP學習但是你應該確保遵循CakePHP的命名規則.
PHP學習命名規則中需要考慮的3個內容是,外鍵,model名字,表名.
PHP學習外鍵:單數形式的 modelName_id
表名:復數形式的 model名
Model名:駝峰法命名單數形式(見文件inflector.php).
PHP學習hasOne 關聯的定義與查詢:通過在model中增加一個array來實現.
PHP學習
class User extends AppModel
{
var $name = 'User';
var $hasOne = array(
'UserInfos' => array(
'className' => 'UserInfos',
'conditions' => '',
'order'=> '',
'dependent' => true,
'foreignKey' => 'user_id'
)
);
}
PHP學習$hasOne 變量是一個array,CakePHP 通過該變量來構建 Blog 與 User 之間的關聯.
PHP學習className: 關聯對象的類名.
conditions: 關聯對象的選擇條件.
order: 關聯對象的排列方式.
dependent: 這是個布爾值,如果為 true,父對象刪除時會級聯刪除關聯子對象.
foreignKey: 指向關聯 Model 的外鍵字段名,僅在不遵循 Cake 的命名約定時需要設置.
PHP學習belongsTo 關聯的定義與使用
PHP學習
class Blog extends AppModel
{
var $name = 'Blog';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'conditions' => '',
'order' => '',
'foreignKey' => 'user_id'
)
);
}
PHP學習className: 關聯對象的類名.
conditions: SQL 條件子句以限定關聯的對象.
order: 關聯對象的排序子句.
foreignKey: 關聯對象所對應的外鍵字段名.
PHP學習hasMany 關聯的定義與查詢
PHP學習
class User extends AppModel
{
var $name = 'User';
var $hasMany = array(
'Blog' => array(
'className' => 'Blog',
'conditions' => 'Blog.status = 1',
'order' => 'Blog.created DESC',
'limit' => '5',
'foreignKey' => 'user_id',
'dependent' => true,
'exclusive' => false, 'finderQuery' => ''
)
);
}
PHP學習$hasMany array 用來定義 User 包含多條 Blog 這樣的關聯關系.
PHP學習className: 關聯對象類名.
conditions: 關聯對象限定條件.
order: 關聯對象排列子句.
PHP學習limit: 用 limit 來限定檢索的關聯對象數量.
PHP學習foreignKey: 外鍵字段名.
dependent: 是否級聯刪除.
exclusive: 如果為 TRUE,所有的關聯對象將在一句 SQL 中刪除,model 的 beforeDelete 回調函數不會被執行.
finderQuery: 定義一句完整的 SQL 語句來檢索關聯對象,能夠對關聯規則進行最大程度上的控制.
PHP學習同樣可以為 Blog 加上關聯 User 對象的 belongTo 關聯.
PHP學習hasAndBelongsToMany 關聯的定義與查詢.
PHP學習
class Blog extends AppModel
{
var $name = 'Blog';
var $hasAndBelongsToMany = array('Tag' =>
array('className' => 'Tag',
'joinTable' => 'blogs_tags',
'foreignKey' => 'blog_id',
'associationForeignKey'=> 'tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => true,
'finderQuery' => '',
'deleteQuery' => '',
)
);
}
PHP學習$hasAndBelongsToMany array 是定義 HABTM 關聯的變量.
PHP學習className: 關聯對象類名.
joinTable: 如果沒有遵循 Cake 的命名約定建立關聯表,則需要設置該 key 來指定關聯表.
foreignKey: 定義本 mode 在關聯表中的外鍵字段.
associationForeignKey: 關聯表中指向關聯對象的外鍵字段名.
conditions:? 關聯對象限定條件.
order: 關聯對象排序子句.
limit: 關聯對象數量限制.
uniq: 設為 true 的話,重復的關聯對象將被過濾掉.
finderQuery: 完整的關聯對象檢索語句.
deleteQuery: 完整的刪除關聯關系的SQL 語句.
PHP學習保存關聯對象:
PHP學習當關聯的兩個對象都沒有持久化,你需要首先持久化主對象.
PHP學習在保存子對象時要把父對象的 ID 保持在子對象中.
PHP學習保存 hasAndBelongsToMany 關聯對象:
PHP學習使用 bindModel()
和 unbindModel()
實時地改變關聯關系:
PHP學習更多關于php框架相關內容感興趣的讀者可查看本站專題:《php優秀開發框架總結》、《codeigniter入門教程》、《ThinkPHP入門教程》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP學習希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/372.html