1

In CakePHP 3, is it possible to run a custom SQL query from within a table model? If so, how?

I tried the following from within a table model:

public function getUsersByLocation($location)
{
    $sql = "SELECT `user_id`, `username` FROM `user` WHERE `location` = ?";
    $rows = $this->query($sql, [$location]);
}

(The query is deliberately simple for example purposes.)

But then, when I loop through the $rows, this results in an infinite loop.

I'm surprised CakePHP 3 does not clearly document running custom queries from a table model anywhere in their documentation. They are currently forcing users to use CakePHP's own numerous proprietary methods to glue together various queries.

5
  • There is simply no point in your case to use a custom query. It is in any case inferior to using the ORM directly. But it is very well documented here to do custom ones. Commented Feb 8, 2016 at 23:07
  • So from within a table model, I need to get the database connection (since it presumably was not passed in via dependency injection), and run the query on that? What an atrocious framework. Commented Feb 9, 2016 at 3:03
  • No, from withtin a table, you don't need to get the connection from the outside world, the connection is by default injected via the constructor, and can be accessed via the Table::connection() method (for tables this will by default return an instance of \Cake\Database\Connection). Maybe you should pay a little more attention to the docs of the oh so horrible framework that you are using. Commented Feb 9, 2016 at 11:28
  • And don't blame the framework if you are abusing things in a really bad way. In general: If you need to workaround sth you usually don't use the recommended best practice approach. The less lines, the better. Commented Feb 9, 2016 at 16:57
  • @mark Where in my example is anything being abused in a really bad way? If you have an elegant answer, please post it, and I will accept it. I would be glad to change my opinion on this framework. Commented Feb 10, 2016 at 0:42

2 Answers 2

3

Here is the answer:

public function getUsersByLocation($location)
{
    $sql = "SELECT `user_id`, `username` FROM `user` WHERE `location` = ?";
    return $this->connection()->execute($sql, [$location])->fetchAll('assoc');
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can also just pull in the eloquent orm and use that directly instead of using the CakePHP orm. Since both are composer-compatible projects, this should be pretty straightforward.
Great to hear that we think alike. As a CakePHP core developer, I hate to see our users complain about software when simple solutions are available to them. Best of luck :)
1

So simply use php

$query = $this->find()
    ->select(['user_id', 'username'])
    ->where('location' => $location);

within your Table class.

This is all documented very clearly in the docs.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.