我正在使用kohana v3.3,我想知道是否有可能在数据透视表中获取/保存其他数据?
让我们以Auth为例:
表:
CREATE TABLE IF NOT NOT EXISTS
roles
(
id
int(11)UNSIGNED NOT NULL AUTO_INCREMENT,
name
varchar(32)NOT NULL,
description
varchar(255)NOT NULL,PRIMARY KEY(
id
),UNIQUE KEY
uniq_name
(name
))ENGINE = InnoDB DEFAULT CHARSET = utf8;
-
CREATE TABLE IF NOT NOT EXISTS
roles_users
(
user_id
int(10)UNSIGNED NOT NULL,
role_id
int(10)UNSIGNED NOT NULL,
date
时间戳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 NOT EXISTS
users
(
id
int(11)UNSIGNED NOT NULL AUTO_INCREMENT,
username
varchar(32)NOT NULL DEFAULT'',
password
varchar(64)NOT NULL,
logins
int(10)UNSIGNED NOT NULL DEFAULT'0',
last_login
int(10)UNSIGNED,PRIMARY KEY(
id
),UNIQUE KEY
uniq_username
(username
),UNIQUE KEY
uniq_email
()ENGINE = InnoDB DEFAULT CHARSET = utf8;
模型
class Model_Auth_Role扩展ORM {
protected $ _has_many = array(
'users' => array('model' => 'User','through' => 'roles_users'),
);
class Model_Auth_User扩展ORM {
protected $ _has_many = array(
'roles' => array('model' => 'Role', 'through' =>
'roles_users'),
);
控制器
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', '=',
'登录') - >发现());
}
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>"; }
}
问题
我有两个问题:
1-如何将“date”设置为“roles_users”表?在控制器action_create()
上2-如何从“roles_users”表中获取“日期”?在控制器action_get()
上提前致谢。
答案 0 :(得分:0)
如果这是你想要的,你可以做两件事:
Model_Roles_User
并使用它来创建关系并获取/设置其属性。这将要求您添加id AUTO_INCREMENT
列,因为Kohana本身不支持复合键;或者做一个好人并扩展kohana的功能来支持它: - )
SELECT
或UPDATE
行使用一些自定义查询这也应该相当容易:
$result = DB::select('date')
->from('roles_users')
->where('user_id', '=', $user_id)
->and_where('role_id', '=', $role_id)
->execute();
问题#1的答案相当容易。数据库已经解决了这个问题! (date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
)