0

I'm back again for another question, i'm trying and trying. But i can't get it fixed. This is my issue;

I have a database table, with a ProvinceID this can alter from 1 to 12. But it's an ID of the province. The provinces are stored with the value pName in the table Provinces

I have the following code to alter the table, and join it with the preferences table

$veiling = ORM::factory('veilingen')
        ->select('veilingvoorkeur.*')
        ->join('veilingvoorkeur', 'LEFT')
        ->on('veilingen.id', '=', 'veilingvoorkeur.vId')
        ->find_all();

    $this->template->content = View::factory('veiling/veilingen')
        ->bind('veiling', $veiling);    

It displays correctly, in the view i have;

echo '<div class="row">';
foreach($veiling as $ve)
{   
echo $ve->provincie;    
}
?>
</div>

it displays the provincie id; but i want to add a function to it; So it will be transformed to a province name. Normally i would create a functions.php file with a function getProvince($provinceId)

Do a mysql query to grab the pName value from Provinces and that is the job. But i'm new to kohana. Is there an option to turn the province id to province['pName'] during the ORM selection part, or do i have to search for another solution. Which i can't find :(

So. Please help me on the road again.

Thanx in advance.

Kind regards, Kevin

Edit: 1:08 I've tried something, and it worked. I used ORM in the view file, for adding a function;

function provincie($id){
$provincie = ORM::factory('provincies', $id);
return $provincie->pName;       
}

But i'm not glad with this way of solution, is there any other way? Or am i have to use it this way?

KevinH
  • 39
  • 1
  • 7

1 Answers1

0

Check out Kohana's ORM relationships. If you use this, you could access the province name by simply calling:

$ve->provincie->name;

The relationship definition could look something like:

protected $_belongs_to = array(
    'provincie' => array(
        'model' => 'Provincie',
        'foreign_key' => 'provincie_id'
    ),
);

Note that if you have a table column called provincie the relationship definition I give above will not work. You'd either have to change the table column to provincie_id or rename the relationship to e.g. provincie_model.

The output you describe when you do echo $ve->provincie; suggests that you store the ID in a column called provincie, thus the above applies to you.

I'd personally go for the first option as I prefer accessing IDs directly with an _id suffix and models without any suffix. But that's up to you.

Edit: If you use Kohanas ORM relationships you could even load the province with the initial query using with() e.g:

ORM::factory('veiling')->with('provincie')->find_all();

This could save you hundreds of extra queries.

AmazingDreams
  • 3,136
  • 2
  • 22
  • 32
  • Lets say the column of the foreign key is called `provincie`, could you not just say `'foreign_key'=>'provincie'` and it would work fine? – kero Nov 04 '13 at 15:39
  • 1
    No, as the relationship is also called `provincie` this would be ambiguous. Kohana would not know whether to get the model or the table column, and picks one of them (the column value if I remember correct). FYI: the `foreign_key` property in a relationship definition refers to a column on the object itself. I believe [my answer here](http://stackoverflow.com/questions/18067064/kohana-3-3-orm-has-many-belongs-to/18250207#18250207) has a more in-depth explanation. – AmazingDreams Nov 04 '13 at 15:53
  • Nice one, i'm going to try this out! =) – KevinH Nov 04 '13 at 18:44