{"id":94,"date":"2014-02-02T20:45:57","date_gmt":"2014-02-03T04:45:57","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=94"},"modified":"2014-03-20T02:04:07","modified_gmt":"2014-03-20T10:04:07","slug":"yii-built-in-functions-generic","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=94","title":{"rendered":"Yii Built In functions &#8211; Generic"},"content":{"rendered":"<p><code>\/\/ For dropdown list<br \/>\n$list=CHtml::listData(SysState::model()->findAll(), 'state_id', 'state_name');<br \/>\necho $form->dropDownList($model,'state_id', $list, array('empty'=>'Select state'));<\/p>\n<p>\/\/ dynamic dropdown populate<br \/>\nBelow lines create the country dropdown which updates state dropdown<br \/>\n<?php $ajaxStatePopulate = array(\n 'type' => 'POST', \/\/request type<br \/>\n 'url' => Yii::app()->createUrl('manager\/events\/dynamicStates\/'), \/\/url to call.<br \/>\n \/\/Style: ii::app()->createUrl('currentController\/methodToCall')<br \/>\n 'update' => '#Events_state_id', \/\/selector to update<br \/>\n 'data'=> array('country_code'=>'js:this.value')<br \/>\n \/\/leave out the data key to pass all form values through<br \/>\n )<br \/>\n?><br \/>\n<?php echo $form->dropDownList($model,'country_code', $list['countries'], array('empty'=>'--none--', 'class'=>'form-control', 'ajax'=>$ajaxStatePopulate)) ?><br \/>\n<?php echo $form->error( $model, 'country_code'); ?> <\/p>\n<p>The above code updates the below line<br \/>\n<?php echo $form->dropDownList($model,'state_id', $list['states'], array('empty'=>'--none--', 'class'=>'form-control')) ?><br \/>\n<?php echo $form->error( $model, 'state_id'); ?> <\/p>\n<p>Called url will have the below code<br \/>\n$result = States::model->findAll();<br \/>\n$data = CHtml::listData($result,'id','name');<br \/>\n\/\/return CHtml::listData($result, 'code', 'name');print_r($data);<br \/>\necho CHtml::tag('option',<br \/>\narray('value'=>''),CHtml::encode('--none--'),true);<br \/>\nforeach($data as $value=>$name)<br \/>\n{<br \/>\n echo CHtml::tag('option',<br \/>\n array('value'=>$value),CHtml::encode($name),true);<br \/>\n}<\/p>\n<p>\/\/ NEXT<br \/>\n\/\/ To update the of the value in text field<br \/>\n<?php $model->pre_reg_start_date = Common::formatDbDate($model->pre_reg_start_date)?><br \/>\n<?php echo $form->textField( $model, 'pre_reg_start_date', array('class'=>'form-control selectDate')); ?><\/p>\n<p>\/\/ NEXT : update label<br \/>\n<?php echo $form->labelEx( $model, 'pre_reg_start_time', array('label'=>'Time') ); ?><\/p>\n<p>\/\/ NEXT : create link with id<br \/>\n<a href=\"<?php echo Yii::app()->createUrl('manager\/events\/update', array(\"id\"=>$event_id))?>\" class=\"btn btn-primary\">Link<\/a><\/p>\n<p>\/\/ NEXT : Set your messages in a controller:<br \/>\nYii::app()->user->setFlash('success', \"Data1 saved!\");<br \/>\nYii::app()->user->setFlash('error', \"Data2 failed!\");<br \/>\nYii::app()->user->setFlash('notice', \"Data3 ignored.\");<\/p>\n<p>Display them in your view:<br \/>\n<?php\n    foreach(Yii::app()->user->getFlashes() as $key => $message) {<br \/>\n        echo '<\/p>\n<div class=\"flash-' . $key . '\">' . $message . \"<\/div>\n<p>\\n\";<br \/>\n    }<br \/>\n?><\/p>\n<p>\/\/ NEXT : Create url examples<br \/>\n$url=$this->createUrl($route,$params); \/\/ syntax<\/p>\n<p>$url = Yii::app()->createUrl('site\/index');<br \/>\nindex.php?r=site\/index<\/p>\n<p>$url = Yii::app()->createUrl('site\/index', array('id'=>100));<br \/>\nindex.php?r=site\/index&id=100<\/p>\n<p>Create URL from Controller<br \/>\nYii::app()->controller->createUrl(\"index\", array(\"id\"=>100));<br \/>\n index.php?r=site\/index&id=100<\/p>\n<p>create url withing same controller with id as parameter<br \/>\n$this->createUrl('index',array('id'=>100));<\/p>\n<p>Create absolute URL:<br \/>\nIn order to create an absolute path url you need to use createAbsoluteUrl() function:<br \/>\nYii::app()->createAbsoluteUrl('site\/index',array('id'=>100));<br \/>\nhttp:\/\/yourdomain.com\/index.php?r=site\/index&id=100<\/p>\n<p>echo CHtml::link('text', array('site\/index', 'id'=>100));<br \/>\n<a href=\u201d\/ABC\/index.php?r=site\/index&amp;id=100?>text<\/a><\/p>\n<p>Redirection example<br \/>\n$this->redirect(array('site\/index','id'=>100));<br \/>\nindex.php?r=site\/index&id=100<\/p>\n<p>$this->redirect(array('site\/index','id'=>100));<\/p>\n<p>\/\/ Get base url<br \/>\nYii::app()->baseUrl<\/p>\n<p>\/\/ get current url before ? mark<br \/>\nYii::app()->request->pathInfo<\/p>\n<p>\/\/ Theme url<br \/>\nYii::app()->theme->baseUrl<\/p>\n<p>\/\/ get the theme name<br \/>\nYii::app()->theme->name<\/p>\n<p>\/\/ get base path<br \/>\n$this->getBasePath()<\/p>\n<p>\/\/ home page url<br \/>\n$this->getHomeUrl()<\/p>\n<p>\/\/ url related properties<br \/>\nYii::app()->request->pathInfo<br \/>\nYii::app()->request->url<br \/>\nYii::app()->request->requestUri<br \/>\nYii::app()->request->queryString<br \/>\nYii::app()->request->isSecureConnection<br \/>\nYii::app()->request->isPostRequest<br \/>\nYii::app()->request->isAjaxRequest<br \/>\nYii::app()->request->serverName<br \/>\nYii::app()->request->urlReferrer<br \/>\nYii::app()->request->userHostAddress \/\/ IP address<\/p>\n<p>\/\/ NEXT : rules example to validate the date and time<br \/>\narray('wave_date', 'type', 'type'=>'date', 'dateFormat'=>'MM\/dd\/yyyy'),<br \/>\narray('wave_time', 'type', 'type'=>'time', 'timeFormat'=>'hh:mm a'),<\/p>\n<p>Text field<br \/>\n<?php echo CHtml::activeTextField($model,$attribute,$htmlOptions); ?><br \/>\nExample:<br \/>\n<?php echo CHtml::activeTextField($model,'name',array('class'=>'form-control')); ?><\/p>\n<p>Text field using Form. It does same as above<br \/>\n<?php echo $form->textField( $model, 'name', array('class'=>'form-control')); ?><\/p>\n<p>Text field without form and model<br \/>\n<?php echo CHtml::textField($name,'value'); ?><\/p>\n<p>Creating a simple widget<br \/>\nStep 1: components\/BreadCrumb.php:<br \/>\n<?php\nclass BreadCrumb extends CWidget {\n \n    public $crumbs = array();\n    public $delimiter = ' \/ ';\n \n    public function run() {\n        $this->render('breadCrumb');<br \/>\n    }<\/p>\n<p>}<br \/>\n?><br \/>\nStep 2: components\/views\/breadCrumb.php <\/p>\n<div id=\"breadCrumb\">\n    <?php \n    foreach($this->crumbs as $crumb) {<br \/>\n        if(isset($crumb['url'])) {<br \/>\n            echo CHtml::link($crumb['name'], $crumb['url']);<br \/>\n        } else {<br \/>\n            echo $crumb['name'];<br \/>\n        }<br \/>\n        if(next($this->crumbs)) {<br \/>\n            echo $this->delimiter;<br \/>\n        }<br \/>\n    }<br \/>\n    ?>\n<\/div>\n<p>Step 3: Call from your actuall view as below<br \/>\n<?php $this->widget('application.components.BreadCrumb', array(<br \/>\n  'crumbs' => array(<br \/>\n    array('name' => 'Home', 'url' => array('site\/index')),<br \/>\n    array('name' => 'Login'),<br \/>\n  ),<br \/>\n  'delimiter' => ' &rarr; ', \/\/ if you want to change it<br \/>\n)); ?><\/p>\n<p>\/\/ NEXT : Logging<br \/>\nYii::log($message, $level, $category);<br \/>\nYii::trace($message, $category);<\/p>\n<p>\/\/ NEXT : end application<br \/>\nYii::app()->end(); \/\/ does<\/p>\n<p>\/\/ Set get session value<br \/>\nYii::app()->session[\"lang\"] = \"English\";<br \/>\necho Yii::app()->session[\"lang\"]<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ For dropdown list $list=CHtml::listData(SysState::model()->findAll(), &#8216;state_id&#8217;, &#8216;state_name&#8217;); echo $form->dropDownList($model,&#8217;state_id&#8217;, $list, array(&#8217;empty&#8217;=>&#8217;Select state&#8217;)); \/\/ dynamic dropdown populate Below lines create the country dropdown which updates state dropdown The above code updates the below line Called url will have the below code $result = States::model->findAll(); $data = CHtml::listData($result,&#8217;id&#8217;,&#8217;name&#8217;); \/\/return CHtml::listData($result, &#8216;code&#8217;, &#8216;name&#8217;);print_r($data); echo CHtml::tag(&#8216;option&#8217;, array(&#8216;value&#8217;=>&#8221;),CHtml::encode(&#8216;&#8211;none&#8211;&#8216;),true); foreach($data as &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=94\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Yii Built In functions &#8211; Generic<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":2,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":120,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/120"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}