[eluser]WanWizard[/eluser]
That won't work, as Datamapper requires you do define keys with an '_id' suffix (as stated in the manual):
Code:
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`to_id` int(11) NOT NULL,
`from_id` int(11) NOT NULL,
`subject` varchar(50) NOT NULL,
`message` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
You can then define the Message model as
Code:
class Message extends DataMapper {
$has_one = array(
'from' => array(
'class' => 'user',
'other_field' => 'from'
),
'to' => array(
'class' => 'user',
'other_field' => 'to'
)
);
}
class User extends DataMapper {
$has_many = array(
'from' => array(
'class' => 'message',
'other_field' => 'from'
),
'to' => array(
'class' => 'message',
'other_field' => 'to'
)
);
}
You can then retrieve data using
Code:
$user = new User();
$message = new Message();
// get all messages send by user 1
$user->get_by_id(1)->from->get();
// get a message and it's from and to info
$message->include_related('from')->include_related('to')->get_by_id(1);