开发者

What is wrong with my PHP ActiveRecord Has Many Through relationship?

开发者 https://www.devze.com 2023-04-01 09:20 出处:网络
When I run the following code: $venue_test = VenuePage::find(104); $cats = $venue_test->categories; I get the following error:

When I run the following code:

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

I get the following error:

An unexpected error occurred. Could not find the association venue_to_cat_pages in model VenuePage

Complete code:

class VenuePage extends ActiveRecord\Model {
  static $table_n开发者_如何学JAVAame = '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')
  );
}   


Your table / column name's are quite confusing so I may have the syntax wrong, but you need the has_many link on Category back to 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'),
  );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消