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

Friday, April 22, 2022

[FIXED] how to use mysql function inside cakephp2

 April 22, 2022     cakephp, cakephp-2.3, mysql     No comments   

Issue

I am trying to make a simple query inside my model, I have already done the same thing in my controller, and so far it works.

/users/controller 
    $reservations = $db->fetchAll(
        "SELECT TIME_FORMAT(time_start, '%H:%i') as time_start, 
                TIME_FORMAT(time_end, '%H:%i') as time_end 
         FROM bookings 
         WHERE datepicker = ?", [$date]); 

I know that solution is not efficient, so I wanted to create a method in my function to do the same thing, however I can't find a way to enclode the time_start field with the mysql function TIME_FORMAT() using ->find() method.

update.

I have tried using virtual fields and I got only an Indirect modification of overloaded property has no effect error.


Solution

You want to use virtual fields for these. So in your case you'd want something like this:-

public $virtualFields = array(
    'time_start' => "TIME_FORMAT(time_start, '%H:%i')",
    'time_end' => "TIME_FORMAT(time_end, '%H:%i')"
);

These are defined within the relevant model.



Answered By - drmonkeyninja
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • 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