Jump to content
Larry Ullman's Book Forums

Invalid Argument Supplied For Foreach() Using Carraydataprovider

Recommended Posts



I'm working in  Yii framework.  In my index action I created the dataprovider in the following way

    $user_id = Yii::app()->user->id;
    $sql = 'SELECT * FROM post
      LEFT JOIN comment ON post.id = comment.post_id
      AND comment.user_id =:user_id
      LIMIT 0 , 30 ';
    $command->bindParam(':user_id', $user_id,PDO::PARAM_STR);
    $rawData = $command->execute();
    $dataProvider=new CArrayDataProvider($rawData, array(
               'defaultOrder' => 'post.created',

then I render the index view



Index view is doing

    <?php $this->widget('zii.widgets.CListView', array(
    )); ?>

which cause the error  


    Invalid argument supplied for foreach()
         * @param array $directions the sorting directions (field name => whether it is descending sort)

         136         foreach($directions as $name=>$descending)
    137         {
    138             $column=array();
    139             $fields_array=preg_split('/\.+/',$name,-1,PREG_SPLIT_NO_EMPTY);
    140             foreach($this->rawData as $index=>$data)
    141                 $column[$index]=$this->getSortingFieldValue($data, $fields_array);
    142             $args[]=&$column;
    143             $dummy[]=&$column;
    144             unset($column);
    145             $direction=$descending ? SORT_DESC : SORT_ASC;
    146             $args[]=&$direction;
    147             $dummy[]=&$direction;
    148             unset($direction);
    149         }


When I use $rawData = $command->queryAll();  instead of       $rawData = $command->execute();


I got the error


Call to a member function getAttributeLabel() on a non-object in C:\wamp\www\contest\protected\views\contest\_view.php on line 8


where the code is


<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>




Can you help me?
Thank you in advance



Link to post
Share on other sites
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
// or using: $rawData=User::model()->findAll();
$dataProvider=new CArrayDataProvider($rawData, array(
             'id', 'username', 'email',
// $dataProvider->getData() will return a list of arrays.

With CActiveDataProvider queryAll() or findAll() you insert in for your $rawData, so i am guess the problems is happening in your views files from trying to interpret the returned data from the CActiveDataProvider.


Can you show us more of the _view.php code and possibly do a print_r surrounded by some HTML <pre> (preformatted tags) of the data attribute $dataProvider you are passing to the _view.php file and post it up here.

    <?php print_r($dataProvider); ?>
  • Upvote 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...