Skip to main content

Posts

Showing posts with the label framework

Yii, how to add current date or time or timestamp in DB field automatically

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' ) ); } ?>

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 afterAction public function beforeAction ( $action ) { $actionId = $action -> id ; ... ?> or just elsewhere inside your controller <?php $actionId = $this -> getAction () -> getId (); ?> To get name of currently called controller action anywhere in the code you can use : <?php print CController :: getAction () -> id ; ?>

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' then it keeps the current url in returnUrl if (CController :: getAction () -> id != 'login' ) { Yii :: app () -> user -> setReturnUrl (Yii :: app () -> request -> getUrl ()); } ?>

yii, handling image uploads

In Model file function rules() : <?php array ( '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 , 'ma...

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 */ class CategorySlider extends CActiveRecord { /** * Returns the static model of the specified AR class. * @return CategorySlider the static model class */ public $category_name ; public static function model ( $className = __CLASS__) { return parent :: model ( $className ); } /** * @return string the associated database table name */ public function tableName () { return 'sf_category_slider' ; } /** * @return array validation rules for model attribute...

Yii, Ccaptcha. Captcha image doesnt show

Yii Ccaptcha Captcha refers to the imagery device ment to capture bots attempting to fill out your forms for automatic spam submission. It auto generates images with codes that users have to input to submit a form to avoid such spam bot issues. The only real forms that would require this device would be forms accessible to none users of your site, all other forms should be filtered by needing access to your site from a membership. Common Captcha Problems Version 1.1.x Captcha Image doesn't load on page? The first step is to see if the gd extension library is installed and functional for your php engine to read and display the image file. To do this, make a php file with the following code: phpinfo(); This will tell you all the specs of your php install. gd extension is there and working, now what? If the gd extension is operational then the error lies within your code. Let's go through this step by step: Did you define a public variable & rule set ...

yii, best way to call jquery, js and css files

Yii, Best pratice to include jquery, js and css files : <?php Yii :: app () -> clientScript -> registerCoreScript ( 'jquery' ); ?> <?php Yii :: app () -> clientScript -> registerCssFile (Yii :: app () -> baseUrl . '/css/style.css' ); ?> <?php Yii :: app () -> clientScript -> registerScriptFile (Yii :: app () -> baseUrl . '/js/star_rating.js' ); ?>

yii, some form validation examples

Yii some basic form validations using rules in models like email, compare password (match password), date, unique, captcha etc Below is full model code: <?php /** * This is the model class for table "users". * * The followings are the available columns in table 'users': * @property integer $user_id * @property string $user_name * @property string $user_password * @property string $user_email * @property string $user_gender * @property integer $user_age * @property string $user_location * @property string $user_avatar * @property string $user_facebook_id * @property integer $user_receive_newsletters_flag * @property integer $is_admin */ class Users extends CActiveRecord { public $confirm_password , $user_captcha ; /** * Returns the static model of the specified AR class. * @return Users the static model class */ public static function model ( $className = __CLASS__) { return parent :: model (...