Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Best Practice

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1057 Views
  • Stefdv
    Avatar
    Community Member
    110 Posts

    Best Practice Link to this post

    Hello,

    I have a question about Database optimalisation.
    I have a Dog (DO) that has a father and a mother. At the moment i have about 200 Dogs in my database, since every dog has a father and a mother i can build a pedigree...I have 2 options for this ( both working, that's not the problem )

    option 1) In my template;

    <% control MyDad %>
    $PedigreeName
    <% control MyDad %>
    $PedigreeName
    <% control MyDad %>
    $PedigreeName
    etc.etc.
    <% end_control %>
    <% end_control %>
    <% end_control %>

    Option 2 ) create functions in my DataObject Dog

    function getMyDad(){
    $father = dataObject::get_by_id('Dog', $this->MyFatherID);
          return $father;
       }

    function getFather3()
    {
       $father3 = dataobject::get_by_id('Dog',$this->MyDad->MyFatherID);
       return $father3;
    }

    function getFather7()
    {
       $father7 = dataObject::get_by_id('Dog',$this->Father3->MyFatherID);
       return $father7;
    }
    etc etc.

    Then i can use those in my template, filling out my table.
    What is the most effective way to do this?

    One other issue i'm having is that i need to compare every father/mother if they are already in the table since that could be inbred..
    ps @Swaiba; Yes yes, still working on the thing ;)

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Best Practice Link to this post

    Hi Stefdv,

    would it not be easier when having the ancestors as named relations ?

    class Dog extends DataObject {
    static $db = array('Name' => 'Varchar');

    static $has_one = array(
    'Father' => 'Dog',
    'Mother' => 'Dog'
    );
    }

    When retrieving the ancestors you can use the simple notation:

    $father= $myDog->Father();
    $grand_father = $myDog->Father()->Father();

    greets, Rob

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Best Practice Link to this post

    Rob,
    tx for replying

    Well, i have

    static $has_one = array (
    'MyFather' = 'Father',
    "MyMother' = 'Mother');

    Where Father / Mother is a DO that reverts to Dog.
    The way you put it looks more logic. I will try that...I'm in a learning curve and most of the times i do it the hard way

    Can i repeat

    $grand_father = $myDog->Father()->Father();

    endlessly ? like GreatGreatGrandFather would be

    $greatgreatgrand_father = $myDog->Father()->Father()->Father()->Father();

    Anyway of comparing every dog i use in my template pedigree?

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Best Practice Link to this post

    hmm,
    i cannot say why it's necessary to store parents in separate classes ....
    But yes, you can use the Parent() accessor endless - as long there are parents

    E.g.
    When a dog has only 1 parent and you use
    $dog->Parent()->Parent()->Parent() you will get an error because, the second Parent() call does not return a DataObject(set)

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Best Practice Link to this post

    No, i don't know why they have to be a seperate class also I'm gonna do it your way.

    Can't i supress the error with

    <% If Grandfather %>
    $grandfather.$PedigreeName
    <% end_if %>


    ?

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Best Practice Link to this post

    yep - right

    in your model:

    function Grandfather() {
    if( $dad = $this->Father() ) {
    return $dad->Father();
    } else {
    return false;
    }
    }

    in your template:

    <% if Grandfather %>
    <% control Grandfather %>
    $Name
    <% end_control %>
    <% end_if %>

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Best Practice Link to this post

    Okay,

    But i was planning to use the If statement only in my template. Why should i use that in my function also?

    Sorry for asking, i'm just learning

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Best Practice Link to this post

    Hi Stefdv,

    I believe it's best to do the if to return a 'null' instead of an empty, but not null ComponentSet object. Return something instead of nothing would pass the if in the template - when you are not expecting it to.

    ps @Swaiba; Yes yes, still working on the thing ;)

    how many projects ever end?

    1057 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.