Yii2:试图获得非对象的属性

时间:2016-04-17 20:15:02

标签: php yii yii2

public function actionCreate()
    {
        $model          = new Bookings();
        $temp           = new RoomTypes();
        if ($model->load(Yii::$app->request->post())) 
        {
            $roomtype = $model->room_type;
            $totalremain = RoomTypes::find('total_remain')->where(['room_type' => $model->room_type])->one();
            if ($roomtype->$totalremain > 0) 
            {
                    $imageName   = $model->first_name;
                    $mobile      = $model->primary_mobile;
                    $model->file = UploadedFile::getInstance($model, 'file');
                    $model->file->saveAs('uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension);
                    //save the path in the db column
                    $model->id_image = 'uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension;
                    $model->save();   
                    return $this->redirect(['view', 'id' => $model->id]);
            } 
                else 
                {
                    echo "This room Types are full ";
                }
        }

        else {
            return $this->render('create', [
                'model' => $model,
                'temp'  => $temp,
            ]);
        }
    }

我需要检查来自Roomtypes模型的total_remain是>在预订模型中来自room_types的0,在提交表单之前,如果用户提交表单,则应该获得闪烁指示“此房间已满” 得到此错误,如何解决此问题

if ($roomtype->$totalremain > 0)

处收到错误

房间类型模型

<?php

namespace backend\models;

use Yii;


/**
 * This is the model class for table "room_types".
 *
 * @property integer $id
 * @property integer $room_id
 * @property string $room_type
 * @property integer $total_count
 * @property string $description
 * @property integer $extra_beds
 * @property string $images
 * @property string $status
 * @property integer $rate
 * @property integer $adults_count
 * @property integer $child_count
 * @property integer $total_people
 */
class RoomTypes extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */

    public $imageFiles;

    public static function tableName()
    {
        return 'room_types';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['room_id','total_booked','total_remain','total_count', 'extra_beds', 'rate', 'adults_count', 'child_count'], 'integer'],
            [['status'], 'string'],
            [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4,'skipOnEmpty' => true, 'on' => 'update-photo-upload'],
            [['room_type'], 'string', 'max' => 40],
            [['description'], 'string', 'max' => 300],
            [['images'], 'string', 'max' => 500]
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'room_id' => 'Room Number',
            'room_type' => 'Room Type',
            'total_count' => 'Total Count',
            'description' => 'Description',
            'extra_beds' => 'Extra Beds',
            //'images' => 'Images',
            'status' => 'Status',
            'rate' => 'Rate',
            'adults_count' => 'Adults Count',
            'child_count' => 'Child Count',
            'total_remain' =>'Remaing Rooms',
            'total_booked' => 'Total Rooms Booked',
        ];
    }
}

1 个答案:

答案 0 :(得分:1)

代码if($ roomtype-&gt; $ totalremain&gt; 0)导致问题。如果($ totalremain-&gt; total_remain&gt; 0)更改它