I have a model with a relationship, and I want to instantiate a new relationship type object.
Example: a person has a company, and I have a person-object: now I want to create a company-object.
The companyobject class is defined in relation, so I don’t think I need to “know” this class, but should I ask the personal object to provide me with a new instance of the Company type? But I dont know how.
This - I think - is the same question as the New Model Object through association , but I use PHPActiveRecord , not ruby.
<h / "> The reason for this: I have an abstract superclass person , and two children have their own attitude to the type of corporate object. I need to be able to create an instance of the correct class in an abstract person.
The workaround is to get it directly from the static $has_one :
$class = $this::$has_one[1]['class_name']; $company = new $class;
a hard-coded number, of course, can be eliminated by looking for the association name in the array, but it's still pretty ugly.
If someone knows how this is implemented in Ruby, and how is the implementation of phpactiverecord different, can I get some ideas from there?
Some tests showed that although “searching for my class name in an array” looks strange, it has no effect on performance and is quite functional in use.
Nanne source share