简体   繁体   English

表的PK条件属于PHP-Activerecord

[英]Conditioning by PK of the table belonged to in PHP-Activerecord

I'm fairly new using PHP-Activerecord, and I am not sure if this is do-able. 我是使用PHP-Activerecord的新手,我不确定这是否可行。

Three models; 三种模式; Foo, Bar and User. Foo,Bar和User。

Foo 1-0 Bar (Foo always assigned to Bar, Bar can have 1 or 0 Foo). Foo 1-0 Bar(Foo始终分配给Bar,Bar可以有1或0 Foo)。

Bar N-1 User (Bar always assigned to User, User can have many Bar). 栏N-1用户(栏始终分配给用户,用户可以有多个栏)。

class Foo extends Model
{
    static $belongs_to = array(
        array('bar',
            'class_name' => 'Bar')
    );
}

class Bar extends Model
{
    static $belongs_to = array(
        array('user', 
            'class_name' => 'User')
    );
}

class User extends Model
{
    static $has_many = array(
        array('bar',
            'class_name' => 'Bar')
    );
}

Works if I do: 可以的话,如果我这样做:

$bars = Bar::find('all', array(
    'user_id' => $userId
);

But I want the Foo's, not the Bars. 但是我想要Foo,而不是Bars。 So I've tried ... 所以我尝试了...

$foos = Foo::find('all', array(
    'bar.user_id' => $userId
);

But it does not work; 但这是行不通的。 column user_id is not found. 找不到列user_id。

How can I apply this condition? 如何应用此条件?

You are looking for the 'through' relation. 您正在寻找“直通”关系。

This should work to get the Foo's of User through bar: 这应该可以通过bar获得用户的Foo:

class User extends Model
{
    static $has_many = array(
        array(
            'bar',
            'class_name' => 'Bar'
        ),
        array(
            'foo',
            'class_name' => 'Foo',
            'through' => 'Bar' 
        )
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM