【问题标题】:Relationship Gas ORM Codeigniter关系气体 ORM Codeigniter
【发布时间】:2016-08-11 09:55:11
【问题描述】:

我是 Gas ORM 的新手,我有两个表角色和用户,一个用户只有一个角色 如何在用户视图中显示角色名称而不是角色 ID。我正在使用 GAS ORM 和 codeigniter

榜样

    function _init()
{
    self::$relationships = array(
                            'user' => ORM::has_many('\\Model\\User_model'),
                    );
    self::$fields = array('role_id' => ORM::field('auto[11]'),
                           'name' => ORM::field('char[255]', array('required','max_length[255]')),);
            }

用户模型

function _init()
        {
                self::$relationships = array(
                        'role' => ORM::belongs_to('\\Model\\Role_model'),
                );
                self::$fields = array(
                        'user_id'                    =>           ORM::field('auto[255]'),
                        'email'                    =>             ORM::field('email[255]'),
                        'name'                    =>              ORM::field('char[255]'),
                        'username'                 =>             ORM::field('char[255]', array('required','max_length[255]')),
                        'password'              =>                ORM::field('char[255]'),
                        'active'                 =>              ORM::field('numeric[255]'),
                );
        }

在我的视图中,我将用户显示为

<?php $row_count = 0; foreach ($users as $user){ $row_count = ++$row_count;?>
                  <tr>
                    <td><?php echo $row_count; ?></td>
                    <td><?php echo $user->role_id . " " . $user->name; ?></td>
                    <td><?php echo $user->username; ?></td>
                    <td><?php echo $user->role($user->role_id)->name; ?></td>
                    <td><?php if($user->active == 1) echo "Active"; else echo "Inactive"; ?></td>
                    <td><span class="btn btn-warning btn-sm" data-toggle="modal" data-target="#editUser" onclick="edit('<?php echo $user->user_id; ?>')"><span class="glyphicon glyphicon-pencil"></span>&nbsp;Edit</span>
                      <a href="javascript:void(0);" onclick="rm('<?php echo $user->name; ?>','<?php echo $user->user_id; ?>');"><span class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span>&nbsp;Delete</span></a>
                    </td>
                  </tr>
                    <?php } ?>

显示角色名称时出现错误

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法

SELECT * FROM tbl_roles WHERE tbl_roles.role_id IN ()

文件名:third_party/gas/classes/core.php

行号:850

【问题讨论】:

    标签: php codeigniter orm


    【解决方案1】:

    我也有同样的问题。

    如果你使用 primary_key 设置,试试这个。

    orm.php

        /* We wanna use foreign_key always.
        if (empty($this->primary_key))
        {
        */
            if ( ! empty($this->foreign_key))
            {
                // Validate foreign keys for consistency naming convention recognizer
                $foreign_key = array();
    
                foreach($this->foreign_key as $namespace => $fk)
                {
                    $foreign_key[strtolower($namespace)] = $fk;
                }
    
                $this->foreign_key = $foreign_key;
            }
            else
            {
                // If so far we didnt have any keys yet, 
                // then hopefully someone is really follow Gas convention
                // while he define his entity relationship (yes, YOU!)
                foreach ($this->meta->get('entities') as $name => $entity)
                {
                    if ($entity['type'] == 'belongs_to')
                    {
                        $child_name     = $entity['child'];
                        $child_instance = new $child_name;
                        $child_table    = $child_instance->table;
                        $child_key      = $child_instance->primary_key;
    
                        $this->foreign_key[strtolower($child_name)] = $child_table.'_'.$child_key;
                    }
                }
            }
        //}
    

    【讨论】:

    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2019-08-13
    • 2013-01-27
    相关资源
    最近更新 更多