문제

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?

도움이 되었습니까?

해결책

You can get other fields value using $this->object() function.

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
}

다른 팁

You have not really named your table columns comfortable. Naming them user and group and the relations also user and group creates ambiguity between the two.

As kohana does this great thing where you can access table fields, relationships etc. as if it's an objects property. $i_am_lazy = $object-><field,relation,whatever>. Now you named your fields and relations such that it is not clear what you are trying to get.

The only way you can access these id's now is like the following (or the hard way through $this->object() as stated in the other answer, both don't feel good anyway):

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

Though, I recommend just renaming the table columns.

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`)
)

That way you can simply use $this->user_id or $this->group_id.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top