Nanne Huiges Mon Feb 13 08:01:27 -0500 2012

Subject: Creating new object trough Association

In a model (lets say 'person') I have an association (lets call it "company").

When a person does not have a company, but I want to link it to one, I might need to create the company-object. Now I want to do this with the model the association knows.

To clarify: I could of course do this:

1 $myCompany = new ARCompany();

But I've already defined what class the company has in the association. So above is not desirable because now I use the classname in two places.

I'm looking for something like this, but I don't think below is legal:

1 //$person is a AR model
2 $myCompany = new $person->company;

Now I have defined the type of $company in only one place (the association), and if it changes there is only one place it has to be changed.

(Background: in my case I have two different "persons" that are children of an abstract parent-class. I want the abstact parent to point to the childrens associated company, so therefore I need to use the associations class, not a hardcoded class)

Nanne Huiges Wed Feb 15 03:08:36 -0500 2012

Also crossposted this to stackoverflow ( ). While I was at it, created a phpactiverecord tag as well :)