Difference between #9 and #8 of The Comedy of Errors

unchanged
Title
The Comedy of Errors
unchanged
Category
Others
unchanged
Tags
errors comedy
changed
Content
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:
~~~
[php]
<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:
~~~
[php]
<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:
~~~
[php]
    array('count', 'number', 'integerOnly'=>true),
~~~
then try
~~~
[php]
    array('count', 'numeric', 'integerOnly'=>true),
~~~
and only then look it up: 'numerical'.


Rules for enums
---------------

In the model rules() method put:
~~~
[php]
    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:
~~~
[php]
    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.

Semicolon
---------

~~~
[php]
$total = 0;
foreach($model->rels as $rel);
{
    $total += $rel->count;
}
echo $total; // wrong result!!
~~~

I wasted 12 hours debugging this.
Write new article
  • Written by: fsb
  • Updated by: Mike
  • Category: Others
  • Yii Version: 1.1
  • Votes: +20
  • Viewed: 7,407 times
  • Created on: Dec 7, 2011
  • Last updated: May 3, 2013
  • Tags: errors comedy