0

Is there a way to show in a Grid that takes it data from CActiveDataProvider some new rows that are not actually yet into the database?

here's my scenario..

I have to fill by X times (the quantity of a product in a bill) and provide to each row the possibility to be edited and saved

 Product    QTY 
 product1   3
     |
     ____  Edit item 1 (not yet in the db)
     ____  Edit item 2 (not yet in the db)
     ____  Edit item 3 (not yet in the db)

So it's kind of like a master detail grid (That I've sorted it out how to do it..) but I can't display things on a grid that arren't yet in the database..

I know I have to create an array of temporary models like $model[]=new MODEL(); and push the somehow to the CActiveDataProvider but don't know the syntaxis...

PartySoft
  • 2,749
  • 7
  • 39
  • 55

1 Answers1

0

You can't do this with CActiveDataProvider because as the documentation says

CActiveDataProvider provides data in terms of ActiveRecord objects which are of class modelClass. It uses the AR CActiveRecord::findAll method to retrieve the data from database.

However, there is CArrayDataProvider. You can put arbitrary data in it, ie. merge an array of real objects with an array of empty ones and use that.

$rawData=array(
    array('id'=>1, 'username'=>'from', 'email'=>'array'),
    array('id'=>2, 'username'=>'test 2', 'email'=>'hello@example.com'),
  );

$arrayDataProvider=new CArrayDataProvider($rawData, array(
    'id'=>'id',
    'sort'=>array(
        'attributes'=>array(
            'username', 'email',
        ),
    ),
    'pagination'=>array(
        'pageSize'=>10,
    ),
));

As you can see, the data here is a simple array, but you can even sort the data.

adamors
  • 2,672
  • 5
  • 32
  • 45