Skip to main content

Posts

Showing posts from January, 2012

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, 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() : <?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

mysql, import sql file from command line

Method 1: A quick way to duplicate MySQL databases is to do the following: Step 1 : Dump the database either through PHPMyAdmin’s export form or through mysqldump (as I mentioned above). Note (usually, it’s mysqldump <database-name> -h<hostname> -u<username> -p<password> if you ommit the -h switch it will default to “localhost”). After you enter the command below and press enter, you will be prompted for a password because you did not enter the password after the -p switch. $ mysqldump <database-name> -u<username> -p >> somedatabasetable.sql Step 2:  Once you have the sql file, log into mysql and create a database or “use” an existing database – again, ommiting the -h will default to localhost and you will be prompted for a password if you don’t enter a password after the -p switch. $ mysql -u<username> -p Step 3:  After logging in, you will need to either create a new database and select the database for use with the i