0

I have following table

create table `groupusers`(
 `id` int not null auto_increment,
 `user` varchar(100) not null,
 `group` varchar(100) not null,
 UNIQUE KEY(`id`),
 PRIMARY KEY(`user`, `group`)
)

My model looks like this,

class Model_Groupuser extends ORM{
    protected $_table_name = 'groupusers';

    public function rules(){
        return array(
                'user' => array(
                        array('not_empty'),
                        array(array($this, 'user_group_not_exists')),
                ),
                'group' => array(
                        array('not_empty'),
                        array(array($this, 'user_group_not_exists')),
                )
        );
    }

    public function user_group_not_exists($param){
        // Need to get other field's value here.
    }
}

Problem is every time user_group_not_exists is called, its called with a single parameter. Either user or group. But I need both to determine if the combination exists in the db already.

How can I get current model's fields' value?

4

2 回答 2

0

$this->object()您可以使用函数获取其他字段值。

public function user_group_not_exists($user_or_group){
    $obj = $this->object();
    $group = $obj['group'];
    $user = $obj['user'];

    // Check if ($group, $user) pair exists in db here
}
于 2013-09-14T08:39:22.787 回答
-1

您还没有真正舒适地命名您的表格列。命名它们usergroup以及关系也会在两者user之间group产生歧义。

正如 kohana 所做的那样,您可以访问表字段、关系等,就好像它是一个对象属性一样。$i_am_lazy = $object-><field,relation,whatever>. 现在您命名了您的字段和关系,因此不清楚您想要获得什么。

您现在可以访问这些 id 的唯一方法如下(或$this->object()其他答案中所述的艰难方式,无论如何都感觉不好):

$user  = $this->user->id;
$group = $this->group->id;

不过,我建议只重命名表格列。

create table `groupusers`(
    `id` int not null auto_increment,
    `user_id` varchar(100) not null,
    `group_id` varchar(100) not null,
    UNIQUE KEY(`id`),
    PRIMARY KEY(`user`, `group`)
)

这样你就可以简单地使用$this->user_idor $this->group_id

于 2013-09-15T08:09:29.867 回答