# Sort from view with when using hasOne associations

**URL:** <https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976>\
**Category:** Need Help\
**Created:** [February 27, 2018, 3:52pm UTC](https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976 "2018-02-27T15:52:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinobi](https://avatars.discourse-cdn.com/v4/letter/x/b19c9b/32.png) [@xinobi](https://discourse.cakephp.org/u/xinobi)\
**Post date:** [February 27, 2018, 3:52pm UTC](https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976/1 "2018-02-27T15:52:16Z")

</div>

I would like to sort data within a view table, the data comes from four different tables that are associated with hasOne relationship.

EER used as example:

![13](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/2f9a84bc33e89d826a23e480b651243c1fe0cc2b.png)

The action code:

```
 public function index()
    {
        $this->paginate = [
            'limit' => 15,
            'contain' => [
                'Addresses',
                'Emails',
                'Phones'
            ],
            'sortWhitelist' => [
                'id',
                'name',
                'Addresses.street',
                'Emails.address',
                'Phones.number'
            ]
        ];

        $this->set('users', $this->paginate('Users'));
        $this->set('_serialize', ['users']);
    } 

```

The relationships as follow:

```
$this->hasOne('Addresses')
    ->setForeignKey('user_id')
    ->setStrategy('select')
    ->setDependent(true);

$this->hasOne('Emails')
    ->setForeignKey('user_id')
    ->setStrategy('select')
    ->setDependent(true);

$this->hasOne('Phones')
    ->setForeignKey('user_id')
    ->setStrategy('select')
    ->setDependent(true);

```

This is my sort link:

`<?= $this->Paginator->sort('Emails.address', h('E-Mail')) ?>`

Problem: Can’t sort any association fields…

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 27, 2018, 5:15pm UTC](https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976/2 "2018-02-27T17:15:38Z")

</div>

When I want to sort by related model, I have to add the fields in a whitelist, explained in [Control which Fields Used for Ordering](https://book.cakephp.org/3.0/en/controllers/components/pagination.html#control-which-fields-used-for-ordering)

In your example you should have

```php
public $paginate = [
    'sortWhitelist' => [
        'id',
        // other fields
        'Emails.address',
    ]
];

```

---

<div class="post-metadata">

**Author:** ![xinobi](https://avatars.discourse-cdn.com/v4/letter/x/b19c9b/32.png) [@xinobi](https://discourse.cakephp.org/u/xinobi)\
**Post date:** [February 27, 2018, 5:51pm UTC](https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976/3 "2018-02-27T17:51:31Z")

</div>

Yes I agree, however I have a sortWhitelist array with the related models included…  
It’s my first block of code in the above example.

```
'sortWhitelist' => [
                'id',
                'name',
                'Addresses.street',
                'Emails.address',
                'Phones.number'
            ]
```

---

<div class="post-metadata">

**Author:** ![questioner](https://avatars.discourse-cdn.com/v4/letter/q/e8c25b/32.png) [@questioner](https://discourse.cakephp.org/u/questioner)\
**Post date:** [February 18, 2019, 8:17am UTC](https://discourse.cakephp.org/t/sort-from-view-with-when-using-hasone-associations/3976/4 "2019-02-18T08:17:05Z")

</div>

Is there something new about the solution of this problem? I would be interested in it, too.
