My problem is similar (or identical ?) to this one. However, the solution offered there does not help me.
I used Gii tot generate a model (Guest
) with a search model (GuestQuery
). Then I used Gii to generate CRUD for these models. Gii generates the code successfully.
Then, a call to /guest/index
results in the following error:
PHP Warning – yii\base\ErrorException
Invalid argument supplied for foreach()
1. in /home/fmivps/photosprint.app/kanematsu.app/vendor/yiisoft/yii2/BaseYii.php at line 520
511512513514515516517518519520521522523524525526527528529
/**
* Configures an object with the initial property values.
* @param object $object the object to be configured
* @param array $properties the property initial values given in terms of name-value pairs.
* @return object the object itself
*/
public static function configure($object, $properties)
{
foreach ($properties as $name => $value) {
$object->$name = $value;
}
return $object;
}
/**
* Returns the public member variables of an object.
* This method is provided such that we can get the public member variables of an object.
2. in /home/fmivps/photosprint.app/kanematsu.app/vendor/yiisoft/yii2/BaseYii.php at line 520 – yii\base\ErrorHandler::handleError(2, 'Invalid argument supplied for fo...', '/home/fmivps/photosprint.app/kan...', 520, ...)
3. in /home/fmivps/photosprint.app/kanematsu.app/vendor/yiisoft/yii2/base/Object.php at line 105 – yii\BaseYii::configure(app\models\generated\GuestQuery, 'app\models\generated\Guest')
4. in /home/fmivps/photosprint.app/kanematsu.app/models/generated/Guest.php at line 55 – yii\base\Object::__construct('app\models\generated\Guest')
495051525354555657
/**
* @inheritdoc
* @return GuestQuery the active query used by this AR class.
*/
public static function find()
{
return new GuestQuery(get_called_class());
}
}
5. in /home/fmivps/photosprint.app/kanematsu.app/models/generated/GuestQuery.php at line 44 – app\models\generated\Guest::find()
38394041424344454647484950
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Guest::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
6. in /home/fmivps/photosprint.app/kanematsu.app/controllers/GuestController.php at line 39 – app\models\generated\GuestQuery::search([])
33343536373839404142434445
* Lists all Guest models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new GuestQuery();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
7. app\controllers\GuestController::actionIndex()
It looks like php get_called_class()
returns a string 'app\models\generated\Guest'
That would be correct. Next, YiiBase wants to threat this string as an array. Which results in the above error.
How do I get this working?
I am running Yii 2.0.8 on php 7.0.6.
update: the models and controllers are as generated by Gii:
Guest.php
<?php
namespace app\models\generated;
use Yii;
class Guest extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'guest';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['created_at', 'remaining_prints'], 'integer'],
[['cookie_value'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'cookie_value' => Yii::t('app', 'Cookie Value'),
'created_at' => Yii::t('app', 'Created At'),
'remaining_prints' => Yii::t('app', 'Remaining Prints'),
];
}
/**
* @inheritdoc
* @return GuestQuery the active query used by this AR class.
*/
public static function find()
{
return new GuestQuery(get_called_class());
}
}
GuestQuery.php:
<?php
namespace app\models\generated;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\generated\Guest;
class GuestQuery extends Guest
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'created_at', 'remaining_prints'], 'integer'],
[['cookie_value'], '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 = Guest::find();
// add conditions that should always apply here
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'created_at' => $this->created_at,
'remaining_prints' => $this->remaining_prints,
]);
$query->andFilterWhere(['like', 'cookie_value', $this->cookie_value]);
return $dataProvider;
}
}
GuestController.php:
/**
* Lists all Guest models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new GuestQuery();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}