【问题标题】:cakephp combining two sql statementscakephp结合两个sql语句
【发布时间】:2012-08-04 03:21:34
【问题描述】:

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

【问题讨论】:

    标签: sql cakephp find


    【解决方案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);
    

    请询问它是否不适合您。

    【讨论】:

    • nameSQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“NULL”附近使用正确的语法
    • 不,该用户的 account_id 为 10,他还创建了模板
    • 没有任何内容的数组打印在顶部
    • 我认为问题在于 - 模板表有 account_id,但它存储了 accounts_users 表 (accounts_users.id) 的 id,而不是 accounts_users.account_id。
    • 刚刚尝试了您更新的代码数组([0] => 数组([AccountsUser] => 数组([account_id] => 10)))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2010-11-24
    • 2011-08-22
    • 2015-12-29
    • 2013-05-31
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多