3

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);

    ...
Community
  • 1
  • 1
Mr.Singh
  • 1,421
  • 6
  • 21
  • 46
  • just a minor question. Why you have property user $contact and $user in your comment,? and use backend\models\User whe you are alredy in namespace backend\models? – ScaisEdge Oct 01 '15 at 16:01
  • 2
    Can You show the error trace please? – ScaisEdge Oct 01 '15 at 16:01
  • 1
    Same problem? Above solution? What do you mean? – robsch Oct 02 '15 at 06:30
  • Sorry for the Same problem and Above solution. i have updated the question, please have a look. – Mr.Singh Oct 02 '15 at 13:21
  • in the model can u commment function find n then check ? – Bloodhound Oct 05 '15 at 06:53
  • Yes, i have tried that as you suggested and found that the problem is generating from this code `code` return new ContactsSearch(get_called_class()); `code` if i use this `code` return new ContactsSearch(); `code`, the code runs successfully. – Mr.Singh Oct 17 '15 at 14:04

0 Answers0