does gii automatically create relations for generated models? - yii

I'm new to Yii here. In the documentation, it seems to imply that gii will create the relations for related models. But when using gii to generate models from DB, it doesn't seem to be the case. For example, I have a user table and a profile table with a column "user_id INTEGER DEFAULT 0", but the relations array is empty in the generated model. Did I do something wrong, or gii just doesn't automatically recognize the relations?
Thanks,

Gii will create relations for MyISAM tables if you include a format like the following in the comment of the referenced columns:
CONSTRAINT FOREIGN KEY (name_of_this_field) REFERENCES related_table_name(related_field_name)

Gii can automatically create relations for generated models if corresponding tables in your DB have foreign keys, but not all storage engines support foreign keys. For example, if you use MyISAM tables in MySQL, you have no way to define it.

Related

Table has a composite primary key which is not supported by crud generator

I am working with YII framework. I tried to generate a model for my DB using the YII framwwork and it made everything correctly, however when I tried to use the Crud Generator for my DB it worked for all the tables but 1 table, for that I get this error:
Table 'doe_logline' has a composite primary key which is not supported by crud generator.
here is my database in more details:
how can I get rid of this problem?
Unfortunately standard Yii Crud Generator doesn't support tables with composite key.
Check this discussion.
There are two options:
Create all pages manually. Don't forget to extend your doe_logline.php with composite key definition:
public function primaryKey(){
return array('id', 'case_id');
}
Use external tools such as hansoncoding.net yii crud generator, which can generate CRUD also for tables with composite keys.

Table has a composite primary key which is not supported by crud generator

Table 'Media_Key' has a composite primary key which is not supported by crud generator.
Why is this happening?
Using the crud generator tool
My DB tables looks like this
MEDIA MEDIA_KEY KEYWORD
media_pk ------------| ID |------ keyword_pk
--< media_fk |
keyword_fk >--
It seems like Media_Key's foreign keys (media_fk and/or keyword_fk) are included into table's primary key. Try to drop PK, recreate it with only ID column, recreate model and then use crud generator again. Or maybe it'll be more reasonable to create controller for MediaKey model manually.

Translating ER to SQL DDL

When I am translating an ER to SQL DDL I need to Create a table only for the entities or for the relations too?
Yes, you need to create tables for both entities and relationships. Also, keep in mind that you have to include foreign keys and link your tables
It depends what type of relationship you have. If it is many to many relationship then it must require a separate table for the relationship itself. Any way you can search on google ER diagram to relational database or look the text book on the relational model chapter of Modern database management sytem of author Hoffer.
You require a CREATE TABLE statement for each ENTITY.
Your relations ships are generally implemented as FOREIGN KEY CONSTRAINTS or FOREIGN KEY INDEXES between those tables.
Each entity becomes a table and each many to many relationship becomes a table.
Add the child columns (FK column) to the (child) tables also. When you create a N:M (many to many) relationship in for example DeZign for Databases, you see that an intersection table is create automatically. The columns which are added automatically are in first instance the columns of the primary key of both tables. You can see that in this video:
http://www.datanamic.com/support/vd-dez001.html

Fluent Nhibernate mapping Legacy DB with composite key

I am using Fluent NHibernate (which I am fairly new to) in an application I am developing using a legacy Oracle DB. The DB has composite keys which are comprised of foreign keys and database generated columns. The generated columns are supplied by calling a DB function with the table name, and one of the other foreign key parts. The generated composite key parts are not unique, and I cannot change this. The generated key parts are often used as foreign keys on other tables too.
If I create entity mapping which specifies the composite key as it is in the database, then we cannot use any identity generation strategies, which breaks unit of work
If I create entity mapping which specifies only the generated column as the primary key, then I can use trigger-identity to generate the ids, and I get unit of work, but I then have a problem when I want to update, or access a child collection: The other parts of the key are not included in the WHERE statement.
Can anyone give me any advice on how to proceed?
If I stick with mapping composite keys, can I extend nhibernate to output the SQL to use trigger-identity? If so, can you suggest a starting point?
If I map a single column key, can I include other properties in a WHERE clause for HasMany mapping and Updates?
Unfortunately, as you have already found out, there is no support at all for this setup.
My suggestion is to do INSERTS manually (using custom SQL, for example). And yes, this breaks the UoW, but that is true of identity too.

How does Yii Gii detect MANY_MANY

My database structure has several MANY_MANY links. However Gii (giix in my case) does not always generate them as MANY_MANY, instead it generates a HAS_MANY with the joint table.
Are there rules to make sure Gii does the correct relationship? Does it look at the name of the columns? Names of the tables? Indexes? Foreign key names? What if there are other columns in the joint table?
Gii actually checks every table to see if there are join tables (see ModelCode::isRelationTable() in gii/generators/model). It detects a table as a join table if:
The table has 2 columns
Both columns are foreign keys
The foreign keys point to different tables
Gii then creates a many-to-many relationship between the participating models.
Gii creates many One to Many (1:n) relations self::BELONGS_TO + self::HAS_MANY in models
self::MANY_MANY need type manually