WorksModel
use yii\db\ActiveRecord; 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 yii\db\ActiveRecord; 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 ) ) ) )