我有一个表“类别”。每个类别都可以有一个父类别。
Categories
id
parent_category
title
每个类别只有一个parent_category
我的问题是:
答案 0 :(得分:1)
您可以尝试以下方法......
<?php
class Category extends ActiveRecord\Model {
static $belongs_to = array(
array('parent', 'foreign_key' => 'parent_category', 'class_name' => 'Category')
);
static $has_many = array(
array('children', 'foreign_key' => 'parent_category', 'class_name' => 'Category'),
);
}
您只需检索父类别:
$category = Category::find(1);
print 'Parent Title : ' . $category->parent->title;
或者检索所有儿童类别:
$categoryParent = Category::find(1);
// loop through all child elements...
foreach ($categoryParent->children as $category) {
print $category->title . ' <br/>';
}