我是yii的新人。我需要使用yii 1.1
通过命令行将csv文件导入mysql数据库我有csv文件:
name, age, location,
sirin,23,Mannarkkad/India
abdul majid,23,Mannarkkad/India
boby,25,Heringen / Helme
这是我的行动:
<?php
class ImportCommand extends CConsoleCommand
{
public function actionImportCSV()
{
if (($handle = fopen(Yii::app()->baseUrl.'/test.csv', 'r')) !== false) {
// $i = 0;
while (($row = fgetcsv($handle, 1000, ",")) !== false) {
$model=TblUser::model()->findAll();
$model = new TblUser();
// $model->id = $row[0];
$model->name = $row[0];
$model->age = $row[1];
$model->location = $row[2];
if ($model->validate()) {
$model->save();
} else {
//... code If an error in the preservation
}
}
fclose($handle);
}
}
}
这是我的错误:
exception 'CException' with message 'Property "TblUser.name" is not defined.' in C:\xampp\htdocs\yii\framework\base\CComponent.php:183
Stack trace:
#0 C:\xampp\htdocs\yii\framework\db\ar\...
我的模特:
<?php
class TblUser extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_user';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name, age, location', 'required'),
array('age', 'numerical', 'integerOnly'=>true),
array('name, location', 'length', 'max'=>100),
array('name, age, location', 'safe', 'on'=>'search'),
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'name' => 'Name',
'age' => 'Age',
'location' => 'Location',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('age',$this->age);
$criteria->compare('location',$this->location,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return TblUser the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
请...提出任何解决此问题的想法?
谢谢!