2012-09-18 43 views
2

嘿傢伙,我們有一個完美的工作網頁(index_admin)的關係控制器,但添加分頁後,它的所有崩潰。分頁SQL錯誤

想出:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Relationship.sender_id' in 'where clause' 

客戶和企業建立的「關係」,使他們可以在我們的網站交換髮票。下面是DB模式:

ID,SENDER_ID,receiver_id,活動,要求,EXPIRY_DATE

SENDER_ID和receiver_id都是外鍵的帳戶表。換句話說,告訴db哪些帳戶是相互關聯的。

關係模型 '屬於關聯' '接收者和發送者的帳戶模型':

public $belongsTo = array(
     'ReceiverAccount' =>array(
      'className' => 'Account', 
      'foreignKey' =>'receiver_id', 
      'associationForeignKey' => 'accounts_id', 
      ), 
     'SenderAccount' =>array(
      'className' => 'Account', 
      'foreignKey' =>'sender_id', 
      'associationForeignKey' => 'accounts_id',) 
      ); 

Index_admin:

public function index_admin(){ 
     $this->set('title_for_layout', 'Relationships'); 
     $this->set('stylesheet_used', 'homestyle'); 
     $this->set('image_used', 'eBOXLogoHome.png'); 
     $this->layout='home_layout'; 

     //retrieve Account Id of current User  
     $accountid=$this->Auth->user('account_id'); 

     //Conditions 
     $conditions=array(
     "OR"=> array(
      'Relationship.sender_id' => $accountid, 
      'Relationship.receiver_id' => $accountid) 
     ); 



     //Find all Invoices where receiver_id = accountid 
     $relationships=$this->Relationship->find('all', array(
     'conditions' => $conditions)); 

     debug($conditions); 

     $compName = $this->Account->field('account_name', array('id' => 'Relationship.id')); 

     $this->paginate = array(
     'limit' => 10, 
     'conditions'=> $conditions 
     ); 

     $this->set('accountid', $accountid); 
     $this->set('relationship', $this->paginate()); 

     $this->set('compName', $compName); 
    } 

Index_admin圖(局部):

<table id="data"> 
       <tr> 
         <td colspan=7 align='right'> 
         <?php 
         echo $this->Paginator->prev('<' . __('previous'), array(), null, array('class'=>'prev disabled')); 
         echo '&nbsp;'; 
         echo $this->Paginator->numbers(array('seperator'=>'')); 
         echo '&nbsp;'; 
         echo $this->Paginator->next(__('next') . '>', array(), null, array('class'=>'next disabled')); 
         ?> 
         </td> 
       </tr> 
       <tr> 
        <th><?php echo $this->Paginator->sort('id'); ?></th> 
        <th><?php echo $this->Paginator->sort('sender_id'); ?></th> 
        <th><?php echo $this->Paginator->sort('receiver_id'); ?></th> 
        <th><?php echo $this->Paginator->sort('expiry_date'); ?></th> 
        <th>Status</th> 
        <th>Actions</th> 
       </tr> 

     <?php foreach($relationship as $relationships):?> 

     <?php 

     if($relationships['Relationship']['requested']==1) 
     { 
      $status = 'Requested'; 
      $bgcol = '#F8FAC0'; 
     } 
     else if($relationships['Relationship']['active']==1) 
     { 
      $status = 'Active'; 
      $bgcol = '#CFDAE8'; 
     } 
     else if($relationships['Relationship']['active']==0) 
     { 
      $status = 'Expired'; 
      $bgcol = '#FAB9B9'; 
     }  

     if($relationships['Relationship']['active']==0 && $relationships['Relationship']['requested']==0) 
     { 
      $action = 'Reactivate'; 
     } 
     else 
     { 
      $action = 'Edit Expiry'; 
     } 

     if($relationships['Relationship']['sender_id']==$accountid) 
     { 
      $start = '<font color="#191970">'; 
      $end = '</font>'; 
     } 
     else 
     { 
      $start = NULL; 
      $end = NULL; 
     } 

     if($relationships['Relationship']['receiver_id']==$accountid) 
     { 
      $startr = '<font color="#191970">'; 
      $endr = '</font>'; 
     } 
     else 
     { 
      $startr = NULL; 
      $endr = NULL; 
     } 


     if($relationships['Relationship']['sender_id']==$accountid) 
     { 
      $acctname = $relationships['ReceiverAccount']['account_name']; 
     } 
     else if($relationships['Relationship']['receiver_id']==$accountid) 
     { 
      $acctname = $relationships['SenderAccount']['account_name']; 
     } 

?> 

        <tr> 
         <td align='center'><?php echo $relationships['Relationship']['id']; ?></td> 
         <td align='center'><?php echo $start?><?php echo $relationships['SenderAccount']['account_name']; ?><?php echo $end ?></td> 
         <td align='center'><?php echo $startr?><?php echo $relationships['ReceiverAccount']['account_name']; ?><?php echo $endr ?></td> 
         <td align='center'><?php echo date('d.m.Y', strtotime($relationships['Relationship']['expiry_date'])); ?></td> 
         <td align='center' bgcolor='<?php echo $bgcol ?>'><?php echo $status ?></td> 
         <td align='center'> 
         <?php echo $this->Form->Html->link('Delete', array('controller' => 'Relationships','action'=>'delete',$relationships['Relationship']['id']), NULL, 'Are you sure you want to delete '. $acctname); 
         ?> | <?php echo $action ?> </td> 
        </tr> 
       <?php endforeach; ?> 

      <tr> 
         <td colspan=7 align='right'> 
         <?php 
         echo $this->Paginator->prev('<' . __('previous'), array(), null, array('class'=>'prev disabled')); 
         echo '&nbsp;'; 
         echo $this->Paginator->numbers(array('seperator'=>'')); 
         echo '&nbsp;'; 
         echo $this->Paginator->next(__('next') . '>', array(), null, array('class'=>'next disabled')); 
         ?> 
         </td> 
      </tr> 
      </table> 

就像我說的它所有人都事先工作,現在不是,但我不明白爲什麼。

+0

是否有一個名爲Relationship.sender_id列? –

+0

很明顯。正如我所說,這一切都有效。如果您在關係表中閱讀上面有sender_id .... – user1402677

+0

什麼返回'debug($ conditions);'? –

回答

1

將調試模式設置爲詳細查看所有可能的錯誤總是明智的。你剛剛分享了sql錯誤部分,從中可以明顯看出目標表沒有「sender_id」字段。我假設你已經開啓了調試模式。所以首先看看生成的查詢。然後你會發現查詢試圖挖掘該表

如果您的查詢引用正確的表,你可以試試這個:

public function index_admin(){ 
     $this->set('title_for_layout', 'Relationships'); 
     $this->set('stylesheet_used', 'homestyle'); 
     $this->set('image_used', 'eBOXLogoHome.png'); 
     $this->layout='home_layout'; 

     //retrieve Account Id of current User  
     $accountid=$this->Auth->user('account_id'); 

     //Conditions 
     $conditions=array(
     "OR"=> array(
      'Relationship.sender_id' => $accountid, 
      'Relationship.receiver_id' => $accountid) 
     ); 
     App::import('Model', 'Relationship'); 
     $objRelationship = new Relationship(); 
     $this->paginate = array("conditions" => $conditions, 'limit' => 10); 
     $relationships = $this->paginate($objRelationship); 

     $compName = $this->Account->field('account_name', array('id' => 'Relationship.id')); 

     $this->set('accountid', $accountid); 
     $this->set('relationship', $this->paginate()); 
     $this->set('compName', $compName); 
}