【发布时间】:2011-05-09 05:50:24
【问题描述】:
我浏览过几个站点(包括这个站点),不幸的是,作为一个 Kohana 新手,我仍然无法使用它。数据关系比较简单,我有一个公司记录,应该关联1个状态记录和1个类型记录。当然,表中会有多家公司,但每家公司只允许链接到每个公司中的一个(并且必须是)。
我拥有的是:
class Model_Company extends ORM
{
protected $_has_one = array(
'companystatus' => array('model' => 'companystatus', 'foreign_key' => 'entryid'),
'companytype' => array('model' => 'companytype', 'foreign_key' => 'entryid')
,
);
}
公司状态模型:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_CompanyStatus extends ORM
{
protected $_table_name = 'datadictionary';
protected $_primary_key = 'entryid';
protected $_has_many = array(
'company' => array('foreign_key' => 'statusid')
,
);
}
?>
公司类型模型:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_CompanyType extends ORM
{
protected $_table_name = 'datadictionary';
protected $_primary_key = 'entryid';
protected $_has_many = array(
'company' => array('foreign_key' => 'companytypeid')
,
);
}
?>
companystatus 和 companytype 模型映射到具有 2 个字段(entryid 和 entryname)的单个表。此表称为“datadictionary”,具有适当的属性,因此我不必使用“id”作为记录 id 字段。
现在我像这样加载我的公司记录:
$company = ORM::factory('company')
->where('id', '=', 1)
->where('hasbeendeleted', '=', 0)
->find();
问题是我没有为公司的 companystatus 和 companytype 属性返回任何东西,当我执行 $company->companystatus->find() 时,我得到了返回的第一条记录,这很奇怪。我错过了什么?
谢谢!!
:-)
编辑: 为简单起见,Company 表包含以下字段:
ID (primary key) - auto inc int
CompanyName - varchar(255)
StatusID - int
CompanyTypeID - int
HasBeenDeleted - smallint (0 for false, 1 for true)
数据字典表:
EntryID (primary key) - auto inc int
EntryName - nvarchar(255)
公司记录示例:
ID: 1
CompanyName: TestCompany
StatusID: 1
CompanyTypeID: 3
HasBeenDeleted: 0
DataDictionary 记录示例:
EntryID: 1
EntryName: Active
EntryID: 2
EntryName: Inactive
EntryID: 3
EntryName: Customer
EntryID: 4
EntryName: Supplier
【问题讨论】:
标签: php model-view-controller orm kohana