2012-08-07 107 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