You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
Simple Yii Script for performing CRUD operations:
Greetings, This tutorial teach you the simple way to perform the crud operation in Yii framework, its easy to perform the database operation in Yii. Here we are going to see the create, update, delete and select scripts that required in almost all the applications.
Create ¶
**// to save all the paramerters of the post request**
$model = new Model;
$model->attributes=$_POST['form-name'];
$model->save();
**// to save parameters with changes before creation**
$model = new Model;
**// here name will concatenate both first and last names**
$model->name=$_POST['firstname'].$_POST['lastname'];
$model->attributes=$_POST['form-name'];
$model->save();
Update ¶
// to update the the whole parameters
$model = new Model;
// the form that already maps the id with value
$model->attributes=$_POST['form-name'];
$model->save();
// to update few parameters
**// Model::model()->updateAll(array($parameters),$conditions);**
Model::model()->updateAll(array('name' => $_POST['name']),'id="'.$POST['id'] );
Delete ¶
Model::model()->deleteAll(array('condition' => 'id=:io','params' => array('io' => $_POST['id'])));
select ¶
// in model
$criteria=new CDbCriteria;
$criteria->compare('status',$this->status,true);
$criteria->compare('name',$this->name,true);
**// if you want to select a few fields the use the following
// other wise ignore this you will get all the records**
$criteria->select='t.*,md.name';
return new CActiveDataProvider($this, array('criteria'=>$criteria));
// in controller
$model=new Currency('search');
$this->render('index',array('model'=>$model));
For more Related information regarding the active records Visit : http://www.bsourcecode.com/2013/06/cdbcriteria-in-yii/
Thanks.
WTF?
WTF?
this tutorials is old version. not worth to learn it.
this tutorials is old version. not worth to learn it.
Not a very good example ..
Thanks for your effort to share, though this is not a very good example.
Less knowledge..
are you trying this youself???
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.