Yii2通过表从链接表进行排序和筛选

Yii2通过表从链接表进行排序和筛选,yii2,yii2-advanced-app,yii2-model,Yii2,Yii2 Advanced App,Yii2 Model,大家好。我试图为相关的phone_数字字段添加一个过滤器,但出现以下错误 结构- 1表-客户客户-姓名、赞助人、姓氏、年龄。 2表-客户端电话-客户端id,电话号码。 客户端(模型) 客户端搜索(模型) 我需要做些什么才能使这项工作顺利进行?我想你应该在加入后提交订单 检查注释行: public function search($params){ // do not put the join of a related table at the beginning because it's not

大家好。我试图为相关的phone_数字字段添加一个过滤器,但出现以下错误

结构-

1表-客户客户-姓名、赞助人、姓氏、年龄。
2表-客户端电话-客户端id,电话号码。

客户端(模型)

客户端搜索(模型)


我需要做些什么才能使这项工作顺利进行?

我想你应该在加入后提交订单 检查注释行:

public function search($params){
// do not put the join of a related table at the beginning because it's not related yet
    $query = Clientclient::find(); 
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        ]);
    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }
    $query->joinWith(['phonedigital' => function($query) { $query->from(['phonedigital' => 'ClientPhone']); }]);

// orderBy only after call the relacion
    $query->orderBy('phonedigital.phone_digital'); 

    $dataProvider->sort->attributes['phone'] = [
        'asc' => ['phonedigital.phone_digital' => SORT_ASC],
        'desc' => ['phonedigital.phone_digital' => SORT_DESC],
    ];
    $query->andFilterWhere([
        'id' => $this->id,
        'age' => $this->age,
    ]);
    $query->andFilterWhere(['like', 'first_name', $this->first_name])
        ->andFilterWhere(['like', 'patronymic', $this->patronymic])
        ->andFilterWhere(['like', 'last_name', $this->last_name])
        ->andFilterWhere(['like', 'phonedigital.phone_digital', $this->getAttribute('phonedigital')]);

    return $dataProvider;
}

我希望它对您有所帮助,但很抱歉我的单个堆栈跟踪不是它的照片,您的代码和问题中的所有内容都有问题,您说
phone\u digital
字段位于
ClientPhone
中,但
ClientPhone
未添加,我可以在
ClientClient
模型函数
attributeLabels()
中看到属性标签?如果它不是这个类的属性,那么我看到您在
ClientSearch
模型中定义了一个名为
$phonedigital
的公共属性,并将其添加到安全列表中,以及在
search()中
功能您正在尝试使用
电话\数字
对字段进行排序,而不使用
客户端电话
class ClientSearch extends Clientclient
{
public $phonedigital;
public function rules(){
    return [
        [['id', 'age'], 'integer'],
        [['first_name', 'phonedigital', 'patronymic', 'last_name'], 'safe'],
    ];
}
public function scenarios(){
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}
public function search($params){
    $query = Clientclient::find()->orderBy('phone_digital');
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        ]);
    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }
    $query->joinWith(['phonedigital' => function($query) { $query->from(['phonedigital' => 'ClientPhone']); }]);
    $dataProvider->sort->attributes['phone'] = [
        'asc' => ['phonedigital.phone_digital' => SORT_ASC],
        'desc' => ['phonedigital.phone_digital' => SORT_DESC],
    ];
    $query->andFilterWhere([
        'id' => $this->id,
        'age' => $this->age,
    ]);
    $query->andFilterWhere(['like', 'first_name', $this->first_name])
        ->andFilterWhere(['like', 'patronymic', $this->patronymic])
        ->andFilterWhere(['like', 'last_name', $this->last_name])
        ->andFilterWhere(['like', 'phonedigital.phone_digital', $this->getAttribute('phonedigital')]);

    return $dataProvider;
}
}
public function search($params){
// do not put the join of a related table at the beginning because it's not related yet
    $query = Clientclient::find(); 
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        ]);
    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }
    $query->joinWith(['phonedigital' => function($query) { $query->from(['phonedigital' => 'ClientPhone']); }]);

// orderBy only after call the relacion
    $query->orderBy('phonedigital.phone_digital'); 

    $dataProvider->sort->attributes['phone'] = [
        'asc' => ['phonedigital.phone_digital' => SORT_ASC],
        'desc' => ['phonedigital.phone_digital' => SORT_DESC],
    ];
    $query->andFilterWhere([
        'id' => $this->id,
        'age' => $this->age,
    ]);
    $query->andFilterWhere(['like', 'first_name', $this->first_name])
        ->andFilterWhere(['like', 'patronymic', $this->patronymic])
        ->andFilterWhere(['like', 'last_name', $this->last_name])
        ->andFilterWhere(['like', 'phonedigital.phone_digital', $this->getAttribute('phonedigital')]);

    return $dataProvider;
}