How can I use a different id field for one of my CakePHP model associations?

Posted by gacrux on Stack Overflow See other posts from Stack Overflow or by gacrux
Published on 2010-04-06T02:06:28Z Indexed on 2010/04/06 2:23 UTC
Read the original article Hit count: 326

Filed under:
|
|

I have an one to many association in which a Thing can have many Statuses defined as below:

Status Model:

class Status extends AppModel
{
    var $name = 'Status';

    var $belongsTo = array(
        'Thing' => array(
            'className' => 'Thing',
            'foreignKey' => 'thing_id',
    );
}

Thing Model:

class Thing extends AppModel
{
    var $name = 'Thing';    

    var $belongsTo = array(
        // other associations
    );

    var $hasMany = array(
        'Status' => array(
            'className' => 'Status',
            'foreignKey' => 'thing_id',
            'dependent' => false,
            'order' => 'datetime DESC',
            'limit' => '10',
        ),
        // other associations
    );
}

This works OK, but I would like Thing to use a different id to connect to Status. E.g. Thing would use 'id' for all of it's other associations but use 'thing_status_id' for it's Status association.

How can I best do this?

© Stack Overflow or respective owner

Related posts about php

Related posts about cakephp