Yii

Calling controller from a View in Yii

There are two ways to call a controller from a view.

$this->forward('/enggdiary/optimiseroutebygoogleapi',true, array('engineer_id'=>$isAdmin));?>

OR

 

if(($ca=Yii::app()->createController('enggdiary'))!==null){
 list($controller,$actionID)=$ca;
 $controller->init();
 $controller->renderPartial('optimiseroutebygoogleapi',array(
 'engineer_id'=>$model->engineer_id,
 'route_date_string'=>$route_date_string,
 'route_date_time'=>$route_date_time,
 'route_map_results'=>$route_map_results,

'planroute'=>'0',

));
 }

Yii assets folder Javascript import

There are two ways in Yii to import the javascript file

$oow_url =Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.oow.assets'));

Yii::app()->clientScript->registerScriptFile($oow_url.'\js\oow.js') ;

Yii::app()->getClientScript()->registerScriptFile($oow_url.'/js/oow.js', CClientScript::POS_END);

I was having following line in my javascript file
document.getElementById("Product_serial_number").value='75787';

When I was using


Yii::app()->clientScript->registerScriptFile($oow_url.'\js\oow.js')
;

I was getting error as Uncaught Object Type

However

When I tried

Yii::app()->getClientScript()->registerScriptFile($oow_url.'/js/oow.js', CClientScript::POS_END);

It worked fine.

I guess the best way to import javascript file in Yii from assets folder is


Yii::app()->getClientScript()->registerScriptFile($oow_url.'/js/oow.js', CClientScript::POS_END);

Yii Create Module using CRUD step by Step

Firstly create a module using Gii
screenshot_76I think this might be helpful how to create CRUD for modules in Yii.How to use Yii framework in modules

Create a table in database
"CREATE TABLE oow(id INTEGER PRIMARY KEY NOT NULL, serial_number TEXT, model_number TEXT, mdel_range TEXT, notes TEXT, created DATETIME ,modified DATETIME, createdby INTEGER, modifiedy INTEGER )"

Next Create Model for your table in moudule you just created. Make sure to change the model path to application.modules.YOURMODULENAME.models and click on Generate
screenshot_78

Last thing is to create the CRUD
Make sure you give model class name as
application.modules.YOURMODULENAME.models.MODELCLASSNAMEYOUJUSTCREATED
else it will give error as model not found while creating CRUD

screenshot_79

You can now run your moudule as
http://localhost/chs/chs/index.php?r=oow/search

If you are using SEO friendly URLs you probably need to change code as per your main.php in config folder

I hope this will help you

Thanks

Yii Ajax Search in Database – Search As You Type

http://www.yiiframework.com/extension/database-live-search/

Yii is fantastic framework for php as it provides lots of user friendly functions through ajax. However, I was searching for a method so that I could search in database as we type- something what google is doing these days.

With some hard work, I am able to write the script that will search into your Yii Database as you type. I had written this script earlier in PHP, but was a bit challenging with Yii as there were different database calling procedures and functions. However, this is how we go:

This is a free search extension to search in database. This extension produces the results as we type and the search narrows down as we type more and more.This extension is incredibly helpful for quick search “Something like google does”.

##Requirements
Yii 1.1 or above
NOTE:- try to keep less number of columns for fast and accurate results

##Usage
Firstly create a folder called js in root (where the file index.php is there) and paste the file the file jquery.js

Then we need to edit Model Class and add the following function. This will be the engine that will provide free search in database and will return the results. This function is currently returning model (Tested) , but you can also return the CActiveDataProvider.


public function freeSearch($keyword)
{

/*Creating a new criteria for search*/
$criteria = new CDbCriteria;

$criteria->compare(‘name’, $keyword, true, ‘OR’);
$criteria->compare(‘barcode’, $keyword, true, ‘OR’);
$criteria->compare(‘part_number’, $keyword, true, ‘OR’);

/*result limit*/
$criteria->limit = 100;
/*When we want to return model*/
return Items::model()->findAll($criteria);

/*To return active dataprovider uncomment the following code*/
/*
return new CActiveDataProvider($this, array(
‘criteria’=>$criteria,
));
*/

}
Then we will be creating two actions (functions) in the controller class to perform this search. The first function (actionFreeSearch()) will be the main static page where we will provide the keyword. This page will send request to next function with the keyword parameter (actionSearchEngine($keyword)). This keyword is then send to model which will perform the search on that keyword and will return the results.


public function actionFreeSearch()
{
$model=new Items('search');
$this->render('freeSearch',array('model'=>$model));
}

public function actionSearchEngine($keyword)
{
// echo “THIS IS IAJAXX “.$keyword;

$model=new Items();
$model->unsetAttributes(); // clear any default values
$results=$model->freeSearch($keyword);
$this->renderPartial(‘_ajax_search’,array(
‘results’=>$results,
));
}

Also you need to modify the access rules


array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','InboundSearch','OutboundSearch','admin','FreeSearch','SearchEngine'),
'users'=>array('@'),
),

Now you need to create two views. First one for the actionFreeSearch() function which will be freeSearch.php

~~~
[php]

baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/jquery.js');
?>

$(document).ready(function() {

$(“#faq_search_input”).keyup(function()

{
var faq_search_input = $(this).val();
var dataString = ‘keyword=’+ faq_search_input;

var ref_id = $(‘#ref_id’).val();
var cust_id = $(‘#cust_id’).val();
var current_url = $(‘#current_url’).val();

/*This is the minimum size of search string. Search will be only done when at-least 3 characters are provided in input*/
if(faq_search_input.length>3)

{
$.ajax({
type: “GET”,
url: current_url+”/SearchEngine”,
data: dataString,
/*Uncomment this if you want to send the additional data*/
//data: dataString+”&refid=”+ref_id+”&custid=”+cust_id,
beforeSend: function() {

$(‘input#faq_search_input’).addClass(‘loading’);

},
success: function(server_response)
{

$(‘#searchresultdata’).html(server_response).show();
$(‘span#faq_category_title’).html(faq_search_input);

if ($(‘input#faq_search_input’).hasClass(“loading”)) {
$(“input#faq_search_input”).removeClass(“loading”);
}

}
});
}return false;
});
});

controller->id;
$current_url=$baseUrl.”/”.$model_name;

/*To Send the additional data if needed*/
$reference_id = 88;
$customer_id = 77;
//echo “Search :”.$current_url;
?>

<input type=”hidden” id=”current_url” value=””/>

<input type=”hidden” id=”ref_id” value=””/>
<input type=”hidden” id=”cust_id” value=””/>
Enter Item Name Part Number or barcode

~~~

Lastly you need to create a file _ajax_search.php for presenting the database results. This file is getting called every time we are typing each character.

foreach ($results as $row)
{
echo “Title: “.$row[‘name’].”
“;
}

##Resources