2012-08-07 109 views
1

大家好我有我的函數中有兩個sql語句,我想要結果的$帳戶,並將其用作我的第二個語句中的where子句。我試圖從accounts_users表中獲取account_id,以便我可以將相關模板獲取到該帳戶。cakephp結合兩個sql語句

這兩個發現被稱爲$模板和$帳戶,我想第一個查詢的結果用於什麼template.account_id ='答案'是。

SELECT `AccountsUser`.`id`, `AccountsUser`.`account_id` FROM `pra`.`accounts_users` AS `AccountsUser` WHERE `id` = 14 LIMIT 1 

SELECT `Template`.`id`, `Template`.`name`, `Template`.`description`, `Template`.`account_id` FROM `pra`.`templates` AS `Template` WHERE `Template`.`account_id` = '' 

這是在我的控制器代碼

$this->Template->unbindModel(array('belongsTo'=>array('Account'))); 
     $templates = $this->Auth->user('name'); 
     $accounts=$this->User->AccountsUser->find('first', array('fields'=>array('id','account_id'),'conditions' =>  array('id' => $this->Auth->user('id')))); 
     $this->set('Templates', $this->Template->find('all', array('conditions' => array('Template.account_id' => $accounts)))); 
     $this->set('templates', $templates); 
     $this->set('accounts'. $accounts); 

視圖 '視圖'

<div class = "conlinks">   

<table width="100%" border="1"> 

      <table width="100%" border="1"> 
       <tr> 
        <th>Template Name</th> 
        <th>Template Description</th> 

       </tr> 

       <?php foreach($templates as $template): ?> 
        <tr> 
         <td align='center'><?php echo $template['Template']['name']; ?></td> 
         <td align='center'><?php echo $template['Template']['description']; ?></td> 
        </tr> 
       <?php endforeach; ?> 

      </table> 

</div>    
    </table> 

回答

1

您可以嘗試使用以下命令:

$this->Template->unbindModel(array('belongsTo'=>array('Account'))); 
    $templates = $this->Auth->user('name'); 
    $accounts=$this->User->AccountsUser->find('list', array('fields'=>array('id', 'account_id'),'conditions' => array('user_id' => $this->Auth->user('id'))));  
    $this->set('Templates', $this->Template->find('all', array('conditions' => array('Template.account_id' => $accounts['AccountsUser'])))); 
    $this->set('templates', $templates); 
    $this->set('accounts'. $accounts); 

懇請如果它不適合你。

+0

nameSQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行的'NULL'附近使用正確的語法。 – user1393064 2012-08-07 06:54:39

+0

當我去重新加載頁面時拋出該錯誤。 – user1393064 2012-08-07 06:55:38

+0

不,這個用戶有一個10的account_id,他也有創建模板以及 – user1393064 2012-08-07 07:01:37