Skip to content

Instantly share code, notes, and snippets.

@xicond
Created June 26, 2016 23:09
Show Gist options
  • Save xicond/2fa0b60ed80a6e2881f11fc5f76f17fd to your computer and use it in GitHub Desktop.
Save xicond/2fa0b60ed80a6e2881f11fc5f76f17fd to your computer and use it in GitHub Desktop.
<?php
namespace app\models;
use Yii;
use yii\db\Expression;
/**
* This is the model class for table "modelA".
*
*
*/
class Post extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'modelA';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'label'], 'safe'],
];
}
/**
* @return \yii\db\ActiveQuery
* Model A to Model B is One-to-Many
*/
public function getModelBs()
{
return $this->hasMany(ModelB::className(), ['modela_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
* Model B to Model C is Many-to-One, safely assume as One-to-one
*/
public function getModelCs()
{
return $this->hasMany(ModelC::className(), ['modelb_id' => 'modelb_id'])->viaTable('modelB', ['modela_id' => 'id'])->andOnCondition(['status'=>true]);
}
/**
* @return \yii\db\Query
* ModelC to ModelD is purely one-to-one
*/
public function getModelCD()
{
return static::find()->innerJoinWith('modelCs')->innerJoin('modelD','modelc.modelc_id=modeld.modelc_id')->select('modelc.*, modeld.*')->where(['modelA.id'=>$this->id])->all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment