doujian4752 2015-10-01 12:57
浏览 101

Yii2搜索模型,为foreach提供的参数无效

I am new at Yii framework and I am facing this problem that when i am creating search model to add filters and sorting, it's not working, i already have tried this solution Yii2 ActiveDataProvider - Invalid argument supplied for foreach() and even generated the new models and controller but the result is same, it did not work.

Please have a look at the code and kindly tell me what i am doing wrong.

Controller

namespace backend\controllers;

use Yii;
use backend\models\Contacts;
use backend\models\ContactsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ContactsController implements the CRUD actions for Contacts model.
 */
class ContactsController extends Controller
{
    ...

    /**
     * Lists all Contacts models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ContactsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);
    }

    ...
}

Model

namespace backend\models;

use Yii;
use backend\models\User;

/**
 * This is the model class for table "user_contacts".
 *
 * @property string $id
 * @property string $user_id
 * @property string $contact_id
 * @property string $created_on
 * @property string $modified_on
 *
 * @property User $contact
 * @property User $user
 */
class Contacts extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_contacts';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'contact_id'], 'integer'],
            [['created_on', 'modified_on'], 'safe'],
            [['user_id', 'contact_id'], 'unique', 'targetAttribute' => ['user_id', 'contact_id'], 'message' => 'The combination of User ID and Contact ID has already been taken.']
        ];
    }

    /**
    * @inheritdoc
    */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'contact_id' => Yii::t('app', 'Contact ID'),
            'created_on' => Yii::t('app', 'Created On'),
            'modified_on' => Yii::t('app', 'Modified On')
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getContact()
    {
        return $this->hasOne(User::className(), ['id' => 'contact_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @inheritdoc
     * @return ContactsSearch the active query used by this AR class.
     */
    public static function find()
    {
        return new ContactsSearch(get_called_class());
    }
}

Search model

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Contacts;

/**
 * ContactsSearch represents the model behind the search form about `backend\models\Contacts`.
 */
class ContactsSearch extends Contacts
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'contact_id'], 'integer'],
            [['created_on', 'modified_on'], '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)
    {
        $query = Contacts::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,
            'user_id' => $this->user_id,
            'contact_id' => $this->contact_id,
            'created_on' => $this->created_on,
            'modified_on' => $this->modified_on
        ]);

        return $dataProvider;
    }
}

View(index.php)

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'user_id',
        'contact_id',
        'created_on',
        'modified_on',

        ['class' => 'yii\grid\ActionColumn']
    ]
]);

Error

PHP Warning – yii\base\ErrorException
Invalid argument supplied for foreach()

1. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\vendor\yiisoft\yii2\BaseYii.php at line 517

public static function configure($object, $properties)
{
    foreach ($properties as $name => $value) {
        $object->$name = $value;
    }

    return $object;
}


2. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\backend\models\Contacts.php at line 76 – yii\base\Object::__construct()

public static function find()
{
    return new ContactsSearch(get_called_class());
}



3. in F:\Files\Web\PHP\wamp\www\Php\sites\expenses\backend\models\ContactsSearch.php at line 43 – backend\models\Contacts::find()

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

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

    $this->load($params);

    ...
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况