Relational Active Record Question

Hello all,

I am very new to yii as I am trying to build an application I have run into a road block of my understanding of the framework. To find an answer to the problem I am having I have been researching this problem and can’t seem to find the answer. I am sure the solution is simple, I just have not been able to find it. I have 2 tables Clients and History with a one to many relation. In a view I have the clients detail and a table with a list of history and everything is running fine, I can view the history records update both history and client details, even breadcrumbs in history leading back to the clients detail page. My problem is when I go to add a new history record from the view, no information is being passed to the new record and because of this my breadcrumbs will not function on the history/create page and the foreign key is not being passed. The only way for me to add a new history record is to enter the FK manually.

I am not sure how to pass the active record for the client to the history record.

I have tried calling the relation in the history _form and I have tried passing it from the clients record. I have also tried setting it up to populate the FK with Before Save and also in the controller I have tried to populate it in the create function.

Here are my relation setups with I believe are correct because everything else functions fine.

public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'clients' => array(self::BELONGS_TO, 'Clients', 'clients_id'),


	);


}





public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'clientsHasProducts' => array(self::HAS_MANY, 'ClientsHasProducts', 'clients_id'),


		'histories' => array(self::HAS_MANY, 'History', 'clients_id'),


	);


}

I am not looking for someone to write it for me, just some guidance on how I should be setting this up. Thank you in advance for your time.

Michael

If i am correct,you are calling create history from view page so you need to pass the client’s id when you click on the addnew link for whom u want to create new history. And in the history controller,action create,you can retrieve the id of the client using $_GET[‘id’] and assign it as required.


CHtml::link('<i class="icon-plus"></i>Add New history',Yii::app()->createUrl("history/create",array("id"=>$model->id));

Chirag,

Thank you for your reply and to let you know it worked. I was already passing the AR like this;

array(‘label’=>‘Create History’,‘url’=>array(‘history/create’,‘id’=>$model->id)),

So all I needed to do was to add the $_GET(‘id’) to my history controller and everything worked fine. I have one question though, is this the standard way to do this in YII? Thank you again for your time.

Michael