Help needed to understand Relations

Hi all,

I have been writing to ask help about understanding the relations in the models. Let me give you a little background what problem I am facing and trying to resolve:

I have one main table e.g MainTable in which I have a cloumn StudentID as a primary key. Then I have 10-15 different tables which have information about a particular student in table MainTable. They all have StudentID column as foreign key. Some of them will have only one row of record of against a particular StudentID in MainTable. Some of them will have multiple rows against StudentID in MainTable.

Let me write once again that StudentID is in all tables as foreign key but primary key in MainTable.

I created Models for all the database table. When it came to creating CRUD, it only performed CRUD operation for MainTable but it does not perform CRUD operation for the rest of the tables and give following error.

‘Primary key is not defined in the table Class.’

I was assuming that the Gii CRUD will generate files for all models but hopeless. Now I have searched a little bit on forum as well as on google but all in vain.

Finally, I need a solution to create controller and view files automatically as it created for MainTable to save lots of hassle of writing code(however i will be ammending the code to suit my needs).

[list=1]

[*]I need to know if there is a solution to this particular problem. If yes, how.

[*]I need to understand the relationships as a whole

[*]I need a link/source where that can help to learn more about relationships

[/list]

I would appreciate a quick response of you guys in this regard.

Thanking you in advance.

The rule of thumb is to use ‘id’ as primary id, and ‘tablename_id’ as foreign key field.

Student

id

name

address

Othertable

id

student_id

whatever

information

Student has a primary id which is called ‘id’.

Othertable also has a primary id called ‘id’ but in addition it has a foreign key called ‘student_id’ which points to student.id.

Gii should be able to pick that up automatically.

If you follow the conventions you can let Yii handle many details for you.

-> doc/guide/1.1/en/basics.convention

<edit>

I would have thought it was listed in ‘conventions/database’ but it isn’t - which seems like an omission, but do use ‘id’ for primary key and ‘modelname_id’ as foreign key.

Thank jacmoe for your reply to my request. I will make the changes shortly and will see if it works. Will update you. Your response is highly appreciated.

I further need to understand how the relations() work in models. such as BELONGS_TO, HAS_ONE, HAS_MANY, MANY_MANY.

I read documentation but unable to understand it fully.

I would appreciate if someone could explain how to handle and write relations in models with a suitable example of your choice.

Thanking you in advance.

Hi Jacmoe,

I have made the changes as suggested and run the CRUD. It has created files but I don’s see it linking up with the Student table automatically. In relations it shows nothing in both table models.

I thought it will generate Dropdown to select ID from and then add associated record in child table. I am really confused and dont know how does it all work.

Really need to get my hands firm on it ASAP.

If you don’t completely understand the documentation then try it with simple examples.

Look at the blog demo which comes with the framework and to which the guide examples refers to.

Try to modify the demo and try to output some related data via relations in some views.

After that generate a new yii application and create a simple database with some tables e.g. for each relation type a set of two tables expect the MANY_MANY, there you need three. Generate the Models/CRUD and create the relations by hand and try also to output data of related tables via relations.

After that you should understand relations and you could start working on your bigger project.

You will need to generate Models first, then CRUD.

If you only did CRUD, then do Model first.

I am sure it will automatically fill out the correct relations.

That’s a slightly different problem.

Gii doesn’t do that for you.

I believe that Giix does, however. :)

Giix ->

http://www.yiiframework.com/extension/giix

But it’s easy enough to do that manually :)

What you need to do is replace the text field with a dropdown list, like this:


                    	<?php echo $form->dropDownList($model, 'tracker_id', CHtml::listData(

                                    	Tracker::model()->findAll(), 'id', 'name')); ?>



The code I just posted will make a dropdown list where people can select a tracker by ‘name’ which will then insert an ‘id’ into the field.

If you need a query, rather than return all rows, then you can use a function which returns list data:


                    	<?php echo $form->dropDownList($model, 'issue_category_id', $this->getCategorySelectList()); ?>




	public function getCategorySelectList() {

    	$Criteria = new CDbCriteria();

    	$Criteria->select = "name, id";

    	if (isset($_GET['identifier'])) {

        	$Criteria->compare('project_id', $this->getProject($_GET['identifier']), true);

    	}

    	$results = IssueCategory::model()->findAll($Criteria);

    	$category_list = array();

    	foreach ($results as $result) {

        	$category_list[$result->id] = $result->name;

    	}

    	return $category_list;

	}



I would take Giix for a spin if I were you, as it saves you some time, but do experiment. :)

Hey jacmoe, I couldn’t agree more ;)