Cakephp 4.x自定义查询

h7wcgrx3  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(170)

有人能帮助我如何生成这个查询到Cakephp查询生成器.我正在升级我的2.x项目到4.x,

SELECT
  Saving.id,
  SUM(IFNULL(Saving.interest,0)) + SUM(IFNULL(CASE SavingSub.type when '1' AND SavingSub.interest <= 0 then SavingSub.amount else 0 end,0)) + SUM(IFNULL(CASE SavingSub.type when '1' AND SavingSub.interest > 0 then SavingSub.interest else 0 end,0)) - SUM(IFNULL(CASE SavingSub.type when '0' then SavingSub.amount else 0 end,0)) - SUM(IFNULL(Saving.interest,0)) as total 

FROM 
 savings as Saving 
 left join saving_subs as SavingSub On SavingSub.saving_id = Saving.id 
where 
  Saving.visible = true and 
  SavingSub.visible = true
group by
  Saving.id
q1qsirdb

q1qsirdb1#

我是如何做到的(不确定这是否是最好的解决方案,如果它在你的情况下工作):

namespace App\Model\Table;
use Cake\Datasource\ConnectionManager;
...
class SomeTable extends Table
{
    public function someFunction() {
        $connection = ConnectionManager::get('default');
        $statement = $connection->execute(
            'SELECT * FROM table WHERE table.id = :id',
            ['id' => $id]
        )->fetchAll('assoc');           

        return $statement;      
    }
}

如果你的查询中没有变量,你可以只保留:id和array ['id' =〉$id]。

相关问题