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.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?