在一个非直接相关的表中得到一个字段iii2

时间:2016-12-17 20:28:33

标签: php yii2

我的表格有这样的关系:My Relation

如果你看到,我现在遇到了问题, 我在deal_komisi的表中工作。 但在我的searchModel中,我希望我的用户可以在表格中进行搜索: item_Layanan.nama_item

这是我的模特

class DealKomisi extends \yii\db\ActiveRecord {
        ...... //Common code

 /* Related to list_harga */
 /**
 * @return \yii\db\ActiveQuery
 */
public function getListHarga() {
    return $this->hasOne(ListHarga::className(), ['id' => 'list_harga_id']);
}

所以,在我的DealKomisiSearch中,我只是使用它:

public function search($params) {
    $query = DealKomisi::find();

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

    $this->load($params);

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

    $query->andFilterWhere([
        'id' => $this->id,
        'list_harga_id' => $this->list_harga_id,
        'komisi' => $this->komisi,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

    $query->joinWith('listHarga');

    $query->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by])
    ;

    return $dataProvider;
}

我应该怎么做, P.S 我还创建了 list_harga 模型,这与** item_layanan **的关系是这样的。

class ListHarga extends \yii\db\ActiveRecord


 public function getItemLayanan()
{
    return $this->hasOne(ItemLayanan::className(), ['id' => 'item_layanan_id']);
}

请指教。 任何帮助它如此赞赏

0 个答案:

没有答案