How to sabe two AR with a single form?

Hi, again… well, I finally developing my applications with Yii (it’s rocks :) )

Well, my probles may be is simple, but it's making me crazy.

I have two db tables Client and Address (in MySQL)

I need to take them separated because other tables are related with address too (eg. Bank)

Well


Client


  client_id

  client_name

  client_lastname

  client_id_address



Address


  address_id

  address_street

  address_number


Both models are already maked.

My Client Model has the following relation

public function relations()


{


	return array(


		'adress'=>array(self::BELONGS_TO,'Address','client_id_address'),


	


	);


}

I have no problem to see the related data on mi client/list (I modifiy that wiew to take address data too)

But my problem is that I want to collect all the data for a client among with his/her address in a single form.

Lets say

Name: (textfield)


Last Name: (textfield)


Street: (textfield)


Number: (textfield)





[SAVE]

I already modify mi ClientController in the following way:

public function actionCreate()

{


	$c=new Client;		


	$c->adress = new Address; // I made the getter and setter for address adding a private member ($_address) to Client and making the correspondients methods.


	


	


	


	if(isset($_POST['Client']))


	{


		//$c->attributes=$_POST['Client'];


		


		$c->client_name = $_POST['client_name'];


		$c->client_lastname = $_POST['client_lastna,e'];


		


		


		


		$c->adress->address_street = $_POST['address_street'];


		$c->adress->address_number = $_POST['address_number'];


		


					


		if ($a->address->save()) { 


		


		


			$a->client_id_address = $a->address->getPrimaryKey();


			


			


			if($a->save())


				$this->redirect(array('show','id'=>$a->client_id));


		}


	}


	$this->render('create',array('client'=>$a));


}

The problem is that when I submit the form, nothing is stored in my db, and I back to the form with the field street marked with css as a required field (but no error)

PLEASE!!! Anybody can help me to solve this problem?

You need the following code:



public function actionCreate()


{


	$client=new Client;


	$address=new Address;


	if(isset($_POST['Client'], $_POST['Address']))


	{


		$client->attributes=$_POST['Client'];


		$address->attributes=$_POST['Address'];


		$valid=$client->validate();


		$valid=$address->validate()&&$valid;


		if($valid)


		{


			$address->save();


			$client->client_id_address=$address->id;


			$client->save();


			//...redirect


		}


	}


	$this->render('create',array(


		'client'=>$client,


		'address'=>$address,


	));


}


You may also refer to: http://www.yiiframew…71.msg4758.html

I will test it now… thanks for the fast answer… you are amazing… thanks again…

It works!!! ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D

THANK YOU VERY MUCH!!!

Another question: the same approach works for 3 or more AR with a single form?

Of course.

Good morning,

I am using Yii 1.0.8 on Xampp 1.7.1 and try to setup 1 form collecting data from 2 models for an adressbook. The actionCreate()-code is based on the example shown above / in cookbook 19.

[list=1]

[*]If I code a rules funtion in both models with more than 1 requiered fields (… array(‘firstName’, ‘lastName’, ‘required’), …) I get this error: “require(lastname.php) [<a href=‘function.require’>function.require</a>]: failed to open stream: No such file or directory”

[*]If I change both rules to … array(array(’.field1.’,’.field2.’,’…’,’…’), ‘required’), … erverything is working fine

[/list]

Would this be the correct Yii-coding or do I misunderstand anything?

In case of 1) we should share/add this information to the cookbook.

Marcus