Tips for Yii. Chapter 14.
-
CHtml::listData usage.
There is a query:
- $ids = array(18, 19, 20, 21);
- $sql = 'SELECT id_object, COUNT(id) as count FROM {{images}} WHERE id_object IN ('.implode(',', $ids).') GROUP BY id_object';
- $res = Yii::app()->db->createCommand($sql)->queryAll();
The result of the query running will be the following array:
- Array
- (
- [0] => Array
- (
- [id_object] => 18
- [count] => 3
- )
-
- [1] => Array
- (
- [id_object] => 19
- [count] => 3
- )
-
- [2] => Array
- (
- [id_object] => 20
- [count] => 4
- )
-
- [3] => Array
- (
- [id_object] => 21
- [count] => 2
- )
- )
Task: to return the array. The keys of this array are "id_object" and key values is "count".
You can certainly use such construct's as foreach / for / while ...
But Yii framework is good because of its "magic" methods.
So all you need is to insert a line of the code:
- return CHtml::listData($res, 'id_object', 'count');
The result is an array, which we need:
- Array
- (
- [18] => 3
- [19] => 3
- [20] => 4
- [21] => 2
- )
-
Display of the validation errors in the block, which you can close.
Practical application: the mobile version of the website/ responsive design, where the viewable size of the screen is limited. That's why user can hide them after reading the description of errors. So we can bring an overview of the "useful" part of the site back.
If the project uses Yii BootStrap, so you need to write this:
- <?php
- if (CHtml::errorSummary($model)) {
- Yii::app()->user->setFlash('error', CHtml::errorSummary($model));
- $this->widget('bootstrap.widgets.TbAlert');
- }
- ?>
Instead of:
- <?php echo $form->errorSummary($model); ?>
It helps to close the error list using this sign "x".
If the project doesn't use Yii BootStrap, we can add the needed functionality ourselves:
Instead of:
- <?php echo $form->errorSummary($model); ?>
write this:
- <?php if (CHtml::errorSummary($model)) :?>
- <div class="error-block">
- <a class="close" href="javascript:void(0);">×</a>
-
- <?php echo CHtml::errorSummary($model);?>
- </div>
-
- <?php
- $css=<<<EOT
- .error-block, .error-block .errorSummary { background-color: #DE6D6B !important; color: #FFFFFF; }
- .error-block .close { float: right; font-size: 22px; position: relative; right: 9px; text-decoration: none; top: 1px;}
- EOT;
- Yii::app()->clientScript->registerCss('inline.css', $css);
-
- Yii::app()->clientScript->registerScript('close-action', "
- $('.error-block .close').click( function(){ $(this).parent().fadeOut(500); });
- ", CClientScript::POS_END);
- ?>
- <?php endif;?>
-
CListView. Passing of optional parameters to the view.
There is the call CListView in the code:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider' => $dataProvider,
- 'itemView' => '_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes' => array(
- 'position',
- 'type',
- 'active',
- ),
- ));
You need to know not only $data object in_view.php, but also the meaning of the text in the variable $text.
We need to add viewData:
- 'viewData' => array('text' => 'Here will be a text'),
The call will look like this:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider' => $dataProvider,
- 'viewData' => array('text' => 'Here will be a text'),
- 'itemView' => '_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes' => array(
- 'position',
- 'type',
- 'active',
- ),
- ));
Now we can display the value of the variable $text in the view.
You can also use a "clip". Write in the controller:
- $this->beginClip('testClip');
- echo 'Here will be a text';
- $this->endClip();
And in _view.php we will add this code:
- <?php
- if (isset($this->clips['testClip']) && !empty($this->clips['testClip'])) {
- echo $this->clips['testClip'];
- }
- ?>
-
CViewAction. Static pages in Yii.
Let's look at the script of the business card website Open Business Card.
Let's create the folder "staticpages" in the directory protected/views.
We will add two files into this folder: staticpage1.php and staticpage2.php.
The content of the file staticpage1.php is:
- <p>This is an example of static page #1.</p>
- <p>This page can be edited in text editor.</p>
Here is the content of the file staticpage2.php:
- <p>This is an example of a static page #2.</p>
In the file protected/controllers/SiteController.php we will replace this code:
- public function actions() {
- return array(
-
- 'captcha' => array(
- 'class' => 'MathCCaptchaAction',
- 'backColor' => 0xFFFFFF,
- ),
-
-
- 'page' => array(
- 'class' => 'CViewAction',
- ),
- );
- }
to:
- public function actions() {
- return array(
-
- 'captcha' => array(
- 'class' => 'MathCCaptchaAction',
- 'backColor' => 0xFFFFFF,
- ),
-
-
- 'staticpage' => array(
- 'class' => 'CViewAction',
- 'basePath' => 'application.views.staticpages', # the directory where the static pages are.
- 'renderAsText' => true, # otherwise PHP code will be processed, if it exists on the page.
- 'defaultView' => 'staticpage1' # default page
- 'layout' => '//layouts/user' # layout for static pages. If "null" is specified, the page will be without a layout
- ),
- );
- }
Now you can call the static pages using this way:
http://site_domain/site/staticpage/view/staticpage1 and http://site_domain/site/staticpage/view/staticpage2
Links do not look good. Let’s fix it.
In the file protected/config/main.php add a rule (section urlManager):
- 'statpage/<view:(staticpage1|staticpage2)>' => array('site/staticpage'),
Now static pages can be invoked so:
http://site_domain/statpage/staticpage1 and http://site_domain/statpage/staticpage2