Home  >  Article  >  Backend Development  >  Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理

WBOY
WBOYOriginal
2016-06-23 13:47:34904browse

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中已建立了它们之间的 一对多关系,因此我们可以自由搭配组合

效果图如下:




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn