A
A
AUN2015-04-15 17:08:39
Yii
AUN, 2015-04-15 17:08:39

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"

How to make it skull AR?
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']);
    }

}

I'm trying to make it through AR, it turns out shit like:
$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

1 answer(s)
V
vyachin, 2015-04-15
@vyachin

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 question

Ask a Question

731 491 924 answers to any question