【问题标题】:Getting the count of table from another table in cakephp从 cakephp 中的另一个表中获取表的计数
【发布时间】:2017-04-27 10:21:52
【问题描述】:

我正在尝试使用 cakephp3 从另一个表中获取表的计数,但出现错误

错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“) AS Transactions__COUNT() FROM transactions Transactions LEFT JOIN transactio' 附近使用正确的语法

我不知道我的代码有什么问题!

这是我的控制器:

  $data = $this->Transactions->find(
        'all', 
        array(
            'fields' => array('COUNT(*)'),
            'group' => array('transactions_type_id'),
            'contain' => array('TransactionsTypes')
             )                );

这是我的 ctp:

 <?php 
                        foreach ($data as $transactionsType) {

                            ?>
             <li>
                <p class="clearfix"><i class="icon-radio-checked text-info"></i><b><?php echo $transactionsType['title'] ?> </b><span>210</span>
                </p>
                <div class="progress progress-md">
                    <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="8" aria-valuemin="0" aria-valuemax="100" style="width: 8%"><span class="sr-only">10% Complete (success)</span>
                    </div>
                </div>
            </li>
             <?php }
                        ?>

【问题讨论】:

    标签: php mysql cakephp-3.0


    【解决方案1】:

    你可以使用count()函数。

    $count=TableRegistry::get('Transactions')
        ->find()
        ->contain('TransactionsTypes')
        ->group('Transactions.transactions_type_id')
        ->count(); 
    

    【讨论】:

    • 感谢您的重播,我已经尝试过了,但这给了我这个错误警告(2):为 foreach() 提供的参数无效 [APP/Template\Pages\home.ctp,第 109 行]
    • 这一行的错误:foreach ($count as $transactionsType) {
    • 我也试过 var_dump($count) 并且给了我 int(1)
    • count() 将行数作为 int 值返回。所以如果你也想得到结果,请使用 all() 函数或 first() book.cakephp.org/3.0/en/orm/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多