標籤:php laravel eloquent 擴充包 架構
Php Laravel架構 多表關係處理 之 Eloquent一對多關聯性處理
本博文主要介紹 Laravel 架構中 Eloquent 對一對多關聯性的處理,在 Laravel Administrator(後台擴充包)
您的資料庫可能是彼此相關的。比如,一篇部落格文章可能有許多評論,或者一個訂單與下訂單的使用者相關。Eloquent 使得管理和處理這些關係變得簡單。Laravel 提供了四種類型的關係: -一對一 -一對多 -多對多 - 多態關係
一對多
一個一對多關聯性的例子是一篇部落格文章有許多評論或者一個課程有的多次分數資訊等。
我們可以像這樣定義關聯式模式 Model:
<?php/** * sobjectinfo:課程資訊表 Model * soc_id :主鍵自增 * soc_name :課程名 * soc_teacher:授課老師 **/class SobjectInfo extends Eloquent { //自訂表格名(protected $table) protected $table = 'sobjectinfo'; //自訂主鍵(protected $primaryKey) protected $primaryKey = 'soc_id'; //關閉 建立時間 與 更新時間 的自動維護(protected $timestamps) public $timestamps = false; /* * 定義一對多關聯性 */ public function Scoreinfo(){ return $this -> hasMany('Scoreinfo','soc_id'); }}?>定義與之對應的逆向關係 Model:
<?php/** * scoreinfo:分數資訊表 Model * so_id :主鍵自增 * s_id :學生資訊表(stuinfo)主鍵 * soc_id :課程資訊表(sobjectinfo)主鍵 * score :分數 */class ScoreInfo extends Eloquent { //自訂表格名(protected $table) protected $table = 'scoreinfo'; //自訂主鍵(protected $primaryKey) protected $primaryKey = 'so_id'; //關閉 建立時間 與 更新時間 的自動維護(protected $timestamps) public $timestamps = false; /* * 分數表(ScoreInfo)與課程表(SobjectInfo)、學生資訊表(StuInfo)有主外鍵關係 * 並且是一對多的關係 */ public function StuInfo(){ return $this -> belongsTo('StuInfo','s_id'); } /* * 定義逆向關係指向主鍵表 * */ public function SobjectInfo(){ return $this -> belongsTo('SobjectInfo','soc_id'); }} ?>通過以上步驟的處理,表與表之間的一對多關聯性已確立,
下面將介紹在Laravel Administrato 後台中的實現 下拉式清單查詢、綁定等應用
<?phpreturn array( 'title' => '分數資訊', //欄目名 'single' => ' >>', //建立描述 'model' => 'ScoreInfo', //分數資訊 'form_width' => 960, //左側邊欄目寬 //列表 'columns' => array( 'so_id' => array( 'title' => '編號', 'select' => "so_id", 'sort_field'=>'so_id' ), 's_name'=>array( 'title'=>'學生姓名', 'relationship' => 'StuInfo', 'select' => '(:table).s_name', ), 'soc_name'=>array( 'title'=>'課程名稱', 'relationship' => 'SobjectInfo', 'select' => '(:table).soc_name', ), 'score'=>array( 'title'=>'考試分數', 'select'=>'score' ), ), //篩選資訊 'filters' => array( 'so_id' => array( 'title'=>'編號' ), 'SobjectInfo'=>array( 'type' => 'relationship', 'title' => '<span style="font-family: Arial, Helvetica, sans-serif;">課程名</span><span style="font-family: Arial, Helvetica, sans-serif;">',</span> 'name_field' => 'soc_name', ), 'StuInfo'=>array( 'type' => 'relationship', 'title' => '學生姓名', 'name_field' => 's_name', ), 'score'=>array( 'title'=>'考試分數', 'type' => 'number' ), ), //修改、新增 'edit_fields' => array( 'StuInfo'=>array( 'type' => 'relationship', 'title' => '學生姓名', 'name_field' => 's_name', ), 'SobjectInfo'=>array( 'type' => 'relationship', 'title' => '課程名', 'name_field' => 'soc_name', ), 'score'=>array( 'title'=>'考試分數', 'type'=>'text' ), ));?>
以上樣本展示的是 後台 分數資訊 類。
樣本中多次使用到 “學生姓名”、“課程名”,雖然他們儲存在不同的表中,但由於我們之前在 Model中已建立了它們之間的 一對多關聯性,因此我們可以自由搭配組合
如下:
10個Laravel4開發人員必用擴充包:http://blog.csdn.net/yimiyuangguang/article/details/39756115
Laravel Administrator 文檔
http://administrator.frozennode.com/docs/field-type-relationship
Laravel4 中文協助手冊:
http://pan.baidu.com/s/1jGl6cqa
Php Laravel架構 多表關係處理 之 Eloquent一對多關聯性處理