2014-02-10 18 views
0

我是相当新的使用PHP-Activerecord,我不知道这是否可行。调整由表中的PK属于PHP-Activerecord

三种型号;美孚,酒吧和用户。 (Foo总是分配给Bar,Bar可以有1或0 Foo)。

酒吧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') 
    ); 
} 

作品,如果我做的:

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

但我想富的,而不是酒吧。 所以我试过...

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

但它不起作用;找不到列user_id。

我该如何应用此条件?

回答

0

您在寻找'through'的关系。

这应该通过酒吧来获得用户的Foo的:

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