Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
extending Model which extends Orm/Model
  • My question is more about architecture.
    I've got an ORM-model Model_Condition for example. And there are another models which extend it: Model_Condition_Category, Model_Condition_Price, etc.

    class Model_Condition extends Orm\Model
    {
    protected static $_properties = array('id', 'type', 'operator', 'value');
    }


    Those models store their data in the same table named 'conditions', but have some methods different from Model_Condition :

    class Model_Condition_Category extends Model_Category
    {
    public function set_form_fields() {}
    public function __toString() {}
    public function execute() {}
    }


    There is a Model_Project which has one-to-many relations to Model_Condition.

    Is it possible to get models by relations as final condition classes?
    I came up with a dirty way, but not sure if it is not head-shot:

    class Model_Condition extends Orm\Model
    {
    public function as_class($type)
    {
    $class = 'Model_Condition_'.ucfirst($type);

    if ( ! class_exists($class))
    {
    throw new \FuelException("Class {$class} not found!");
    }

    return $class::forge( $this->to_array() );
    }
    }

    $condition = $project->conditions[0];
    $condition = $condition->as_class( $condition->type );


    I can see an obvious architecture flaw there, but on the other hand it looks like typical issue.
    How is it normally done?
  • You're the first I know of that uses multiple ORM models for the same table.

    Normally, you have an ORM model for each table, and you have non-ORM model classes functional to your app, which is turn use one or more ORM models to interact with the database. This way, you don't have functional methods in an ORM model, and you don't have the problem you run into.

    Of the top of my head I can't think of a better solution for your problem.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion