2016-08-11 31 views
0

我是Gas ORM的新手,我有兩個表角色和用戶,一個用戶只有一個角色 如何在用戶視圖中顯示角色名稱和角色ID。我使用GAS ORM和笨關係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]'), 
       ); 
     } 

在我查看 i顯示用戶爲

<?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其中tbl_rolesrole_id IN()

文件名:THIRD_PARTY /氣/班/ core.php中

行號:850

回答

0

我有這個同樣的問題。

如果您使用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; 
       } 
      } 
     } 
    //}