In general, an error occurred when displaying the list of products from the category:

Database_Exception [ 1054 ]: Unknown column 'products_categories.categorie_id' in 'where clause' [ SELECT `product`.`id` AS `id`, `product`.`title` AS `title`, `product`.`description` AS `description`, `product`.`price` AS `price`, `product`.`available` AS `available`, `product`.`image_id` AS `image_id` FROM `products` AS `product` JOIN `products_categories` ON (`products_categories`.`product_id` = `product`.`id`) WHERE `products_categories`.`categorie_id` IS NULL ] 

As it turned out, if you change the names in the associated table in the database from categories_id to categorie_id, then everything works. But now adding, editing, deleting goods from the database stops working. It turns out for one case one title of a column fits and for the second another one. Is there any way to fix it at the fix level in php code, and not in the database itself?

Model category and product model is as follows.

 class Model_Product extends ORM{ protected $_table_name = 'products'; protected $_primary_key = 'id'; protected $_db_group = 'default'; protected $_has_many = array( 'categories' => array( 'model' => 'categorie', 'foreign_key' => 'product_id', 'through' => 'products_categories', 'far_key' => 'categories_id', ), 'images' => array( 'model' => 'image', 'foreign_key' => 'product_id', ), ); protected $_belongs_to = array( 'main_img' => array( 'model' => 'image', 'foreign_key' => 'image_id', ), ); class Model_Categorie extends ORM_MPTT { protected $_has_many = array( 'products' => array( 'model' => 'product', 'through' => 'products_categories', ), ); 

Displaying the list of products from the database:

 class Controller_Index_Catalog extends Controller_FrontPage { public function action_cat(){ $cat = (int) $this->request->param('cat'); $categories = ORM::factory('categorie')->where('id', '=', $cat)->find(); $products = $categories->products->find_all(); $content = View::factory('/index/catalog/catalog_cat') ->set('cat',$cat) ->set('categories',$categories) ->set('products',$products); $this->template->content = $content; $this->template->title = $categories->title; } } 

Swears at the line:

$ products = $ categories-> products-> find_all ();

    2 answers 2

    You have a problem with naming tables and key fields. Need to adhere to certain rules:

    1. Tables call everything in plural, based on the rules of the English language: a table with categories - categories, with goods - items. If necessary - use prefixes.
    2. Primary_key in the table write as the name of the table in the singular + '_id'. For categories - category_id, for products - item_id.

    In general, read how to call it all.

      All right said :-) Here, read: ORM - Russian documentation Kohana 3 and more Coding style
      There is enough to understand. :-)

      • Thank. Links at once did not find =) - kemerov4anin