Taylor Otwell Wed Dec 22 09:31:31 -0500 2010

Subject: Saving related objects?

How does PHP ActiveRecord handle saving related objects. For example, let's say I have a "User" that has many "Phone Numbers". When I call "$user->save()", does it save the phone numbers associated with that user, or does each object have to be persisted individually?

If there is a section in the docs about this a link would be greatly appreciated!

Thanks!


Andrea Franceschini Tue Dec 28 06:25:43 -0500 2010

Hi. I'd really like this feature, and as far as I can see, it seems to be quite trivial to be implemented, though it'd be an inefficient implementation. Basically, my idea is going at https://github.com/kla/php-activerecord/blob/master/lib/Model.php#L757 and adding a couple (or more) of foreach over the owned associations, and save them whenever they're dirty.

It'd be more efficient to build a sql query to perform this stuff in a single call to the database, but I'm not (yet) enough familiar with SQLBulder's and the whole project's internals to propose such a solution.

While we're discussing this, it seems to me that there's no support for nested attributes. Is this correct?

Taylor Otwell Tue Dec 28 14:04:49 -0500 2010

I think your idea would work. But, like you said, I'm not sure it would be the most efficient, especially for large datasets. I would be interested to hear one of the author's thoughts on this.

What do you mean by nested attributes?

(1-2/2)