dqxhit3376 2011-05-09 05:50
浏览 27
已采纳

Kohana 3 ORM关系问题

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
  • 写回答

1条回答 默认 最新

  • dongzhong7443 2011-05-09 06:11
    关注

    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 using entry_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 the company 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 for hasbeendeleted.

    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 simply ORM::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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件