Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Best Practice


Go to End


8 Posts   1420 Views

Avatar
Stefdv

Community Member, 110 Posts

21 April 2011 at 4:08am

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 ;)

Avatar
rob.s

Community Member, 78 Posts

21 April 2011 at 6:36pm

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

Avatar
Stefdv

Community Member, 110 Posts

21 April 2011 at 9:48pm

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?

Avatar
rob.s

Community Member, 78 Posts

21 April 2011 at 10:30pm

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)

Avatar
Stefdv

Community Member, 110 Posts

21 April 2011 at 10:37pm

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 %>

?

Avatar
rob.s

Community Member, 78 Posts

21 April 2011 at 10:41pm

Edited: 21/04/2011 10:44pm

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 %>

Avatar
Stefdv

Community Member, 110 Posts

21 April 2011 at 11:13pm

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

Avatar
swaiba

Forum Moderator, 1899 Posts

22 April 2011 at 7:10am

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? :)