Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM - Multiple model in one relationship
  • Hi,

    I don't if it's possible with this ORM. But how i can have multiple possibility model in one relationship ?

    For example, my 1st model is named "Column".

    And i have other model named "Element_HTML", "Element_Slider", "Element_Block"

    I want to do this :
    ------------------------------------------
    <?php

    $columnOne = \Model_Column::forge();
    $columnTwo = \Model_Column::forge();

    // "element" is the name of the relationship (Has_One)
    $columnOne->element = \Model_Element_HTML::forge();

    $columnTwo->element = \Model_Element_Block::forge();

    ----------------------------------


    I don't know if it's a design pattern, or if it's possible natively in Fuel ORM ?


    Other question : Is it possible to do a relation Belongs to -> Belongs to ?
  • To start with your last one:

    What do you want to achive with that? The "Belongs_to" side is the record that has the foreign key, and there can be only one. So you can only have: has_one -> belongs_to, and has_many -> belongs_to.

    And no, the current ORM doesn't support polymorphic relations, a release is hard-coded between two models.
  • Thanks Harro.

    For my last question, it's if the first question is not possible. I've do this :

    Model_Column
    - id
    - id_element

    Model_Element
    - id 
    - type (html or block or slider)
    - id_html (nullable)
    - id_element_block (nullable)
    - id_element_slider (nullable)

    Model_Element_HTML
    - id
    - title

    etc.

    And i do this :

    $columnOne = \Model_Column::forge();
    $columnTwo = \Model_Column::forge();

    $elementOne = \Model_Element::forge();
    $elementOne->type = "html";
    $elementOne->html =  \Model_Element_HTML::forge();
    $columnOne->element = $elementOne;

    $elementTwo = \Model_Element::forge();
    $elementTwo->type = "block";
    $elementTwo->block =  \Model_Element_Block::forge();
    $columnTwo->element = $elementTwo;


    ------

    I don't know if it's better to place the foreign key in "Model_Element", or in "Model_Element_HTML" like that :

    Model_Element_HTML
    - id
    - title
    - id_element

    I think the first solution is better for performance. But it's strange to say "Model_Element" Belongs to "Model_Element_HTML"

    It's for this reason i wanted to set the foreign in Model_Element and Model_Element_HTML like that :

    Model_Element
    - id 
    - type
    - id_html (nullable) (Model_Element BelongsTo Model_Element_HTML)
    - id_element_block (nullable)
    - id_element_slider (nullable)

    Model_Element_HTML
    - id
    - title
    - id_element (Model_Element_HTML BelongsTo Model_Element)

    -----

    But i think it's better to put only the foreign key in Model_Element. I'm right ?
  • Hm, finally i think this design pattern is better :

    Model_Element
    - id
    (Model_Element HasOne Model_Element_HTML) (can be null)
    (Model_Element HasOne Model_Element_Block) (can be null)
    (Model_Element HasOne Model_Element_Slider) (can be null)

    Model_Element_HTML
    - id 
    - title
    - id_element (BelongsTo Model_Element)

    Model_Element_Block
    - id
    - title
    - ...
    - id_element (BelongsTo Model_Element)

    Model_Element_Slider
    - id
    - title
    - ...
    - id_element (BelongsTo Model_Element)

    -----

    But what about performance ? Knowing that i would more $element->html than the reverse
  • HarroHarro
    Accepted Answer
    If you can have multiple Model_Element_HTML per Model_Element, then it is a one-to-many relation, and the foreign key (the PK of Model_Element) should be in Model_Element_HTML.

    Which gives you:

    Model_Element has_many Model_Element_HTML
    Model_Element_HTML belongs_to Model_Element
  • No just one Model_Element_HTML. But i can have multiple Model_Element per Model_Column :

    Model_Column has_many Model_Element
    Model_Element belongs_to Model_Column

    Model_Element has_one Model_Element_HTML
    Model_Element_HTML belongs_to Model_Element

    Model_Element has_one Model_Element_Block
    Model_Element_Block belongs_to Model_Element

    etc..

    I think it's correct. Thanks Harro !
  • Yes, correct.

Howdy, Stranger!

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

In this Discussion