2017-08-09 17 views
1

関連するモデルに対して安全な属性を設定する必要があります。私は、翻訳関係の検索モデルを使用します。そして、タイトル属性を安全に設定したいと思っています。Yii2の関連モデルの属性を安全に設定する

問題は、関連するArticleTranslationモデルでtitle属性が必要であることです。

私は、y2-translatableの動作をcreocoderから使用します。

class ArticleSearch extends Article 
{ 

public function rules() 
{ 
    return [ 
     [['id', 'user_id', 'status', 'category_id', 'created_at', 'updated_at'], 'integer'], 
     [['title', 'summary', 'content'], 'safe'], 
    ]; 
} 


/** 
* @inheritdoc 
*/ 
public function scenarios() 
{ 
    // bypass scenarios() implementation in the parent class 
    return Model::scenarios(); 
} 

/** 
* Creates data provider instance with search query applied 
* 
* @param array $params 
* 
* @return ActiveDataProvider 
*/ 
public function search($params) 
{ 
    // $this->detachBehaviors(); 
    $query = Article::find(); 
    $query->joinWith(['translations']); 
    // add conditions that should always apply here 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 



    if ($this->load($params) && !$this->validate()) { 
     // uncomment the following line if you do not want to return any records when validation fails 
     // $query->where('0=1'); 
     return $dataProvider; 
    } 

    // grid filtering conditions 
    $query->andFilterWhere([ 
     'id' => $this->id, 
     'user_id' => $this->user_id, 
     'status' => $this->status, 
     'category_id' => $this->category_id, 
     'created_at' => $this->created_at, 
     'updated_at' => $this->updated_at, 
    ]); 

    $query->andFilterWhere(['like', 'article_translation.title', $this->title]) 
     ->andFilterWhere(['like', 'article_translation.summary', $this->summary]) 
     ->andFilterWhere(['like', 'content', $this->content]); 

    return $dataProvider; 
} 

}

答えて

0

あなたが検索に参加し、モデル内の関係に基づいて、関連するモデルをtitle属性を使用したい場合は、検索モデルでの使用を許可するための適切なプロパティpublic varを宣言(この$を宣言する必要がありますタイトルも安全です)。例:

class ArticleSearch extends Article 
{ 

    public $title; 

    public function rules() 
    { 
     return [ 
      [['id', 'user_id', 'status', 'category_id', 'created_at', 'updated_at'], 'integer'], 
      [['title', 'summary', 'content'], 'safe'], 
     ]; 
    } 
    .... 
+0

ありがとうございます。それが仕事です。 –

+0

私の答えが正しければそれを受け入れられたものとしてマークしてください...こちらをどうぞ。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge

関連する問題