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)

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?



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.

    打赏 评论

相关推荐 更多相似问题