1

大家好,我的函数中有两个 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>
4

1 回答 1

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);

请询问它是否不适合您。

于 2012-08-07T06:52:20.947 回答