简体   繁体   English

Kohana 3.3 $ _has_many错误

[英]Kohana 3.3 $_has_many error

I'm trying to setup a one-to-many relationship between a User and his Clients. 我正在尝试在用户与其客户之间建立一对多关系。 I can access a single client when I set my model like this: 像这样设置模型时,我可以访问一个客户端:

class Model_User extends Model_Auth_User {
    protected $_has_one = array(
        'client' => array(
            'model' => 'Client',
            'foreign_key' => 'user_id')
    );
    [...]

But when I try to switch to $_has_many: 但是,当我尝试切换到$ _has_many时:

class Model_User extends Model_Auth_User {
    protected $_has_many = array(
        'clients' => array(
            'model' => 'Client',
            'foreign_key' => 'user_id')
    );
    [...]

I get the following error: 我收到以下错误:

Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT COUNT(*) AS `records_found` FROM `` WHERE = '74' AND IN ('4') ]

Can't figure out what's wrong.. 无法找出问题所在。

I found the problem. 我发现了问题。 Declaring the $_has_many property in my User model was overwriting the same property in Kohana source file, which contained roles and tokens information. 在我的用户模型中声明$ _has_many属性将覆盖Kohana源文件中的相同属性,该文件包含角色和令牌信息。

Solved by copying the values from Kohana's Model_Auth_User and adding my property: 通过复制Kohana的Model_Auth_User的值并添加我的属性来解决:

protected $_has_many = array(
    'user_tokens' => array('model' => 'User_Token'),
    'roles'       => array('model' => 'Role', 'through' => 'roles_users'),
    'clients'     => array('model' => 'Client', 'foreign_key' => 'user_id'),
);

How does it look in the Client? 客户端中的外观如何? If you have "Client -> has_one -> User", this shouldn't be a problem. 如果您有“客户端-> has_one->用户”,则应该没有问题。

If you have "Client -> has_many -> User", you need has_many through 如果您具有“客户端-> has_many->用户”,则需要has_many

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM