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)











Both models are already maked.

My Client Model has the following relation

public function relations()


	return array(





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)


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.








		$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();









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']))



















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


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.


[*]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


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.