One way would be to have all the possible item types (ie NewsItem,
EventItem) associated, with different conditions.  Only those that
match should be loaded (but you will probably get all the keys, and
it'll be a complicated and possibly slow JOIN generated).

class Item extends AppModel
{
        var $name = 'Item';
        var $belongsTo = array(
                'EventItem' => array('className'  => 'EventItem', 'foreignKey' 
=>
'type_id', 'conditions' => 'Item.type = 0'),
                'NewsItem' => array('className'  => 'NewsItem', 'foreignKey' =>
'type_id', 'conditions' => 'Item.type = 1'),
        );
}

other than that you can have something in the afterFind of your Item
model to make a query and load the correct items data.  Something
like:

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

        function afterFind( $results )
        {
                for ($i = 0; $i < sizeof($results); $i++) {
                        // check if this is a model, or if it is an array of 
models
                        if ( isset($results[$i][$this->name]) ){
                                // this is the model we want, see if it's a 
single or array
                                if ( isset($results[$i][$this->name][0]) ){
                                        // run on every model
                                        for ($j = 0; $j < 
sizeof($results[$i][$this->name]); $j++) {
                                                $this->_attachAttributes( 
&$results[$i][$this->name][$j] );
                                        }
                                } else {
                                        $this->_attachAttributes( 
&$results[$i][$this->name] );
                                }
                        }
                }

                return $results;
        }

        function _attachAttributes( &$data_row )
        {
                if ( !empty($data_row['id']) ){

                        switch( $data_row['id'] ){
                                case 0:
                                        $event_item = 
$this->EventItem->find('EventItem.id'=>
$data_row['type_id']);
                                        $data_row['EventItem'] = 
$event_item['EventItem'];
                                        break;
                                case 0:
                                        $event_item = 
$this->NewsItem->find('NewsItem.id'=>
$data_row['type_id']);
                                        $data_row['NewsItem'] = 
$event_item['NewsItem'];
                                        break;
                        }

                }
        }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to