cakephp结合了两个sql语句

时间:2012-08-07 06:27:21

标签: sql cakephp find

大家好我在我的函数中有两个sql语句,我想获取$ accounts的结果并将其用作我的第二个语句中的where子句。我试图从accounts_users表中获取account_id,以便我可以获得该帐户的相关模板。

这两个发现被称为$ 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 个答案:

答案 0 :(得分: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);

请问它是否对您不起作用。