Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
model self relation
  • hi,
    i have category table
    categories => {id, name, parent}
    (parent is 0 or id value)
    how write this relation in model?
    how to use that model for view sub categories?
    thanks
  • HarroHarro
    Accepted Answer
    You mean you have a table that has a recursive relation, i.e. one-to-many relation to itself?
  • i have table row like this :
    id        name    parent
    1        a        0
    2        b        0
    3        c        1
    4        d        1
    5        e        3
    6        f         4
    7        g        4
    ...


    and need to be back like this :
    array (
        array(1, a, 0,     array(
                            array(3, c, 1,     array(
                                                array(5,e,3)
                                            )
                            ),
                            array(4, d, 1,     array(
                                                array(6,f,4),
                                                array(7,g,4)
                                            )
                            
                            )
                        )
        ),
        array(2, b, 0)
    );

    Like tree : (countinue to any deep level)
    a    -> c
                 -> e
          -> d
                -> f
                -> g
    b


  • HarroHarro
    Accepted Answer
    If you want tree's you'd better look at nested sets.

    You can define self-relations without problems in ORM, but you can not construct a query that automatically and recusively returns the complete tree regardless of the depth.
  • hi Harro
    can you give me sample of self-relation please?
    how can i use model field array ($_properties = array('field1', 'field2', ...)) as class static variable like
    public static $fields1;
    public static $fields2;
    ...
    thanks

  • HarroHarro
    Accepted Answer
    Self relation works like any other relation, the only difference is the model you relate to, which is the same model:

    class SelfRelation extends \Orm\Model
    {
        /**
         * @var array    model properties
         */
        protected static $_properties = [
            'id'                => [
                'label'            => 'primary key',
                'data_type'        => 'int',
            ],
            'self_id'                => [
                'label'            => 'foreign key',
                'data_type'        => 'int',
            ],
        ];

        /**
         * @var array    belongs_to relationships
         */
        protected static $_belongs_to = [
            'parent' => [
                'model_to'            => '\\Model\\SelfRelated',
                'key_from'            => 'self_id',
                'key_to'            => 'id',
            ],

        /**
         * @var array    has_many relationships
         */
        protected static $_has_many = [
            'children' => [
                'model_to'            => '\\Model\\SelfRelated',
                'key_from'            => 'id',
                'key_to'            => 'self_id',
            ],
        ];

    }

    And I don't understand your second question. Can you explain what exactly you want? The $_properties array is just the list of fieldnames in the object.
  • thanks.
    i need to define fieldnames as class variable for direct access by IDE (netbeans)
    class Model_Sample extends \Orm\Model
    {
          public $field1;
    }

    and in controller
    class Controller_Sample extends \Controller_Dashboa
    {
         public function action_index()
         {
            $test = Model_Categories::forge();
            $test->field1 = \Input::post('value1'); (autocomplete with ide like doctrine2 orm)
         }
    }
    very help for table with many columns;
    very fast develop;


  • HarroHarro
    Accepted Answer
    You can't.

    As soon as you define a model property with the same name as a ORM column, the ORM breaks.

    You need something like this: http://stackoverflow.com/questions/21805555/netbeans-code-completion-for-pseudo-properties-and-methods-made-from-magic-funct

Howdy, Stranger!

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

In this Discussion