Add a new record to dataProvider

In my case, I have a $dataProvider which pasted forom controller. In view layout, i can get $dataProvider value by cgridview.

But, if I want to add a new reocord to the dataprovider before fetch it by gridview.

For example:

If I find all my model [in this case, i’ll call User model], in User model, there are 5 records, when I load layout, i want to add to $dataProvider a last record.

Can I? :mellow:

Try this:

$data = $dataProvider->getData();

$data[] = $model;


1 Like

andy_s, +1 from me for providing very interesting and quick solution for this problem! :] Thank you…

I am really surprised you liked it :lol: Ok, there is a bonus one string solution:

$dataProvider->setData(array_merge($dataProvider->getData(), array($model)));

Yes! :] Is that something strange about it? :}

A bit off-topic, but… And about one-string solutions. You really gave a short-cut version (shall I give you another +1? :P). But I’m always laughing, if some developer is trying to show-off his experience by saying that he can do this or that in one line. White chars (spaces and new line breaks) are for humans, compiler always ignores them, am I right. You could write whole long script in one enormously long line, by separating statements only with ; and brackets {} right?

Of course I am for readable code, but the best code is a short and readable one :D And a little note: PHP scripts are executed faster without tons of comments and white spaces, but it’s a different topic ::)

Probably that is the reason why we have Yii.php and YiiLite.php.