我的PHP ActiveRecord有多少通过关系有什么问题?

时间:2011-08-29 17:26:15

标签: php activerecord

当我运行以下代码时:

$venue_test = VenuePage::find(104);
$cats = $venue_test->categories;

我收到以下错误:

  
    

发生意外错误。     在VenuePage模型中找不到关联venue_to_cat_pages

  

完整代码:

class VenuePage extends ActiveRecord\Model {
  static $table_name = 'Pages';
  static $primary_key = 'cid';
  static $has_many = array(
              array('venue_to_cat_pages', 'class_name' => 'VenueToCategory', 'foreign_key' => 'venuecid', 'primary_key' => 'cid'),
              array('categories', 'through' => 'venue_to_cat_pages')
  );
}



class Category extends ActiveRecord\Model {
  static $table_name = 'EnilonVenueCategories';
  static $has_many = array(
  array('venue_to_cat_categories', 'class_name' => 'VenueToCategory', 'foreign_key' => 'categoryid', 'primary_key' => 'id'),
  );
}


class VenueToCategory extends ActiveRecord\Model {
  static $table_name = 'EnilonVenuesToCategories';
  static $primary_key = 'ID';

  static $belongs_to = array(
    array('venue_to_cat_pages', 'class_name' => 'VenuePage',  'foreign_key' => 'venuecid', 'primary_key' => 'cid'),
    array('venue_to_cat_categories',  'class_name' => 'Category', 'foreign_key' => 'categoryid', 'primary_key' => 'id')
  );
}   

1 个答案:

答案 0 :(得分:3)

您的表/列名称非常混乱,因此我的语法可能有误,但您需要将Category上的has_many链接返回VenuePage:

class Category extends ActiveRecord\Model {
  static $table_name = 'EnilonVenueCategories';
  static $has_many = array(
      array('venue_pages', 'class_name' => 'VenuePages'),   // <---- Add this
      array('venue_to_cat_categories', 'class_name' => 'VenueToCategory', 'foreign_key' => 'categoryid', 'primary_key' => 'id'),
  );
}