问题
I'm using kohana v3.3, and i would like to know if there is a possibility to get/save another data in the pivot table or not ?
let's take the Auth example :
- So we have 3 tables (roles, users, roles_users) and i added another column "date" on the pivot table
Tables :
CREATE TABLE IF NOT EXISTS
roles(
idint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
namevarchar(32) NOT NULL,
descriptionvarchar(255) NOT NULL,PRIMARY KEY (
id),UNIQUE KEY
uniq_name(name)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
CREATE TABLE IF NOT EXISTS
roles_users(
user_idint(10) UNSIGNED NOT NULL,
role_idint(10) UNSIGNED NOT NULL,
datetimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (
user_id,role_id),KEY
fk_role_id(role_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
CREATE TABLE IF NOT EXISTS
users(
idint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
usernamevarchar(32) NOT NULL DEFAULT '',
passwordvarchar(64) NOT NULL,
loginsint(10) UNSIGNED NOT NULL DEFAULT '0',
last_loginint(10) UNSIGNED,PRIMARY KEY (
id),UNIQUE KEY
uniq_username(username),UNIQUE KEY
uniq_email() ENGINE=InnoDB DEFAULT CHARSET=utf8;
Models
- Model Role
class Model_Auth_Role extends ORM {
protected $_has_many = array(
'users' => array('model' => 'User','through' => 'roles_users'),);
- User Model
class Model_Auth_User extends ORM {
protected $_has_many = array(
'roles' => array('model' => 'Role', 'through' =>'roles_users'),
);
Controller
public function action_create()
{
$model = ORM::factory('user'); $model->username = 'myusername'; $model->password = 'password'; $model->email = 'test@example.com'; $model->save(); **// How can i set the "date" to "roles_users" table ?** $model->add('roles', ORM::factory('role')->where('name', '=','login')->find());
}
puboic function action_get()
{
$users = ORM::factory('user')->find_all();
foreach ($users as $user) { $roles = $user->roles->find_all(); echo $user->email." : <br>"; **// How can i get the "date" from "roles_users" table ?** foreach ($roles as $role) echo " ->".$role->name."<br>"; }}
Questions
I have two questions :
1- How can i set the "date" to "roles_users" table ? on controller action_create()
2- How can i get the "date" from "roles_users" table ? on controller action_get()
Thanks in advance.
回答1:
If that is what you want, you can do two things:
- Create a seperate
Model_Roles_Userand use it to create the relationship and get/set its properties.
This would require you to add an id AUTO_INCREMENT column as Kohana does not natively support compound keys; or be a good man and extend kohana's functionality to support it :-)
- Leave it as is and use some custom queries to
SELECTorUPDATEthe rows
This should also be fairly easy:
$result = DB::select('date')
->from('roles_users')
->where('user_id', '=', $user_id)
->and_where('role_id', '=', $role_id)
->execute();
The answer to question #1 is rather easy. The database already takes care of this! (date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,)
来源:https://stackoverflow.com/questions/21284699/kohana-many-to-many-relationship-has-many-through