# Need help for a query with cakephp 3

**URL:** <https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365>\
**Category:** Need Help\
**Created:** [October 22, 2016, 9:01am UTC](https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365 "2016-10-22T09:01:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nes280](https://avatars.discourse-cdn.com/v4/letter/n/b4bc9f/32.png) [@nes280](https://discourse.cakephp.org/u/nes280)\
**Post date:** [October 22, 2016, 9:01am UTC](https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365/1 "2016-10-22T09:01:16Z")

</div>

Hello,  
I’m new with cakephp 3 and I don’t know how can I do this SQL query:

```
SELECT n1.mot, r.nomc, n2.mot, d.def
FROM `aretes` as a, `noeuds` as n1, `noeuds` as n2, `definitions` as d, `relations` as r
WHERE a.mot1 = n1.id and 
		a.mot2 = n2.id and
		a.rel = r.id and
		n1.id = d.noeud_id and 
		n1.id = 41114
group by a.rel
order by n2.poids desc

```

I need to use this query in NoeudsController.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [October 22, 2016, 4:18pm UTC](https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365/2 "2016-10-22T16:18:26Z")

</div>

I’d use the querybuilder for that kind of query. If you haven’t used QB before, I’d practice on some simple queries first.  
Also have a look at this, not cake, but shows how a complex query was worked up using QB.  
[https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute](https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute)  
Just remember cake QB syntax is a little different.  
Also you can write raw pdo see [http://book.cakephp.org/3.0/en/orm/database-basics.html](http://book.cakephp.org/3.0/en/orm/database-basics.html)

---

<div class="post-metadata">

**Author:** ![nes280](https://avatars.discourse-cdn.com/v4/letter/n/b4bc9f/32.png) [@nes280](https://discourse.cakephp.org/u/nes280)\
**Post date:** [October 22, 2016, 4:29pm UTC](https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365/3 "2016-10-22T16:29:23Z")

</div>

Ok, thank you!  
Now I have a problem with paginator…

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [October 22, 2016, 4:47pm UTC](https://discourse.cakephp.org/t/need-help-for-a-query-with-cakephp-3/1365/4 "2016-10-22T16:47:12Z")

</div>

I have a fix for paginator, I use a different one where I have full control. See this paginator and learn how to use it, I have used in asp.ner, jsp / servlets, just had to change the language etc

> <https://github.com/nova-framework/framework/blob/2.2/app/Helpers/Paginator.php>

There are instructions in the comments how to use skip take —

```auto
    /**
     * getLimit2 and getPerPage are used together
     * when using the Eloquent Query Builder
     * for the skip and take parameters.
     *
     * There are also other ORM's that need the skip and take
     * parameters separated.
     *
     * Example in controller method calling model method:
     *
     * $data['pets'] = $this->pet->getPets($pages->getLimit2(), $pages->getPerPage(), $petSearch);
     *
     * Example model method using Eloquent Query Builder:
     *
     * public function getPets($offset = "", $rowsPerPage = "", $petSearch = "")
     * {
     * $petsearch = $petsearch . "%";
     *
     * return Capsule::table('pets')
     * ->where('petName', 'like', $petsearch)
     * ->orderBy('petName', 'asc')
     * ->skip($offset)->take($rowsPerPage)->get();
     * }
     *
     * Also see the file in the helpers folder page_eloq.md for more help.
     *

```

If you need more help just start a new topic, but believe me this is a good paginator.

Also see [http://laravel.io/forum/11-13-2014-laravel-5-pagination](http://laravel.io/forum/11-13-2014-laravel-5-pagination) where I show it, but updated since no \_ now
