Greetings all,
I am new to Yii but starting to get the hang of it a l’il bit. I am building a small application. Rather than load a “flat” model like in the Gii-generated MVC, I’d like to load a model with all its children and let the user update the whole thing, rather than just parts of it.
For example, let’s say I have four tables: Company, Location, Contact, Contact Phone. A company can have multiple locations. A company can have multiple contacts. And a contact can have multiple phones. So for my update page, I want to load all the meta data for a single company, all its locations, all its contacts, and all its contacts’ phones. Hope that makes sense.
Now, from what I understand, I could theoretically do this with Yii OOB functionality. However, one thing I ran into is that it doesn’t appear I can use the CActiveForm field helpers for relational data. IOW, I can do something like $form->textField($model,‘company_name’), but I could not do something like $form->textField($model,‘contact->contact_name’) (within a loop or something). So the first question is, how can I include child data in the view’s form fields?
Another approach I’m playing with is using KnockoutJs to take advantage of Ajax, data-binding, etc. Here, I would require a Json-serialized version of my model including all its relational data. I realize I can use the “attributes” property and “getRelation()” method on the CActiveRecord model to get at the data via PHP. However, there is no obvious way to serialize it all for Json. I can do something like CJSON::encode(array($model->attributes)) – but that just serializes the attributes. The retrieval obviously needs to be “eager” (not “lazy”) so I have everything at once (I’m aware of the “with()” method but I don’t see how that applies here, if at all). So my second question is, is there a way to serialize the attributes and relational data (including those contact phones, which are two relations from the company) in one swoop? Short of writing my own model template?
Ultimately I would want a Json-serialized version of something like this:
-Company Name (company meta data)
-Logo (company meta data)
-Acronym (company meta data)
-Locations (collection)
–Location 1
—City
—State
–Location 2
—City
—State
-Contacts
–Contact 1
—Phones
----Phone no.
----Phone no.
–Contact 2
—Phones
----Phone no.
Any idears? Hope this is clearer than mud. Any help appreciated.
Thanks
Tom