Kohana多对多关系“具有许多-通过"关系. [英] Kohana many-to-many relationship "has many - through"
问题描述
我正在使用kohana v3.3,我想知道是否有可能在数据透视表中获取/保存其他数据?
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 ?
以Auth为例:
- 所以我们有3个表(角色,用户,roles_users),我在数据透视表中添加了另一列日期"
表格:
如果不存在则创建表
roles
(
id
int(11)UNSIGNED NOT NULL AUTO_INCREMENT,
id
int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
name
varchar(32)NOT NULL,
name
varchar(32) NOT NULL,
description
varchar(255)NOT NULL,
description
varchar(255) NOT NULL,
主键(id
)
唯一键uniq_name
(name
)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
如果不存在则创建表roles_users
(
user_id
int(10)UNSIGNED NOT NULL,
user_id
int(10) UNSIGNED NOT NULL,
role_id
int(10)UNSIGNED NOT NULL,
role_id
int(10) UNSIGNED NOT NULL,
date
时间戳记NOT NULL DEFAULT CURRENT_TIMESTAMP,
date
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
主键(user_id
,role_id
),
键fk_role_id
(role_id
)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
如果不存在则创建表users
(
id
int(11)UNSIGNED NOT NULL AUTO_INCREMENT,
id
int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
email
varchar(254)NOT NULL,
email
varchar(254) NOT NULL,
username
varchar(32)NOT NULL DEFAULT'',
username
varchar(32) NOT NULL DEFAULT '',
password
varchar(64)NOT NULL,
password
varchar(64) NOT NULL,
logins
int(10)UNSIGNED NOT NULL DEFAULT'0',
logins
int(10) UNSIGNED NOT NULL DEFAULT '0',
last_login
int(10)未签名,
last_login
int(10) UNSIGNED,
主键(id
)
唯一键uniq_username
(username
),
唯一键uniq_email
(email
)
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
模型
- 模特角色
Model_Auth_Role类扩展ORM {
class Model_Auth_Role extends ORM {
受保护的$ _has_many = array(
protected $_has_many = array(
'users' => array('model' => 'User','through' => 'roles_users'),
);
- 用户模型
- User Model
Model_Auth_User类扩展了ORM {
class Model_Auth_User extends ORM {
受保护的$ _has_many = array(
protected $_has_many = array(
'roles' => array('model' => 'Role', 'through' =>
'roles_users'),
'roles_users'),
);
控制器
公共函数action_create()
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());
'login')->find());
}
公共函数action_get()
puboic function action_get()
{
$ users = ORM :: factory('user')-> find_all();
$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()
1- How can i set the "date" to "roles_users" table ? on controller action_create()
2-如何从"roles_users"表中获取日期"?在控制器上action_get()
2- How can i get the "date" from "roles_users" table ? on controller action_get()
谢谢.
推荐答案
如果这是您想要的,则可以做两件事:
If that is what you want, you can do two things:
- 创建一个单独的
Model_Roles_User
并使用它来创建关系并获取/设置其属性.
- Create a seperate
Model_Roles_User
and use it to create the relationship and get/set its properties.
这将要求您添加id AUTO_INCREMENT
列,因为Kohana本身不支持复合键.或者成为一个好人,并扩展kohana的功能以支持它:-)
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 :-)
- 将其保留不变,并使用一些自定义查询来
SELECT
或UPDATE
行
这也应该很容易:
$result = DB::select('date')
->from('roles_users')
->where('user_id', '=', $user_id)
->and_where('role_id', '=', $role_id)
->execute();
第一个问题的答案很简单.数据库已经解决了这个问题! (date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
)
The answer to question #1 is rather easy. The database already takes care of this! (date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
)
这篇关于Kohana多对多关系“具有许多-通过"关系.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!