Sasha
Sasha

Reputation: 5944

cakephp 3.x Model adapter for 2.x

Is there somewhere an adapter for cakephp 3.x model to handle 2.x syntax? Like, internally, it just convert syntax like this:

array(
    'conditions' => array('Model.field' => $thisValue),
    'fields' => array('Model.field1', 'DISTINCT Model.field2'),
    'order' => array('Model.created', 'Model.field3 DESC'),
    'group' => array('Model.field'), 
    'limit' => n
)

into:

$query->select('Model.field1')
->distinct('Model.field2')
->order->(['Model.created', 'Model.field3' => 'DESC'])
->group('Model.field')
->where('Model.field' => $thisValue)

and return executed query ->toArray(). So the model of 2.x, becomes compatible with 3.x.

Many thanks to any advises.

Upvotes: 0

Views: 109

Answers (2)

Xymanek
Xymanek

Reputation: 1389

In cake 3 you are not limited to query builder, you can also pass an array to find() (similar to how it worked in 2.x):

$query = $articles->find('all', [
    'conditions' => ['Articles.created >' => new DateTime('-10 days')],
    'contain' => ['Authors', 'Comments'],
    'limit' => 10
]);

You can read about it here

Upvotes: 2

Sasha
Sasha

Reputation: 5944

In case someone else facing with the same problem: shim plugin can solve most of issues with compatibility 2.x and 3.x version.

Upvotes: 0

Related Questions