无法检索有很多模型结果

时间:2018-04-11 02:35:45

标签: php mysql codeigniter orm

我正在使用GasORM到我的codeigniter应用程序,在我的数据库中,我有这两个表彼此关系。

table name: Roles
id : int|primary|uniq|auto
label : var|uniq
description : longtext|null
created_at : timestamp|current_timestamp
updated_at : timestamp|null
-------------------------------------------------------------------------------------------
table name: Permissions
id : int|primary|uniq|auto
role_id : int|uniq|index (FK) -> Roles:id [on update: cascade, on delete: cascade]
label : var|uniq
description : longtext|null
created_at : timestamp|current_timestamp
updated_at : timestamp|null

及其相应的模型

角色模型

<?php

namespace Model;

use \Gas\Core;
use \Gas\ORM;

class Roles extends ORM {

    public $primary_key = 'id';
    public $foreign_key = array('\\model\\roles' => 'id', '\\model\\permissions' => 'role_id');

    function _init()
    {

            self::$relationships = array (
                    // 'roles' => ORM::belongs_to('\\Model\\admins'),
                    'roles' => ORM::has_many('\\Model\\permissions')
            );

            self::$fields = array(
                    'id' => ORM::field('auto[11]'),
                    'user_id' => ORM::field('int[11]',array('required')),
                    'label' => ORM::field('char[250]',array('required')),
                    'description' => ORM::field('string'),
            );

            $this->ts_fields = array('created_at','updated_at');
    }
}

权限模型

<?php

namespace Model;

use \Gas\Core;
use \Gas\ORM;

class Permissions extends ORM {

    public $primary_key = 'id';
    public $foreign_key = array('\\model\\permissions' => 'role_id', '\\model\\roles' => 'id');

    function _init()
    {

            self::$relationships = array (
                    'permissions' => ORM::belongs_to('\\Model\\roles')
            );

            self::$fields = array(
                    'id' => ORM::field('auto[11]'),
                    'role_id' => ORM::field('int[11]',array('required')),
                    'label' => ORM::field('char[250]',array('required')),
                    'description' => ORM::field('string'),
            );

            $this->ts_fields = array('created_at','updated_at');
    }
}

并尝试通过

显示特定角色的所有权限
$roles = Model\Roles::find(1)->with('permissions');

echo '<pre>';

var_dump($roles->permissions);

但它在数据库上仍然返回null,有2个权限链接到该特定角色id。有什么想法,请帮忙吗?

0 个答案:

没有答案