To add current date or time or timestamp in DB field automatically Using Rules in model use it like below : <?php/** * @return array validation rules for model attributes. */public function rules(){ return array( array(‘title’,’length’,’max’=>255), array(‘title, created, modified’, ‘required’), array(‘modified’,’default’, ‘value’=>new CDbExpression(‘NOW()’), ‘setOnEmpty’=>false,’on’=>’update’), array(‘created,modified’,’default’, ‘value’=>new CDbExpression(‘NOW()’), ‘setOnEmpty’=>false,’on’=>’insert’) );}?>
Category: framework
YII, How to get current controller name and action name
To get current controller name use this code : <?php$controllerId = Yii::app()->controller->id;//or$controllerId = $this->getId();?> To get current action name/id being executed, if you are inside beforeAction() or afterAction(), use the received CAction argument <?php//inside beforeAction or afterActionpublic function beforeAction($action){ $actionId = $action->id;…?> or just elsewhere inside your controller <?php$actionId = $this->getAction()->getId();?> To get name of… Continue reading YII, How to get current controller name and action name
Yii, return to previous url after login or logout
If you want to return to your previous url after login or logout try this : <?php$this->redirect(Yii::app()->request->urlReferrer);?> To set the return url to be the url that was before the login page or registeration page was called you can put following code in views/layouts/main.php file : <?php//this checks id the controller action is not ‘login’… Continue reading Yii, return to previous url after login or logout
yii, extension to use for image thumbnail generation
yii, extension to use for image thumbnail generation : http://www.yiiframework.com/extension/justintimeimageresizer/ And this extension requires this extension : http://www.yiiframework.com/extension/image/
YII, display model errors
Yii, display model errors : print_r($model->getErrors());
YII, get current page url
YII, get current page URL Yii::app()->request->requestUri
YII, get name of the called controller action
YII, get name of the called controller action: <?php print CController::getAction()->id ;?>
yii, form, create drop down with values from a model
yii, form, create drop down and populate it with values from any model : Use code below in the view file inside the form: <?php echo $form->dropDownList($model,’item_type_id’, CHtml::listData(ItemType::model()->findAll(), ‘id’, ‘type’), array(’empty’=>’select Type’)); ?>
yii, handling image uploads
In Model file function rules() : <?phparray(‘product_image_1, product_image_2, product_image_3’, ‘file’, ‘types’=>’jpg, gif, png’, ‘allowEmpty’=>true),?> In View file set form like this : <div class=”form”><?php $form=$this->beginWidget(‘CActiveForm’, array( ‘id’=>’products-form’, ‘enableAjaxValidation’=>false, ‘htmlOptions’=>array(‘enctype’ => ‘multipart/form-data’))); ?> <p class=”note”>Fields with <span class=”required”>*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class=”row”> <?php echo $form->labelEx($model,’product_name’); ?> <?php echo $form->textField($model,’product_name’,array(‘size’=>60,’maxlength’=>255)); ?> <?php echo… Continue reading yii, handling image uploads
YII, my sample model for search with relataionship
A model file for yii which has search fucntion which searches using relation ship: <?php/** * This is the model class for table “sf_category_slider”. * * The followings are the available columns in table ‘sf_category_slider’: * @property integer $rec_id * @property integer $cat_id * @property string $brand_id * @property string $slider_image * @property string $slider_link… Continue reading YII, my sample model for search with relataionship