views:

10

answers:

1

Hello. I have two tables:

taxonomies
 -id
 -name

taxonomy_type
 -taxonomy_id
 -type_id

I've configured two models:

class Model_Taxonomy{
protected $_has_many = array('types'=>array());
}

class Model_Taxonomy_Type{
protected $_belongs_to = array('taxonomy' => array());
}

*Please note that taxonomy_type is not a pivot table.*

A taxonomy can have multiple types associated.

Then, what I'm trying to do is get all taxonomies that belong to a given type id. This is would be the SQL query I would execute:

SELECT * FROM taxonomies, taxonomy_type WHERE taxonomy_type.type_id='X' AND taxonomies.id=taxonomy_type.taxonomy_id

I've tried this:

$taxonomies = ORM::factory('taxonomy')
    ->where('type_id','=',$type_id)
    ->find_all();

Obviously this doesn't work, but I can't find info about how execute this kind of queries so I have no clue.

A: 

type_id column is a PK of taxonomy_type table, am I right? So, you have one (unique) taxonomy_type record, and only one related taxonomy object (because of belongs_to relationship). Instead of your:

get all taxonomies that belong to a given type id

it will be a

get taxonomy for a given type id

biakaveron
Not exactly, PK of taxonomy_type table are both fields type_id and taxonomy_id. So I really want to get all taxonomies that has the type_id assigned. So, a taxonomy can have multiple types and a type can have multiple taxonomies associated.
clinisbut
consider type_id as a simple field
clinisbut