0

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.

1
  • 1
    Whenever receiving errors, please always post the complete error message including the full stacktrace (ideally copied from the logs where it it is available in a properly readable fashion)! Also show the proper context, ie the code that actually triggers the error! You're most certainly not doing it as shown in the answer of @drmonkeyninja, which should work just fine. Commented Feb 22, 2016 at 17:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this actually. I think it was meant to work with previous versions of php, before on 5.6 I get Indirect modification of overloaded property has no effect error. I should actually update the anwer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.