powerpak / dakota

A lightweight Active Record implementation for PHP5, built on top of Idiorm
7 stars 3 forks source link

Create record based on association #1

Open Surt opened 13 years ago

Surt commented 13 years ago

Hi, first: love your Idiorm-Paris workaround

I'm triying to create a new record on an associated Model. I'm triying this way:

having class Content extends Model { public function languages() { return $this->has_many('Content_Language', 'content_fk'); } }

and belongs_to in Content_Language

I'm triying to insert a language inside a content :

$content = new Content(); $content->find_one(1)->languages()->create(array('title'=>"test"))->save();

it works saving a new Content_Language but it dosn't create the association, so the parent_fk is not filled, wich would rock. Is this possible?

powerpak commented 13 years ago

Hi Surt,

Just as a side note: it helps to provide a complete code sample with your table structure just so I can quickly replicate what you are doing.

You're right, it would be lovely in general if relationships were "smarter." Right now they are not, because of the basis on the simple behavior of Paris they are at best functions that alias to other models. Even if we think about Kohana, it also provides the table relationship functions as mostly a one-way bridge to the other model--once you use them, you're in the other model and you don't have any special context that ties you back to the original object. It's an interesting idea to contemplate though.

I actually didn't intend any usage of create() to be exposed in Dakota, it happens to be only because Idiorm's public API still pokes through the Model class. To me, the only idiomatic way of creating an ActiveRecord row should be through the instantiation of an object, like new Content_Language; or the factory method. But then you would have to set the foreign key manually.

The closest thing Kohana had to this, in terms of smartly setting FK's within a relationship, was the add() method for many to many relationships. I can see a form of it being used here, like

$language = new Content_Language;
$language->title = "test";
$language->save(); // FK is still null
$content->find_one(1)->add($language); // FK on $language is now set and re-saved

where the add() method, instead of adding the entry in the join table for a many-to-many, sees that this is a one-to-many and sets the FK on the correct row. (Kohana required you to set your own FK's for all relationships other than many-to-many.) That would require statically annoted information for all relationships on each model, like Kohana, which hasn't been added yet. I'd like to do that at some point, as a first step toward anything like this, because I see the value in it. Someday!

Surt commented 13 years ago

Hi powepak, thanks so much for taking time in it! Actually I'm setting the 'related' keys manually, it is really easy so theres no need to add more complexity to the lib.

By the way, some "pull requests" from Paris and Idiorm makes my life even easier:

https://github.com/sandermarechal/idiorm/commit/f9af1ffce3b01e6e87ded22cc5903c0bf253fbc1 https://github.com/sandermarechal/idiorm/commit/2463ca7ace76a3b8bb9216910ca6f6d8e3f40e15

Thank again!