未定义属性“tablename.title”

时间:2015-11-14 14:32:08

标签: php yii

我是伊比。我想要做的是上传图像(照片)和数据库中的路径条目与更新功能。我已经关注了这个链接`http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-database-with-update-functionality/#hh0。我已经按照这个链接的所有内容但是在点击提交按钮时收到“Property'Ngo.title'未定义”的错误。 Down是我一直试图做的代码。

这是NgoController的代码

<?php

class NgoController extends Controller
{
    /**
    * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
    * using two-column layout. See 'protected/views/layouts/column2.php'.
    */
    public $layout='//layouts/column2';

    /**
    * @return array action filters
    */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
    * Specifies the access control rules.
    * This method is used by the 'accessControl' filter.
    * @return array access control rules
    */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update'),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('admin','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

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

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Ngo']))
        {
                    $rnd = rand(0,9999);  // generate random number between 0-9999
            $model->attributes=$_POST['Ngo'];
                        $uploadedFile=CUploadedFile::getInstance($model,'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            $model->image = $fileName;
            if($model->save())
                             {
                $uploadedFile->saveAs(Yii::app()->basePath.'/../ngo/'.$fileName);  // image will uplode to rootDirectory/ngo/
                $this->redirect(array('admin'));
            }
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
        'model'=>$model,
        ));
    }

    /**
    * Updates a particular model.
    * If update is successful, the browser will be redirected to the 'view' page.
    * @param integer $id the ID of the model to be updated
    */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Ngo']))
        {
                    $_POST['Ngo']['image'] = $model->image;
            $model->attributes=$_POST['Ngo'];
                        $uploadedFile=CUploadedFile::getInstance($model,'image');

            if($model->save())
                            {
                if(!empty($uploadedFile))  // check if uploaded file is set or not
                {
                    $uploadedFile->saveAs(Yii::app()->basePath.'/../ngo/'.$model->image);
                }
                $this->redirect(array('admin'));
            }
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

    /**
    * Deletes a particular model.
    * If deletion is successful, the browser will be redirected to the 'admin' page.
    * @param integer $id the ID of the model to be deleted
    */
    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    /**
    * Lists all models.
    */
    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('Ngo');
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new Ngo('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Ngo']))
            $model->attributes=$_GET['Ngo'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Returns the data model based on the primary key given in the GET variable.
    * If the data model is not found, an HTTP exception will be raised.
    * @param integer $id the ID of the model to be loaded
    * @return Ngo the loaded model
    * @throws CHttpException
    */
    public function loadModel($id)
    {
        $model=Ngo::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    /**
    * Performs the AJAX validation.
    * @param Ngo $model the model to be validated
    */
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='ngo-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}

这是来自视图的代码(_form.php)

<?php
/* @var $this NgoController */
/* @var $model Ngo */
/* @var $form BSActiveForm */
?>

<?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
    'id'=>'ngo-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
    'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),
)); ?>

    <p class="help-block">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <?php echo $form->textFieldControlGroup($model,'id'); ?>
    <?php echo $form->textFieldControlGroup($model,'Ngo_name',array('maxlength'=>45)); ?>
    <?php echo $form->textFieldControlGroup($model,'username',array('maxlength'=>45)); ?>
    <?php echo $form->passwordFieldControlGroup($model,'password',array('maxlength'=>45)); ?>
    <?php echo $form->textFieldControlGroup($model,'email',array('maxlength'=>45)); ?>
    <?php echo $form->textFieldControlGroup($model,'address',array('maxlength'=>45)); ?>
    <?php echo $form->textFieldControlGroup($model,'upload_doc',array('maxlength'=>45)); ?>
    <?php echo $form->textFieldControlGroup($model,'image',array('maxlength'=>45)); ?>
<div class="row">
        <?php echo $form->labelEx($model,'image'); ?>
        <?php echo CHtml::activeFileField($model, 'image'); ?>  <!--by this we can upload image-->
        <?php echo $form->error($model,'image'); ?>
</div>
    <?php if($model->isNewRecord!='1') ?>
    <div class="row">
     <?php echo CHtml::image(Yii::app()->request->baseUrl.'/ngo/'.$model->image,"image",array("width"=>200)); ?>  <!--Image shown here if page is update page-->
</div>
    <br>
    <?php echo BsHtml::submitButton('Submit', array('color' => BsHtml::BUTTON_COLOR_PRIMARY)); ?>

<?php $this->endWidget(); ?>

这是来自models(ngo.php)的代码

<?php

/**
 * This is the model class for table "ngo".
 *
 * The followings are the available columns in table 'ngo':
 * @property integer $id
 * @property string $Ngo_name
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $address
 * @property string $upload_doc
 * @property string $image
 *
 * The followings are the available model relations:
 * @property UserRateReviewNgo[] $userRateReviewNgos
 */
class Ngo extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Ngo the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'ngo';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id, Ngo_name, username, password, email, address, upload_doc, image', 'required'),
            array('id', 'numerical', 'integerOnly'=>true),
            array('Ngo_name, username, password, email, address, upload_doc, image', 'length', 'max'=>45),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, Ngo_name, username, password, email, address, upload_doc, image', 'safe', 'on'=>'search'),
        array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
                    array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),
                    );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'userRateReviewNgos' => array(self::HAS_MANY, 'UserRateReviewNgo', 'Ngo_Ngo_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'Ngo_name' => 'Ngo Name',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'address' => 'Address',
            'upload_doc' => 'Upload Doc',
            'image' => 'Image',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('Ngo_name',$this->Ngo_name,true);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('upload_doc',$this->upload_doc,true);
        $criteria->compare('image',$this->image,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

}

1 个答案:

答案 0 :(得分:0)

已经增加了一条线。 只需从相关表的模型文件中删除一行 数组(&#39;标题,图片&#39;,&#39;长度&#39;,&#39; max&#39; =&gt; 255,&#39; on&#39; =&gt;&#39;插入,更新&#39),