无法检索有许多模型结果

pkbketx9  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(169)

我在我的codeigniter应用程序中使用gasorm,在我的数据库中,我有两个相互关联的表。

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。有什么想法,请帮助?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题