Yii Crud开箱即用生成ErrorException

时间:2016-05-26 02:11:48

标签: php yii2 gii

我的问题与this one类似(或相同?)。但是,那里提供的解决方案对我没有帮助。

我使用Gii tot生成了一个带有搜索模型(Guest)的模型(GuestQuery)。然后我用Gii为这些模型生成CRUD。 Gii成功生成代码。

然后,对/guest/index的调用会导致以下错误:

 PHP Warning – yii\base\ErrorException

为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()

看起来像php get_called_class()返回一个字符串'app\models\generated\Guest'这是正确的。接下来,YiiBase想要将此字符串威胁为数组。这导致上述错误。

如何使其正常工作?

我在php 7.0.6上运行Yii 2.0.8。

更新:模型和控制器由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,
    ]);
}

0 个答案:

没有答案