2012-08-07 6 views
1

안녕 모두 내 함수에 두 개의 sql 문이 있습니다. $ 계정의 결과를 가져 와서 두 번째 문에서 where 절로 사용하고 싶습니다. 해당 계정에 관련 템플릿을 얻을 수 있도록 accounts_users 테이블에서 account_id를 가져 오려고합니다.두 개의 sql 문을 결합한 cakephp

두 개의 검색은 $ templates 및 $ accounts라고하며, 첫 번째 쿼리의 결과를 template.account_id = 'answer'에 사용하고 싶습니다.

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 구문에 오류가 있습니다. 라인 1에서 'NULL'근처에서 사용할 올바른 구문에 대한 MySQL 서버 버전에 해당하는 매뉴얼을 확인하십시오. – user1393064

+0

은 페이지를 다시로드 할 때 그 오류를 던집니다. – user1393064

+0

아니요,이 사용자는 account_id가 10이고 그도 템플릿을 만들었습니다. – user1393064

관련 문제