R
R
Roman Savitsky2017-05-25 20:48:46
Yii
Roman Savitsky, 2017-05-25 20:48:46

Selection of posts through hasMany by passed label in url?

Good time !
In the url I pass the slug of the tag (from the slug I can get the id of the tag), by which I want to display all the posts attached to it
There is a "tags" label with tags, the table has such
id | name | slug
columns I don't understand how to register dependencies using hasMany
post_id | tag_id
to display my articles from the "statjiblog" table.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-05-25
@PRC

class Tags extends ActiveRecord
{
    public function getPages()
    {
         return $this->hasMany(Statjiblog::className(), ['id' => 'post_id'])->viaTable('{{%statjiblogtags}}', ['tag_id' => 'id']);
    }
}

public function actionPage($slug){
  $model = Tags::find()->andWhere(['slug'=>$slug])->one();
  foreach($model->pages as $page){
      echo $page->title;
  }
}

Well, or from the other side, through join;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question