Reputation: 9480
I have the following tables:
users { id, name }
events { id, name }
user_events { id, date, user_id, event_id }
user_summaries { id, user_id, event_id, qty }
Table user_events
stores all user events (there are many of the same types at different times) and user_summaries
stores how many of each type of events occured to each user. The former gets only INSERT
queries and the latter mostly UPDATE
ones.
Of course both user_events
and user_summaries
have foreign keys to link with users
and events
tables.
I want to represent this relation in my Models in Kohana. I have experience with foreign keys, but it's my first approach to both Kohana and ORM and I'm not sure really how to define my models there to represent these relations.
What I want to be able to do is basically this:
// I'm able to do this.
$user = ORM::factory('user', 1);
$user_events = $user->events->find_all();
$user_summaries = $user->usersummaries->find_all();
// I'm able to do this, too.
$event = ORM::factory('event', 1);
$event_users = $event->users->get_all();
// I don't know how to be able to do this:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
$event_details = $user_event->event->find();
My current models look like this:
# classes/model/user.php
class Model_User extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/event.php
class Model_Event extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/userevent.php
class Model_Userevent extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
# classes/model/usersummary.php
class Model_Usersummary extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
Currently calling:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
returns always the first user from users
table, even though userevent
has different user_id.
I presume I need to change something in Model_userevent
but I'd use with some help.
NB. I'm using Kohana 3.0.8.
Upvotes: 1
Views: 1532
Reputation: 5483
Use $user_event->user
(without find()
). Models look fine, but may be you should manually define related models for usersummaries
and userevents
relations (Ko3 inflector may works wrong for these names)
Upvotes: 1