【发布时间】:2011-07-26 05:48:36
【问题描述】:
我有一个如下所示的 MySQL 表结构:
Countries -> Country_Regions (FK: country_id) -> Country_Regions_Cities (FK: region_id)
所以,国家和地区之间是一对多的关系,地区和城市之间是一对多的关系
我尝试将它们与以下类链接:
class Model_Country extends ORM {
protected $_has_many = array('regions' => array("model" => "Countries_Region"));
}
class Model_Countries_Region extends ORM {
protected $_has_many = array('cities' => array("model" => "Countries_Regions_City"));
protected $_belongs_to = array('country' => array("model" => "Country"));
}
class Model_Countries_Regions_City extends ORM {
protected $_belongs_to = array('region' => array("model" => "Countries_Region"));
}
如果我尝试使用
找到所有区域,一切都会好起来的$country = ORM::factory("country", 1);
$region = $country->regions->find_all();
但是当我尝试自下而上查找所有城市时,
$country = ORM::factory("country", 1);
$city = $country->regions->cities->find_all();
它确实可以识别 region 中的 city 属性,但它返回一个空行,其中所有 city 值都设置为 NULL。
我觉得我遗漏了一些非常明显的东西,但我无法弄清楚它是什么。请帮帮我。
【问题讨论】:
标签: kohana kohana-3 one-to-many kohana-orm