zoukankan      html  css  js  c++  java
  • Yii ActiveRecord with()用法

    WorksModel

    use yiidbActiveRecord;
    
    class WorksModel extends ActiveRecord
    {
        public static function tableName()
        {
            return 'vote_works';
        }
    
        public function getFiles()
        {
            return $this->hasMany(WorksFileModel::class, ['works_id' => 'works_id']);
        }
    }

    WorksFileModel

    use yiidbActiveRecord;
    
    class WorksFileModel extends ActiveRecord {
    
    	public static function tableName() {
    		return 'vote_works_file';
    	}
    
        public function getWorks()
        {
            return $this->hasOne(WorksModel::class, ['works_id' => 'works_id']);
    	}
    
    }
    

      

    控制器

    $obj = WorksModel::find();
            $files = $obj->with('files')->asArray()->all();
            print_r($files);
    

      

    数据集

    Array
    (
        [0] => Array
            (
                [works_id] => 1
                [name] => 第一次投稿
                [add_time] => 2021-07-01 15:47:46
                [update_time] => 2021-07-03 11:02:08
                [files] => Array
                    (
                        [0] => Array
                            (
                                [file_id] => 1
                                [works_id] => 1
                                [add_time] => 2021-07-01 15:26:49
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [works_id] => 2
                [name] => 第二次投稿
                [add_time] => 2021-07-01 15:47:46
                [update_time] => 2021-07-01 15:47:49
                [files] => Array
                    (
                        [0] => Array
                            (
                                [file_id] => 2
                                [works_id] => 2
                                [add_time] => 2021-07-01 15:26:49
                            )
    
                    )
    
            )
    
    )
    

      

      

  • 相关阅读:
    Oracle函数题
    多态
    继承
    封装
    属性和修饰符
    方法重载
    构造方法
    类与实例
    SQL Server 2005 To Oracle
    Toad for Oracle 使用文档
  • 原文地址:https://www.cnblogs.com/xiangdongsheng/p/14970431.html
Copyright © 2011-2022 走看看