zoukankan      html  css  js  c++  java
  • 关联模型 (1对1)

    关联模型 (1对1)

    ThinkPHP5.0 的关联采用了对象化的操作模式,你无需继承不同的模型类
    只是把关联定义成一个方法,并且直接通过当前模型对象的属性名获取定义的关联数据。

    关联定义:一个user有一份profile

    DROP TABLE IF EXISTS `think_user`;
    CREATE TABLE IF NOT EXISTS `think_user` (
    `id` int(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nickname` varchar(25) NOT NULL,
    `name` varchar(25) NOT NULL,
    `password` varchar(50) NOT NULL,
    `create_time` int(11) UNSIGNED NOT NULL,
    `update_time` int(11) UNSIGNED NOT NULL,
    `status` tinyint(1) DEFAULT 0,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    DROP TABLE IF EXISTS `think_profile`;
    CREATE TABLE IF NOT EXISTS `think_profile` (
    `id` int(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `truename` varchar(25) NOT NULL,
    `birthday` int(11) NOT NULL,
    `address` varchar(255) DEFAULT NULL,
    `email` varchar(255) DEFAULT NULL,
    `user_id` int(6) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    

    hasOne (一个用户有一份档案)

    步骤一、定义用户模型,并在用户模型中定义关联方法profile()

    <?php
    namespace appindexmodel;
    use thinkModel;
    class User extends Model
    {
            // 开启自动写入时间戳
            protected $autoWriteTimestamp = true;
            // 定义自动完成的属性
            protected $insert = ['status' => 1];
            // 定义关联方法
            public function profile()
            {
            // 用户HAS ONE档案关联
            return $this->hasOne('Profile');
            }
    }
    

    很关键的是在关联的方法中调用 hasOne方法()

    hasOne方法有5个参数,依次分别是:
    hasOne('关联模型名','关联外键','主键','别名定义','join类型')

    1. 关联那个模型
    2. 关联表中的哪个字段是本模型的主键(也就是关联本模型的键是那个)
    3. 本模型的主键是那个
    4. 数据别名定义使用数组
    5. 默认是inner

    注意点:

    • 通常关联模型和当前模型都是相同的命名空间,如果关联模型在不同的命名空间,需要指定完整的类名,例如:
    // 关联admin模块下面的模型对象
    return $this->hasOne('appadminProfile');
    
    • 在关联查询的时候,默认使用当前模型的名称(小写)作为数据表别名,可以指定查询使用的数据表别名,例如:
    // 用户HAS ONE档案关联
    return $this->hasOne('Profile','user_id','id',['user'=>'member','profile'=>'info']);
    
    • 要进行模型的关联操作,我们必须同时定义好关联模型

    步骤二、定义Profile模型

    <?php 
    namespace appindexmodel;
    use thinkModel;
    class Profile extends Model
    {
            protected $type = [
            'birthday' => 'timestamp:Y-m-d',
            ];
    }
    
     ?>
    

    可以看到Profile 模型中并没有定义关联方法。如果你的关联操作都是基于User 模型的话, Profile
    模型中并不需要定义关联方法。
    如果你需要基于Profile 模型来进行关联操作,则需要在Profile 模型中定义对应的BELONGS_TO 关
    联,如下:

    <?php
    namespace appindexmodel;
    use thinkModel;
    class Profile extends Model
    {
        protected $type = [
        'birthday' => 'timestamp:Y-m-d',
        ];
        public function user()
        {
        // 档案 BELONGS TO 关联用户
        return $this->belongsTo('User');
        }
    }
    

    belongsTo 方法和hasOne 一样,也有5个参数:
    belongsTo('关联模型名','关联外键','关联模型主键','别名定义','join类型')

    1. 关联新增

    
    <?php
    namespace appindexcontroller;
    use appindexmodelProfile;
    use appindexmodelUser as UserModel;
    class User
    {
    // 关联新增数据
        public function add()
        {
            $user = new UserModel;
            $user->name = 'thinkphp';
            $user->password = '123456';
            $user->nickname = '流年';
            if ($user->save()) {
            // 写入关联数据
            $profile = new Profile;
            $profile->truename = '刘晨';
            $profile->birthday = '1977-03-05';
            $profile->address = '中国上海';
            $profile->email = 'thinkphp@qq.com';
            $user->profile()->save($profile);
            return '用户新增成功';
            } else {
            return $user->getError();
            }
        }
    }
    
    1. $profile = new Profile;
    2. $user->profile()->save($profile);

    关联模型的写入调用了关联方法profile() ,该方法返回的是一个Relation 对象,执行save 方法会自动传入当前模型User 的主键作为关联键值,所以不需要手动传入Profile 模型的user_id 属性。save 方法也可以直接使用数组而不是Profile 对象,例如:

    <?php
    namespace appindexcontroller;
    use appindexmodelProfile;
    use appindexmodelUser as UserModel;
    class User extends Controller
    {
    // 关联新增数据
    public function add()
    {
            $user = new UserModel;
            $user->name = 'thinkphp';
            $user->password = '123456';
            $user->nickname = '流年';
            if ($user->save()) {
            // 写入关联数据
            $profile['truename'] = '刘晨';
            $profile['birthday'] = '1977-03-05';
            $profile['address'] = '中国上海';
            $profile['email'] = 'thinkphp@qq.com';
            $user->profile()->save($profile);
            return '用户[ ' . $user->name . ' ]新增成功';
            } else {
            return $user->getError();
            }
    }
    }
    

    *$user->profile()->save($profile);

    2.关联查询

    一对一的关联查询很简单,直接把关联对象当成属性来用即可,例如:

    public function read($id)
    {
     $user = UserModel::get($id);
     echo $user->name . '<br/>';
     echo $user->nickname . '<br/>';
     echo $user->profile->truename . '<br/>';
     echo $user->profile->email . '<br/>';
    }
    
    
    

    以上关联查询的时候,只有在获取关联对象($user->profile)的时候才会进行实际的关联查询,缺点
    是会可能进行多次查询,但可以使用预载入查询来提高查询性能,对于一对一关联来说只需要进行一次查询即可获取关联对象数据,例如:

    public function read($id)
    {
    $user = UserModel::get($id,'profile');
    echo $user->name . '<br/>';
    echo $user->nickname . '<br/>';
    echo $user->profile->truename . '<br/>';
    echo $user->profile->email . '<br/>';
    }
    

    get方法使用第二个参数就表示进行关联预载入查询。

    • $user = UserModel::get($id,'profile');

    3.关联更新

    一对一的关联更新如下:

    public function update($id)
    {
        $user = UserModel::get($id);
        $user->name = 'framework';
        if ($user->save()) {
        // 更新关联数据
        $user->profile->email = 'liu21st@gmail.com';
        $user->profile->save();
        return '用户[ ' . $user->name . ' ]更新成功';
        } else {
        return $user->getError();
        }
    }
    

    4. 关联删除

    关联删除代码如下:

    public function delete($id)
    {
        $user = UserModel::get($id);
        if ($user->delete()) {
        // 删除关联数据
        $user->profile->delete();
        return '用户[ ' . $user->name . ' ]删除成功';
        } else {
        return $user->getError();
        }
    }
    
  • 相关阅读:
    jenkins连接Publish over SSH失败系列(一)Failed to add SSH key
    jenkins连接Publish over SSH失败系列(一)Failed to change to remote directory
    【2021-07-04】人生十三信条
    【2021-07-03】人生十三信条
    【2021-07-02】发现了成长一丝丝的痕迹
    【2021-07-01】回顾自己过往那点压力
    【2021-06-30】我现在连做梦都纯洁了
    【2021-06-29】做事情时老让自己分心
    【2021-06-28】责任要匹配得上对应的压力
    【2021-06-27】人生十三信条
  • 原文地址:https://www.cnblogs.com/oneboi/p/8266813.html
Copyright © 2011-2022 走看看