Yii Framework Forum: Problem With Importing Csv File - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem With Importing Csv File Rate Topic: -----

#1 User is offline   Anthony Gough 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-October 12

Posted 30 October 2012 - 03:03 AM

RE:Importing CSV file to mysql table using "LOAD DATA" command. I cannot post links into this post as this is my first post - link is in file attached

I am having trouble implementing this. I created a file UserImportForm.php file with the form model code in the models directory. The code for the form I placed into importcsv.php into views\site. Finally I placed the function actionImportCSV into the SiteController. When I try to use the importcsv function index.php?r=user/importcsv
I get an error Alias "bootstrap.widgets.BootActiveForm" is invalid. Make sure it points to an existing directory or file.
I am fairly new to YII so I am sure I have done something wrong. I have followed suggestions such as autoloading extensions in the config\main.php file however I cannot get this to work. I have a requirement to load a csv file into a MySQL table and I could do this with a php script however I was trying to stick to YII conventions.
Any help appreciated

Attached File(s)

  • Attached File  link.txt (95bytes)
    Number of downloads: 26

0

#2 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 879
  • Joined: 20-April 11
  • Location:India

Posted 30 October 2012 - 04:03 AM

post your controller codes..

and if view rendering in user/importcsv, then that code too..
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   Anthony Gough 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-October 12

Posted 30 October 2012 - 06:59 AM

Codes are as per below

UserImportForm.php - In Models directory
<?php
class UserImportForm extends CFormModel
{
public $file;
/**
* @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('file', 'file',
'types'=>'csv',
'maxSize'=>1024 * 1024 * 10, // 10MB
'tooLarge'=>'The file was larger than 10MB. Please upload a smaller file.',
'allowEmpty' => false
),
);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'file' => 'Select file',
);
}

}
?>

UserController.php


<?php

class UserController 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', 'importcsv'),
'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('*'),
),
);
}
public function actionImportCSV()
{
$model=new UserImportForm;

if(isset($_POST['UserImportForm']))
{

$model->attributes=$_POST['UserImportForm'];

if($model->validate())
{

$csvFile=CUploadedFile::getInstance($model,'file');
$tempLoc=$csvFile->getTempName();

$sql="LOAD DATA LOCAL INFILE '".$tempLoc."'
INTO TABLE `tbl_user`
FIELDS TERMINATED BY ','
ENCLOSED BY '\"' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(`name`, `age`, `location`)";

$connection=Yii::app()->db;
$transaction=$connection->beginTransaction();
try
{

$connection->createCommand($sql)->execute();
$transaction->commit();
}
catch(Exception $e) // an exception is raised if a query fails
{
print_r($e);
exit;
$transaction->rollBack();

}
$this->redirect(array("user/index"));
}
}

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

/**
* 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 User;

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

if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$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['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$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)
{
$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'));
}

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

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

$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 the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=User::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}

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

importcsv.php in views\user


<div class="form">

<?php
$form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array(
'id'=>'service-form',
'enableAjaxValidation'=>false,
'method'=>'post',
'type'=>'horizontal',
'htmlOptions'=>array(
'enctype'=>'multipart/form-data'
)
)); ?>

<fieldset>
<legend>
<p class="note">Fields with <span class="required">*</span> are required.</p>
</legend>

<?php echo $form->errorSummary($model, 'Opps!!!', null, array('class'=>'alert alert-error span12')); ?>

<div class="control-group">
<div class="span4">
<div class="control-group <?php if ($model->hasErrors('postcode')) echo "error"; ?>">
<?php echo $form->labelEx($model,'file'); ?>
<?php echo $form->fileField($model,'file'); ?>
<?php echo $form->error($model,'file'); ?>
</div>


</div>
</div>

<div class="form-actions">
<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'submit', 'type'=>'primary', 'icon'=>'ok white', 'label'=>'UPLOAD')); ?>
<?php $this->widget('bootstrap.widgets.BootButton', array('buttonType'=>'reset', 'icon'=>'remove', 'label'=>'Reset')); ?>
</div>

</fieldset>

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

</div><!-- form -->
0

#4 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 879
  • Joined: 20-April 11
  • Location:India

Posted 30 October 2012 - 07:51 AM

the problem is with the path

'bootstrap.widgets.BootButton','bootstrap.widgets.BootActiveForm',
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#5 User is offline   Anthony Gough 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-October 12

Posted 30 October 2012 - 08:23 AM

I get what you mean now - path does not exist. Thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users