H
H
hanran_koizoku2016-04-05 20:51:28
Yii
hanran_koizoku, 2016-04-05 20:51:28

Setting read-only property and Trying to get property of non-object Yii2 - what's wrong?

These two errors occur when trying to update a record and when trying to add a new one, respectively.
It arose due to the replacement of id from another table with the corresponding name (example: the movie table is passed id_genre there, but the name of the genre is displayed)
Model code:

<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "cinema".
 *
 * @property integer $id_cinema
 * @property string $Name
 * @property integer $dlitelnost
 * @property integer $Categori
 * @property integer $Price
 * @property integer $Prokatchiki
 *
 * @property Janr $categori
 * @property Dlitelnost $dlitelnost0
 * @property Prokatchiki $prokatchiki
 * @property Tickets[] $tickets
 */
class Cinema extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cinema';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_cinema', 'Name', 'dlitelnost', 'Categori', 'Price', 'Prokatchiki'], 'required'],
            [['id_cinema', 'dlitelnost', 'Categori', 'Price', 'Prokatchiki'], 'integer'],
            [['Name'], 'string', 'max' => 3000],
      [['CategoriName'], 'string','max'=>3000],
      [['TimeName'], 'safe'],
      [['CategoriName'], 'exist', 'skipOnError' => true, 'targetClass' => Janr::className(), 'targetAttribute' => ['CategoriName' => 'Name_janra']],
      [['TimeName'], 'exist', 'skipOnError' => true, 'targetClass' => Dlitelnost::className(), 'targetAttribute' => ['TimeName' => 'Date_premiere']],
            [['Categori'], 'exist', 'skipOnError' => true, 'targetClass' => Janr::className(), 'targetAttribute' => ['Categori' => 'Id_janra']],
            [['dlitelnost'], 'exist', 'skipOnError' => true, 'targetClass' => Dlitelnost::className(), 'targetAttribute' => ['dlitelnost' => 'id_time']],
            [['Prokatchiki'], 'exist', 'skipOnError' => true, 'targetClass' => Prokatchiki::className(), 'targetAttribute' => ['Prokatchiki' => 'Id_Prokatchika']],

      [['CategoriName'], 'exist', 'skipOnError' => true, 'targetClass' => Janr::className(), 'targetAttribute' => ['CategoriName' => 'Name_janra']],
      
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_cinema' => 'Id Cinema',
            'Name' => 'Name',
            'dlitelnost' => 'Dlitelnost',
            'Categori' => 'Categori',
            'Price' => 'Price',
            'Prokatchiki' => 'Prokatchiki',
      'TimeName'=>'Date Premiere',
      
       'CategoriName' => 'Janr',
        ];
    }

  public static function getGroupData()
    {
        $data = static::find()->select('id_cinema')->all();

        $value=(count($data)==0)? [''=>'']: \yii\helpers\ArrayHelper::map($data, 'cinema','Id Cinema'); //id = your ID model, name = your captio

        return $value;
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategori()
    {
        return $this->hasOne(janr::className(), ['Id_janra' => 'Categori']);
    }
  public function getCategoriName()
    {
        return $this->getCategori()->one()->Name_janra;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDlitelnost0()
    {
        return $this->hasOne(Dlitelnost::className(), ['id_time' => 'dlitelnost']);
    }
  public function getTimeName()
    {
        return $this->getDlitelnost()->one()->Date_premiere;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProkatchiki()
    {
        return $this->hasOne(Prokatchiki::className(), ['Id_Prokatchika' => 'Prokatchiki']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTickets()
    {
        return $this->hasMany(Tickets::className(), ['id_cinema' => 'id_cinema']);
    }
}

controller code:
<?php

namespace backend\controllers;

use Yii;
use common\models\Cinema;
use backend\models\CinemaSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * CinemaController implements the CRUD actions for Cinema model.
 */
class CinemaController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single Cinema model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Cinema model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Cinema();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_cinema]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }


    /**
     * Updates an existing Cinema model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_cinema]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Cinema model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Cinema model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Cinema the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Cinema::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}

serch code:
<?php

namespace backend\models;

use common\models\janr;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Cinema;

/**
 * CinemaSearch represents the model behind the search form about `common\models\Cinema`.
 */
class CinemaSearch extends Cinema
{
    /**
     * @inheritdoc
     */
   public $CategoriName;
    public function rules()
    {
        return [
            [['id_cinema', 'dlitelnost','Categori', 'Price', 'Prokatchiki'], 'integer'],
            [['Name'], 'safe'],
      [['CategoriName'],'string'],
        ];
    }

    /**
     * @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 = Cinema::find();

        // add conditions that should always apply here

    $myQuery = janr::find()->select(['Id_janra','Name_janra'])->groupBy('Id_janra');
        $query->leftJoin(['CategoriName' => $myQuery], 'CategoriName.Id_janra = Categori');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
    
     $dataProvider->setSort([
            'attributes' => [
                'id_cinema',
                'Name',
                'Categori',
                'CategoriName' => [
                    'asc' => ['CategoriName.Name_janra' => SORT_ASC],
                    'desc' => ['CategoriName.Name_janra' => SORT_DESC],
                    'label' => 'CATEGORI',
                    'default' => SORT_ASC,
                ],
            ]
        ]);

        $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_cinema' => $this->id_cinema,
            'Categori' => $this->Categori,
        ])->andFilterWhere(['like', 'CategoriName.Name_janra', $this->CategoriName]);
        $query->andFilterWhere([
            'dlitelnost' => $this->dlitelnost,
            'Price' => $this->Price,
            'Prokatchiki' => $this->Prokatchiki,
        ]);

        $query->andFilterWhere(['like', 'Name', $this->Name]);

        return $dataProvider;
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
mitaichik, 2016-04-06
@mitaichik

There is at least one flaw in the last file - the method of a distant ancestor is called in the override of scripts, perhaps this fails. In the first file, there are several getters without setters - that's the read-only property for you. What file is the error? Post the stacktrace.
And what the hell is that naming? Where is Russian, where is English, where with a capital letter, where with a small letter, where there is a shellcase, where there is an underscore. Well, how is it possible!

S
sokollondon, 2020-05-18
@sokollondon

In most cases, you don't need to set a relation. But if you still need it:

php composer.phar require la-haute-societe/yii2-save-relations-behavior "*"

Model
class Post extends yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'saveRelations' => [
                'class' => SaveRelationsBehavior::class,
                'relations' => [
                    'author',
                ],
            ],
        ];
    }
}

Now this code will not cause errors Setting read-only property=) Also, the yii2-save-relations-behavior module will help you save hasMany relations easier
$post->author = Author::findOne(2);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question