has_many: updating relationships

2 views
Skip to first unread message

tom

unread,
Oct 26, 2007, 8:02:52 AM10/26/07
to Akelos PHP Framework
Hi,

Is there documentation about how to set a child/parent relationship
between two model objects and update it in the database?

My models look like this:

class Customer extends ActiveRecord
{
var $has_many = array( 'people' => array('order' => 'list_position',
'dependent' => 'destroy'));
...

}

class Person extends ActiveRecord
{
var $belongs_to = 'customer';
var $acts_as = array('list' => array('scope' => 'customer_id',
'column' => 'list_position'));
...
}

So, a customer object has_many Persons.

I found those nice assign() methods, but e.g.

if ($this->my_customer->isValid() && $this->my_person->isValid() ) {
$this->my_person->customer->assign( $this->my_customer );
$this->my_person->save();
$this->my_customer->save();
}

just won't update the table... I had to do it by hand ( $this-
>my_person->customer->id = $this->my_customer->id ).

Now when and how do the foreign key columns get saved?

Reards

TS

Kaste

unread,
Oct 26, 2007, 4:14:38 PM10/26/07
to akelos-f...@googlegroups.com
>
> Hi,

Hi,

> So, a customer object has_many Persons.

quick & dirty reply:

$my_custom =& new Customer("put something in here");
$my_custom->person->create("dadadum");
$my_custom->save();

OR

$Ellen =& new Person("...");
$my_costum->person->add($Ellen);
$my_costum->save();

remember: you can use new, create or find to get an Active Record Object!


hm, that wasnt very explicit, anyway
Grüße
Kaste


Reply all
Reply to author
Forward
0 new messages