Question

I have approximately 25 join queries I need to build to run in a project on CakePHP.

It is much faster to just work in MySQL WorkBench or phpMyAdmin building the queries, and then drop into PHP code than to use the screwed up cakePHP model parms that many times have no one-to-one relationship to a MySQL query.

Is there a no-table or multi-table "take anything MySQL" example that reduces or eliminates the CakePHP complexity?

No correct solution

OTHER TIPS

TLDR:

Use the Model's query() method to write any MySQL you'd like.

More details:

It sounds like you haven't actually read through the book. Should probably try that before bashing things that you don't understand.

For anyone else who might want to learn how to join things in CakePHP:

How to Join using CakePHP

And

How to run any MySQL query in CakePHP

CakePHP provides Model::query() for you to have more control. I'd be inclined that you use prepared statements too, if you really want to let go of CakePHP's find methods.

$db = $this->getDataSource();
$db->fetchAll(
    'SELECT * from users where username = ? AND password = ?',
    array('jhon', '12345')
);
$db->fetchAll(
    'SELECT * from users where username = :username AND password = :password',
    array('username' => 'jhon','password' => '12345')
);

Here is a note:

query() does not honor $Model->cacheQueries as its functionality is inherently disjoint from that of the calling model. To avoid caching calls to query, supply a second argument of false, ie: query($query, $cachequeries = false)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top