PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

[FIXED] Cakephp how I will write coalesce in query builder on sum?

 February 03, 2022     cakephp, cakephp-4.x     No comments   

Issue

I have postgres query like below

coalesce(sum(SUMMARY_COINS.get_count),0) as total 

I am trying to convert in cakephp query builder

$query = $query->select([
    'total' => $query->func()->coalesce($query->func()->sum('c.get_count'),0)
]

Getting error , How I will apply coalesce in query builder for make isnull zero in sum?


Solution

Check the API documentation, the coalesce() function expects an array that holds the function arguments!

Also note that you need to pass corresponding type information unless you want your scalar values to be bound as strings:

$query->func()->coalesce(
    [$query->func()->sum('c.get_count'), 0],
    // the first type is `null` because expressions (`sum()`) are not being bound
    [null, 'integer']
)

See also

  • API > \Cake\Database\FunctionsBuilder::coalesce()
  • Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
  • Cookbook > Database Access & ORM > Query Builder > Using SQL Functions > Custom Functions


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing