CRUD generation Foreign Keys

Hi,

I've 2 tables. Categories and Programs.

One program belong to one categorie.

When I generate Program CRUD I want the create and update view shows a list of categories and not the field categorieID. Is this possible with crud command or i have to do it manually?

Thanks.

this is not possible with crud because it provides a basic access to the table. you have to:

  • create models of the 2 tables with model command

  • declare relationships of the models (see CActiveRecord in docs on howto)

  • extend CrudCommand.php putting YourCrudCommand.php in the shell directory of your webapp (protected/commands/shell) and modify the function "generateinputfield": inside it if for the column your are generating the input you find that there is a relationship in the model, do cool stuffs! (for example a dropdownlist related to the external table).

  • run yourcrud in shell instead of crud

this is my idea

While Yii knows the relationships (which FK references to which PK) between tables, it is impossible for Yii to identify the type of the relation. For the same relationship, its type could be HAS_ONE, HAS_MANY or MANY_MANY. That's why crud doesn't support modelling the relationship. However, this should be very easy for developers to identity.

Maybe you can add a parameter to shell crud command called 'proceedkeys'. When crud command will be called with this parameter it will ask user for relanshionship type using some kind of "survey".

This task is more suitable for a Web-based yiic, which we might provide in 1.1 or 1.2 release.

I agree Qiang that this is suitable option for web-based yiic.  However, it is possible to identify the type of relationship and hence can be generated.  If need be we can always edit the generated file, and the generation would have saved us much of the effort.

Here is how we can derive the relationship type:

For two tables taking part in the relationship, if ALL the columns of the relationship comprise the primary (or unique) key of one table, then automatically that side of the table has to be a HAS_ONE.  If the 2nd table also has all columns that take part in the relationship in its PK, then it also is a HAS_ONE.  If there are additional columns in the PK besides the ones taking part in the relationship (identifying relationship) OR if some or all of the columns are NOT part of PK (non-identifying relationship), then it is a HAS_MANY.

In most cases direct M:M without an associate table are not correct relationships and are usually (but not always) as a result of bad design.  So we should be well covered with this formula. However if there is a M:M then it will follow the same rule.  Both tables will have extra columns in the PK that are not taking part in the relationship (or have columns that are not even part of the PK). 

To cover M:M where there is an associate table, it may not be easy to generate code automatically, but seems like it can be done, as I can see some patterns we can work with.  I am not sure however why the M:M definition is even important when there is an associate table.  Is it not just two 1:M relationship?  Where/how does the application benefit from this definition?  I am curious to know.

Hi… does the actual version of Yii/Gii already does this? (dropdown boxes for foreign key fields)

Tks

BSC

It doesn’t do it …

But as I know, there is extension for Gii that does needed stuff : template collection for the Gii System (GTC) . You can find it in yii extensions. It uses Relation extension. Try it.

Will it not be a Good Option/Feature if

1- We can select the Tables which have the relation with other Tables

2- Set the Type of the Relation

3- Set which Column is Related with Which Column

4- Can Process it Using models created by CURD.

By this, we can have automated Software Developer which need Just Database to Process its operation.

Thanks