[Solar-talk] _hasOne Help

Rodrigo Moraes rodrigo.moraes at gmail.com
Wed Nov 14 09:36:41 CST 2007


On Nov 14, 2007 1:13 PM, Raymond Kolbe wrote:
> Hey folks,
>  I am in need of some help with doing a _hasOne relationship in my model. I
> do not have the specifics with me at the moment but in a simple world let's
> say my DB is setup like this
>
>  COMPANY (the table name)
>  id (PK)
>  name
>  market (FK -> MARKET_id)
>
>  MARKET
>  id (PK)
>  name
>
>  Now, with the example on the wiki, _hasMany is used and there are 3 tables
> involved. I do not wish to create another table for the above relationship
> (and I'm sure I do not have to).

Here's a quick example on how to setup the schema above:

In COMPANY:

        $this->_hasOne('market', array(
            'foreign_class' => 'MARKET',
            'native_col'    => 'MARKET_id',
        ));

In MARKET:

        $this->_belongsTo('company', array(
            'foreign_class' => 'COMPANY',
            'foreign_col'    => 'MARKET_id',
        ));

You could also have a foreign key in MARKET instead of one in COMPANY:

>  COMPANY (the table name)
>  id (PK)
>  name
>
>  MARKET
>  id (PK)
>  name
>  company (FK -> COMPANY_id)

Then the setup would be:

In COMPANY:

        $this->_hasOne('market', array(
            'foreign_class' => 'MARKET',
            'foreign_col'    => 'COMPANY_id',
        ));

In MARKET:

        $this->_belongsTo('company', array(
            'foreign_class' => 'COMPANY',
            'native_col'    => 'COMPANY_id',
        ));

I'm not sure, but I think that you need to define the relationship in
both models (also, it makes sense :).

-- rodrigo


More information about the Solar-talk mailing list