【发布时间】:2014-02-12 14:46:13
【问题描述】:
我正在使用 kohana v3.3,我想知道是否有可能在数据透视表中获取/保存其他数据?
让我们以 Auth 为例:
- 所以我们有 3 个表(roles、users、roles_users),我在数据透视表上添加了另一列 "date"
表格:
如果不存在则创建表
roles(
idint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
namevarchar(32) 非空,
descriptionvarchar(255) 非空,主键 (
id),唯一键
uniq_name(name)) ENGINE=InnoDB 默认字符集=utf8;
--
如果不存在则创建表
roles_users(
user_idint(10) UNSIGNED NOT NULL,
role_idint(10) UNSIGNED NOT NULL,
datetimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,主键 (
user_id,role_id),密钥
fk_role_id(role_id)) ENGINE=InnoDB 默认字符集=utf8;
--
如果不存在则创建表
users(
idint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
usernamevarchar(32) NOT NULL DEFAULT '',
passwordvarchar(64) 非空,
loginsint(10) UNSIGNED NOT NULL DEFAULT '0',
last_loginint(10) 未签名,主键 (
id),唯一键
uniq_username(username),唯一键
uniq_email() ENGINE=InnoDB 默认字符集=utf8;
模型
- 模特角色
类 Model_Auth_Role 扩展 ORM {
受保护的 $_has_many = 数组(
'users' => array('model' => 'User','through' => 'roles_users'),);
- 用户模型
类 Model_Auth_User 扩展 ORM {
受保护的 $_has_many = 数组(
'roles' => array('model' => 'Role', 'through' =>'roles_users'),
);
控制器
公共函数 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', '=','登录')->find());
}
公共函数 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- 如何将“日期”设置为“roles_users”表?关于控制器 action_create()
2- 如何从“roles_users”表中获取“日期”?关于控制器 action_get()
提前致谢。
【问题讨论】:
标签: php orm many-to-many kohana relationship