我在 Kohana ORM 中加载对象子项时遇到问题。
我的数据库表看起来:
-objects (something like "houses", but objects sounds better in my opinion)
-id
-name
-title
-author
-city (id link to cities.city_id)
-description
-access
-cities
-city_id
-country (id link to countries.country_name)
-city_name
-countries
-country_id
-country_name
和我的模型:
class Model_Object extends ORM
{
protected $_table_name = 'objects';
protected $_table_columns = array(
'id' => NULL,'name' => NULL,
'title' => NULL,
'author' => NULL,
'city' => NULL,
'description' => NULL,
'access' => NULL,
'created' => NULL
);
protected $_primary_key = 'id';
protected $_has_one = array(
'city'=>array(),
'author'=>array(
'model'=>'User',
'foreign_key'=>'user_id',
)
);
}
class Model_City extends ORM
{
protected $_table_name = 'cities';
protected $_table_columns = array('city_id'=>NULL,'city_name'=>NULL);
protected $_primary_key = 'city_id';
protected $_has_one = array('country'=>array(
'model'=>'country',
'foreign_key'=>'country',
));
protected $_belongs_to = array(
'objects'=>array(),
);
}
class Model_Country extends ORM
{
protected $_table_name = 'countries';
protected $_table_columns = array(
'country_id'=>NULL,
'country_name'=>NULL,
);
protected $_primary_key = 'country_id';
protected $_belongs_to = array(
'cities'=>array(
'model' => 'city',
'foreign_key' => 'country'
),
);
}
我需要获取对象的对象,包括国家和城市的名称。我找到了这个:
ORM::factory('Object')->with('City')->with('City:Country')->find_all();
在这里,但它返回城市 ID 而不是城市对象。
所以我的问题是:如何在我呈现的表格中获取带有子对象的对象对象?