Answer the question
In order to leave comments, you need to log in
Yii2, relation, join. How right?
Hey!
I deal with relation, I need to get a list of all (rbac) rights of a specific role.
In sql it would look like this:
select c.name from vkl_auth_item_child t join vkl_auth_item c on t.child=c.name and c.type=2 and t.parent="FIRST2"
class AuthItemChild extends \yii\db\ActiveRecord
{ ... public function getChildItem()
{
return $this->hasOne(AuthItem::className(), ['name' => 'child']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParentItem()
{
return $this->hasOne(AuthItem::className(), ['name' => 'parent']);
}
}
$selectPermiss=AuthItemChild::find()->joinWith('childItem')
->where(AuthItem::tableName().'.type='.AuthItem::$permission.' and '.AuthItemChild::tableName().'.parent="'.$name.'"')
->asArray()->all();
Answer the question
In order to leave comments, you need to log in
class AuthItemChild extends \yii\db\ActiveRecord
{
}
class AuthItem extends \yii\db\ActiveRecord
{
public function getChildItems()
{
return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable(AuthItemChild::className(), ['parent'=>'name']);
}
}
$item = AuthItem::findOne(['name'=>'FIRST2']);
$childItems = $item->childItems();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question