new to Yii and MVC, how to create a foreign key selection into crud pages

Hi all,

I am new to yii and the mvc programming architecture and trying to create some database application with various tables and admin pages.

I have created some models and crud pages with the yiic and i am now at the point to make my modifications and extensions to the generated code.

My problem is I have a address table with many address entries.

Each address can has a company_id wich is a foreign key in the company table.

So my point it how can I modify the code to easily create the selection of the company in a address entry on the admin update form for a selected address.

Sadly I have that many entries in both tables (address and company) so that i can't do an easy select element.

What I need is a button which possibly opens a view where all the companies are displayed in a table and also be search for. And on a click of a link or another button on the select page my company_id is given back to the admin form of the address.

My questions are how would you program this in mvc with yii?

  • would you create a separate controller for the company selection?

  (a controller class for the company and address table is created with crud)

  • or add this code to either the company or address controller class ?

  • or create something with ajax?

  and add the code also into the update view code for the address table

I also wonder how the whole stuff will be called and how the input is given back to the right page.

Also how should the whole thing be accessible via url?

my update url ist currently like this

http://localhost/yii…st/update&id=11

should the company selection for the address entry with the id =11 have the url like:

http://localhost/yii…any_select&id=5

or should it be more like

http://localhost/yii…ct&id=5&a_id=11

to use the mvc design correctly?

thx in advance for any hints regards Horizons

You generally want one controller and model per table in your database.  So you may want a AddressController and a CompanyController, as well as as a address model and a company model.

You may want, for instance, your address modification page, to be set up like this:

http://localhost/yii…ss/update&id=11

-updates address with id 11.  Will route to the update() action in the address controller

How are the addresses and companies related?

(http://www.yiiframew…ng-relationship - read up)

The relationship is an easy 1:1 but i have that many addresses and that many companies that i can't do the selection for the company of an address in an easy select box.

Each address can only have one company address connected to.

What I need is a search functionality which lists the companies which matched the search string and a kind of selection out of the search results.

But I don't know if the company selection for the address (and all the required code) should be stored in either the address or company controller, or if I maybe should create a separate controller for this. I also don't know how I can do the transfer from the address update page to the company selection and return back.

Hasn't anyone programed something similar yet?

regards Horizons

I am not quite sure what you want to do. Is it something similar to the “category” dropdown list as displayed in this page? http://www.yiiframew…com/extensions/

No i don't want a select box.

I have address entries , with name, firstname whatsoever in the database.

Each address entry could a company_id connected to which corresponds to one company of the company table.

But because I have thousands of company entries i could not easily make a drop down for a company selection (also there are some companies similar in name).

So I must refer to some other page where i can search for some fields of the company table and then select one entry which should be set as a company_id in the address list entry.

Workflow e.g. like below:

address list => select one address to edit => want to change company (click select company button) => search for specific entry in company table with search options => select one entry => get back to the update form of the address

Regards Horizons