Yii 1.1: The Comedy of Errors


Please join the party by adding your own favorite Yii programming errors. Be sure to leave at least a hint at the correction.

The missing echo

In the view put:

<h1>Edit item: <?php $model->label; ?></h1>

Now spend your time debugging why you are not seeing the value of $model->label even though you are clearly asking for it.

The echo too much

In the view put:

<h1>Edit item: <?php echo $this->widget('CWidget'); ?></h1>

"Object of class CWidget could not be converted to string." A widget may not have an echo.

The true false

In the config file or wherever you are to provide a boolean value, put:

'someOption' => 'false',

Then read the PHP manual to find out why this does not evaluate to false like you wanted it to.

Rules for numbers

In the model rules() method put:

array('count', 'number', 'integerOnly'=>true),

then try

array('count', 'numeric', 'integerOnly'=>true),

and only then look it up: 'numerical'.

Rules for enums

In the model rules() method put:

array('format', 'range', 'in' => array('html', 'rss2', 'atom', 'json', 'xml')),

then later remind yourself that Yii is not SQL.

Rules for foreign keys

In the model rules() method put:

array('foreingKey', 'exists', 'className' => 'SomeModel', 'attributeName' => 'id'),

Sure we have 'exists' method for CActiveRecord model, but ...

Ending ?> tag in non-view

Extend a base class or edit a config file (needs to be included before the sessions starts) and leave a ?> at the ending with at least one trailing white space character. Make sure output buffering is off. Cry manly tears while trying to figure out why "Cannot modify header information - headers already sent" spams your log files.


$total = 0;
foreach($model->rels as $rel);
    $total += $rel->count;
echo $total; // wrong result!!

I wasted 12 hours debugging this.

visible option in CMenu and null values

Use a parameter feature.enabled to enable/disable a feature through configuration. And then wonder why you still see the menu item, even if you have no feature.enabled parameter at all.

<?php $this->widget('zii.widgets.CMenu', array(
    'items' => array(
            'label' => 'A new feature',
            'url' => array('feature/view'),
            'visible' => Yii::app()->params['feature.enabled'],

Yii::app()->params['feature.enabled'] returns null if the parameter is not defined. But in this case that's different from false because CMenu uses isset($items['visible']) which returns false for null values.

Total 3 comments

#12492 report it
fsb at 2013/03/23 02:52pm
Semicolon error

@Igoru-san good point!

My IDE does that as part of reformatting to PSR-2, so I don't have to search-replace. (Also inserting a missing {} around the iterated instruction if necessary.)

It also highlights this error on the screen via it's static code inspections.

All in all, with a good IDE like PhpStorm, my code quality is better and I finish it faster.

#12491 report it
Igoru-san at 2013/03/23 02:37pm
Semicolon error

That's why I tend to search/replace "\n{" in Yii-generated code and reformat it to have the curly brackets always in the end of the line :P

#8651 report it
Roman Solomatin at 2012/06/16 11:52pm

Thanks! I was having one of these errors.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: fsb
  • Updated by: Mike
  • Category: Others
  • Yii Version: 1.1
  • Votes: +21 / -1
  • Viewed: 10,539 times
  • Created on: Dec 7, 2011
  • Last updated: May 3, 2013
  • Tags: errors comedy