The Comedy of Errors

You are viewing revision #9 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#8)next (#10) »

  1. The missing echo
  2. The echo too much
  3. The true false
  4. Rules for numbers
  5. Rules for enums
  6. Rules for foreign keys
  7. Ending ?> tag in non-view
  8. Semicolon

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.

Semicolon

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

I wasted 12 hours debugging this.

19 0
15 followers
Viewed: 19 732 times
Version: Unknown (update)
Category: Others
Written by: fsb
Last updated by: Mike
Created on: Dec 7, 2011
Last updated: 11 years ago
Update Article

Revisions

View all history

Related Articles