Kohana 3 ORM 关系问题
我浏览过几个网站(包括这个),不幸的是,作为 Kohana 新手,我仍然无法让它工作。数据关系相当简单,我有一条公司记录,它应该链接到 1 个状态记录和 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')
,
);
}
?>
公司状态和公司类型模型映射到一个表,该表有 2 个字段:entryid 和entryname。该表称为“datadictionary”,并且具有适当的属性,因此我不必使用“id”作为记录 id 字段。
现在我像这样加载我的公司记录:
$company = ORM::factory('company')
->where('id', '=', 1)
->where('hasbeendeleted', '=', 0)
->find();
问题是我没有得到公司的 companystatus 和 companytype 属性的任何信息,当我执行 $company->companystatus->find() 时,我得到了返回第一条记录,这很奇怪。我缺少什么?
谢谢!!
:-)
编辑: 为简单起见,Companys 表具有以下字段:
ID (primary key) - auto inc int
CompanyName - varchar(255)
StatusID - int
CompanyTypeID - int
HasBeenDeleted - smallint (0 for false, 1 for true)
DataDictionary 表:
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
I've been through several sites (including this one), and unfortunately as a Kohana newbie I still can't get this to work. The data relationship is fairly simple, I have a company record, which should be linked to 1 status record and 1 type record. Of course there will be multiple companies in the table, but each company is only allowed to be linked to 1 of each (and must be).
What I have is:
class Model_Company extends ORM
{
protected $_has_one = array(
'companystatus' => array('model' => 'companystatus', 'foreign_key' => 'entryid'),
'companytype' => array('model' => 'companytype', 'foreign_key' => 'entryid')
,
);
}
Company Status Model:
<?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')
,
);
}
?>
Company Type Model:
<?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')
,
);
}
?>
The companystatus and companytype models are mapped to a single table which has 2 fields, entryid and entryname. This table is called "datadictionary", and has the appropriate properties so that I don't have to use "id" as the record id field.
Now I load my Company record like this:
$company = ORM::factory('company')
->where('id', '=', 1)
->where('hasbeendeleted', '=', 0)
->find();
The problem is that I don't get anything back for the companystatus and companytype properties for the company, and when I do a $company->companystatus->find() I get the first record returned, which is weird. What am I missing?
Thanks!!
:-)
Edit:
For simplicity's sake the Companies table has the following fields:
ID (primary key) - auto inc int
CompanyName - varchar(255)
StatusID - int
CompanyTypeID - int
HasBeenDeleted - smallint (0 for false, 1 for true)
DataDictionary Table:
EntryID (primary key) - auto inc int
EntryName - nvarchar(255)
Example Company record:
ID: 1
CompanyName: TestCompany
StatusID: 1
CompanyTypeID: 3
HasBeenDeleted: 0
Example DataDictionary records:
EntryID: 1
EntryName: Active
EntryID: 2
EntryName: Inactive
EntryID: 3
EntryName: Customer
EntryID: 4
EntryName: Supplier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些事情我会尝试改变。
首先,为了可读性,大多数人在外键中使用下划线。因此,我建议使用
entry_id
而不是entryid
(您必须在数据库和代码中进行更改)。在 Kohana 3 中,声明
'model' =>当键与模型名称相同时,
是多余的。您可以安全地删除该部分。$has_one
数组中的“companystatus”但实际上,这对于您的问题来说都是偶然的,它存在于最后一个 ORM 调用和您的数据库之间的某个位置。 (我在这里假设
hasbeendeleted
是company
表中的一列,而不是您提到的其他两个表中的任何一个。如果情况并非如此,请告诉我。)如果您将
->where('id', '=', 1)
与->find()
一起执行,那么您确实期望返回一条公司记录(如果数据库中存在)。我建议对hasbeendeleted
进行单独检查。说到这里,该变量不应命名为
$companies
,而应该是单数(例如$company
),因为它只保存一条记录。您可以将
ORM::factory('company')->where('id', '=', 1)
简化为ORM::factory('company', 1 )
如果您知道存在数据库 ID 为 1 的公司,则以下代码应返回该记录:
$myCompany = ORM::factory('company ', 1);
然后您可以执行类似
if (! $myCompany->hasbeendeleted) ...
的操作,这应该会对您有所帮助。如果您遇到麻烦,请发布更多详细信息。
There are a few things here I would try changing.
First of all, for readability, most people use underscores in foreign keys. So instead of
entryid
, I'd recommend usingentry_id
(you'd have to make the change in both your database and your code).In Kohana 3, declaring
'model' => 'companystatus'
in a$has_one
array is redundant when the key is the same as the model name. You can safely remove that part.But really, that's all incidental to your problem, which exists somewhere between that last ORM call and your database. (I'm assuming here that
hasbeendeleted
is a column in thecompany
table, not either of the other two tables you mentioned. Let me know if that's not the case.)If you're doing a
->where('id', '=', 1)
together with a->find()
, you're really expecting to return the one company record if it exists in the database. I would recommend making a separate check forhasbeendeleted
.And speaking of which, instead of naming that variable
$companies
, it should really be singular (e.g.$company
) since it will only hold one record.And you can simplify
ORM::factory('company')->where('id', '=', 1)
to simplyORM::factory('company', 1)
If you know for sure that a company with a database ID of 1 exists, then the following code should return that record:
$myCompany = ORM::factory('company', 1);
Then you can do something like
if ( ! $myCompany->hasbeendeleted) ...
That should help you a bit. Post more details if you run into trouble.