Tips for Yii. Сhapter 4.
Label marked as a required field.
Sometimes it is necessary to mark a label as a required field (marked with an asterisk), but you may not write 'required' in the rules.
So you should pass 'required' => true to the htmlOption array:
CGridView. How to add a drop-down list for sorting/search..
The filter in CGridView which is set to true is a text field by default.
Let us see how to turn it to a drop-down list (dropDownList).
Add the following code to its view:
So you will have the following:
And now you need only to add the following code to a search method of the model:
As a result you will have the following filter drop-down list:
When a query is cached, its result is stored in the query cache and is taken from it when needed.
For instance, if you have to store the cache in the file, you need to write in config/main.php file the following:
Set the cache time to 1000 seconds for the DAO:
Do all the same for AR:
If you would like to clear the entire cache, call the flush() method:
Debugging SQL queries.
In my opinion the most qualitative debugger for Yii is yii-debug-toolbar extension.
If you do not have an opportunity/necessity/wish to install it, but you have to fix smth, for example, drop of sql query, you can get help from logging in FireBug/Google Chrome Developer Tools.
For this purpose, you should make the following changes in the config/main.php file:
Uncomment the following lines in the index.php file:
defined('YII_DEBUG') or define('YII_DEBUG',true); and defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
The "Console" tab in FireFox:
The "Console" tab in Google Chrome:
For example, we have the query:
By calling getText() which allows to view the query, not to execute it.
As a result we get the following: