dqxhit3376 2011-05-08 21: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-08 22: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.

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

报告相同问题?

悬赏问题

  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?