Using A Single Form With Multiple Models

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]I spend a long time, thinking how to perform a multiple form, by combining two models.[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]

[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]The biggest problem that I see, it´s: "how to send information to controller when press submit?". [/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]

[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]Keeping in mind, that with renderPartial() method, is possible to sending "one model" only.[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]

[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]How curiousity: the models to combining are: one parent entity and other many_many relation entity, of entity parent.[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]

[/color][/size][/font]

[font="Helvetica Neue, Helvetica, Arial, sans-serif"][size="3"][color="#282828"]ThankS![/color][/size][/font]

Take a look at the MultiModelForm extension; it might be useful to you for that purpose.

The idea of MultiModelForm could be a good solution for me, such as says the intro "allows to work with multiple records and different models in a edit form".

Anyway, I want to continue to studing other ideas, because if I read that is a very complete extension with many possibilitys.

And in my case, I consider that I need something more simple because only need to create one multiform.

PD: BTW thanks for your reply Jose

From the PM you sent me I gather that you want a "master-detail" form, yet believe it requires a MANY_MANY relationship. MMF (multimodelform) is very good for master-detail forms such as invoices, etc. but it uses a HAS_MANY (parent) - BELONGS_TO (child) kind of relationship, not MANY_MANY.

Take a look at what the Guide says about MANY_MANY relationships. It supposes a pivot table (e.g. tbl_post_category) that doesn’t even require a model, much less the CRUD scaffolding. My understanding is that the pivot table gets populated automagically through the relations() method defined in each main table. I try to stay away from MANY_MANY relationships unless unavoidable, and then only because I need additional columns in the pivot table which then requires me to write a model and CRUD for it, defeating its original intent.

As for renderPartial(), you can send more than one model’s data if you use a dataProvider.

Exactly! As Jose say, I’m forced to use a pivot table because I have additional fields in my table. It is what I have deduced after reading Relational Active Record and found many threads in YiiFramework.

My intention:

I have to generate all the information on one form that contains data information from: pivot table (RouteLanguage) and parent table (Route).

In other words and detailed, the parent table contains general data and pivot table the fields with more detailed data.

What I did so far [Photo]:

  • Autogenerate the 3 models with giix.

  • Modify MANY_MANY relationship with a HAS_MANY and ‘through’ connector.

  • Autogenerate the CRUD from parent tables (Route and Language).

What does it lack and what problems are there?

That giix / gii can not generate a multiple form for two tables. Then I need to add in Route’s “create form” (parent table form), the pivot table information ‘RouteLanguage’ (that is, their fields additional).

And then send form data to saving the information in both tables (parent and pivot table), and then save everything from "parent controller" (RouteController.php).

Therefore lack the following:

  • Modify ‘create form’ of parent table (Route) with pivot table (RouteLanguage) aditional fields, so that having a multiform.

  • Figure out how send data information of two model (Probably with partialRender?) to controller.

  • Save information from controller in each of two data model: pivot table (RouteLanguage) and parent table (Route).

I copy the models to guide you:

These are the first 2 ‘Tables father’:

Route

Id_route INT (11)

Active TINYINT (1)

DateCreated DATETIME

DateModified DATETIME

PRIMARY KEY (id_route)

Language

Code VARCHAR (13)

Name VARCHAR (45)

Active TINYINT (1)

Main TINYINT (1)

PRIMARY KEY (code)

And the third, the pivot table (with four additional fields):

RouteLanguage

Id_route INT (11) <- Foreign Key

Code_language VARCHAR (13) <- Foreign Key

Name TINYTEXT

Description TEXT

Detail MEDIUMTEXT

Zone TEXT

PRIMARY KEY (id_route, code_language)

Is it possible to doing so, or is there a better way?

If possible, what do I need to do now? Refered to if I need a ‘behavior extension’, another structure (more tables and models)…I’m disoriented what’s the next step. :mellow:

Thanks!

MultiModelForm will not help you with that. However, there are a few extensions that support MANY_MANY relationships and some even provide Gii templates that will generate the CRUD for you. Search the extensions area for "many many".

I recommend that you use an integer as the primary (surrogate) key for table Language instead of a varchar because otherwise you’ll have a hard time not only with the pivot table updates but also with indexing.

I had looked for a long time, a way to generate the CRUD for the pivot’s model table (RouteLanguage) but I realized that when it’s time to managing Routes the father (Route) and the pivot are joined, i.e., are controlled simultaneously and form a single functional block (this proposal among other more reasons took me to make this decision).

This is because the parent (Route) contains general data and pivot (RouteLanguage) contains the details. So for example, when is saving a Route, you need an action to controlling two items: data general and details, and in another way when shows a Route, the same.

Thus, with the above approach I’ve found a way to complete a form with two models (How to use single form to collect data for two or more models).

Shortly, I tell whether it can manage my idea.

PD: I use a varchar for two reasons, first is close to reality and second, because sql helps me to handle duplicates.

Hi it’s very simple

please see this forum I hope it’s some help.

It is a elegant solution, thanks @Ankit! :)

It’s the solution that I will use in the following prototype.

So, for now, I’m going to continue with the option I propose above …

…it functions perfectly. Allows to sending information from the view to controller and from the latter to, validate and save with ajax whether on success.

The best thing about this option is that you can use the rules() methods of each model to validate. And the worse, is that breaks the normal render() flow to having to send two models instead of one.

So, that is why I really like your option with CFormModel and how solve the problem. That is closest to reality, creating a separate form to annex models. Just one thing that I don’t fit me, why create rules() (probably copied model rules() ) for each model to validating? Imagine that we cannot dispense with it, right?

Hi please see this wiki

I’ve been reading the link (Creating Model) and related issues: Best MVC Practices and Module.

In summary, I saw that I should not use CForm because I’m going to keep data in db I must, and then use ActiveRecord.

The way I see it, could be an exception because when keeping information in order to two models, it’s not worth the effort creating an ActiveRecord, considering that would being an intermediate step and for this interests a CForm.

@Ankit: it’s, what you’re trying to show me?

Sorry man I don’t understand last message

Sorry my English, what did you not understand?

Basically says: according to the above link you sent, I should not use CForm.

Why? Because CForm is NOT used to storing information in tables (db, Data Base). Then I have to use the other: ActiveRecord.

@Ankit: isn’t it?

Problem: The AR model is already auto-created (from giix). So, the controller just save information in both models (model parent and child: both have its own AR class) through the same action.

@Ankit: how would you do and why?

In my understand your question you can create the proper relation on Cform function?

Yes, it is. Knowing, above all, that CForm is used only if we don’t want to save information.

Two or Mulit log-in in single form

see wiki - http://www.yiiframework.com/wiki/779/how-to-pass-the-third-parameter-to-useridentity-on-login-authentication/