Multiple model of same instances in one page

Hi

Which is the best way to use two or more same instances of model (but with different data) on the same view ?

For example three model Books

  1. one cgridview for published books another

  2. one cgridview for unpublished books

  3. one form to add a book.

All in the same controller/action/view

THis wiki is usefull for tabular data but not for above issue

http://www.yiiframework.com/doc/guide/1.1/en/form.table

What is your suggestion ?

Thanks

-Kostas

What is your need?

Hi Fabrizio.

Exactly that I mentioned :)

In the same page I want to display 2 cgridview one for published book one for unpublished, plus a form to add a new book.

I want a User interface without more than one action.

So you need:

  1. For the first grid, a dataProvider for published book;

  2. For the second grid, a dataProvider for unpublished book;

  3. An ActiveForm to handle user data;

For 1) and 2) if you don’t need to implement search inside the grid, model is unnecessary, because it is required only the data provider for each grid;

For 3) you can pass an empty model and then in the controller check if $_POST data is filled.

The problem is both of cgridview filter send the same name attributes (Book[id],Book[title] etc) to the action, so there is conflict on filter of two cgridviews

In addition the form (that generates names based on the class name of the models) may make worst the conflict with the two cgridviews, especially in case all of them use the same method (GET or POST)

Both of widgets, $fotm->textField ,etc use internally the get_class PHP function to generate the name of the html fields

In addition, I want to use search both for two cgridview (your answer 1,2).

The only think that separate the form for the two cgridview is by using GET (about cgridview) and POST method (about form). But I can’t seperate the cgridview filters…

The only think (but tricky)

is to create a class model Book2 (extends Book)

I mean class with empty code!


class Book2 extends Book {


}

So, two separated array in GET/POST method with name Book[id],Book[title] and Book2[id],Book2[title] will be generated solving this problem.

But is tricky enough…

The only robust solution that I found is to use custom filters name both of two cgridview

and set custom name of form fields.

Just separate the ajax urls for the gridviews, though it will require js to be effective in the user’s browser.

http://www.yiiframework.com/doc/api/1.1/CGridView#ajaxUrl-detail

Or, as you are thinking, creating dedicated models for searching (Book1 and Book2) extending from Book is a good idea. I don’t think it’s tricky.

Gii of Yii 2 is doing just that, creating a dedicated model for searching from the main AR model.