Yii Framework Forum: Detect $models array before use it! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Detect $models array before use it! See page. 96 codes in 3.

#1 User is offline   Mikewolf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-January 12

Posted 18 January 2012 - 04:41 PM

hi, I checked errata page, don't find this, so I write here,

in Page 96 3. there are some code like this:

<ul class="tasks">
		<?php if(is_array($models) && count($models)):?>
			<?php for($i=0; $i < count($models); $i++):?>
				<?php $this->renderPartial('_task', array(
                                                                          'model'=>$models[$i],
                                                                           'index'=>$i,
                                                                            )
                                                            );
				?>
			<?php endfor?>
		<?php endif?>
	</ul>


but because in 2. there are also assing $models as object, code like this:
if(!empty($models)) {
			//could be saved to DB here
		} else {
			$models = new Task();
		}


if w/o checking of $models in index.php, then people will get error, because there is not model in $models[0]
so maybe man can append code like this:
<?php if(is_array($models) && count($models)):?>
    <?php for($i=0; $i < count($models); $i++):?>
              ....
    <?php endfor?>
<?php endif?>


Or there is other solution? Please help, thank you!
Newbee of Yii
0

#2 User is offline   Mikewolf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-January 12

Posted 18 January 2012 - 04:47 PM

forget it, I was wrong, I make $models[] = new Task() as $model = new Task();

in book all correct. Sorry.
Newbee of Yii
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users