doucuo4413 2015-11-14 14:32
浏览 75

未定义属性“tablename.title”

i am Yiibie. What i am trying to do is to upload image(photo), and path entry in database with update functionality. i have followed this link `http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-database-with-update-functionality/#hh0. I have followed everything of this link but getting an error which is "Property 'Ngo.title' is not defined" upon clicking submit button. Down is the code what i have been trying to do.

This is the code for the 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();
        }
    }
}

This is the code from view(_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(); ?>

and this is the code from 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条回答 默认 最新

  • douliaodun9153 2015-11-14 15:04
    关注

    had put one extra line. just remove one line from the model file of the related table array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度