# Is there any way to to still get a result if the contain condition is not respected?

**URL:** <https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285>\
**Category:** Need Help\
**Created:** [April 23, 2021, 1:47pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285 "2021-04-23T13:47:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mainanthem](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mainanthem](https://discourse.cakephp.org/u/mainanthem)\
**Post date:** [April 23, 2021, 1:47pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/1 "2021-04-23T13:47:42Z")

</div>

Hey,

I am trying to get an object with an association that is not always respected.

```
$record = $this->Records->find('all')->contain('Users')->first();

```

I still want to obtain that record even if the user\_id is empty.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 23, 2021, 1:51pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/2 "2021-04-23T13:51:07Z")

</div>

What’s the association between Records and Users?

---

<div class="post-metadata">

**Author:** ![mainanthem](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mainanthem](https://discourse.cakephp.org/u/mainanthem)\
**Post date:** [April 23, 2021, 1:52pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/3 "2021-04-23T13:52:04Z")

</div>

Records BelongsTo Users

**RecordsTable.php**

```
$this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'className' => 'Users',
        'joinType' => 'INNER',
    ]);
```

---

<div class="post-metadata">

**Author:** ![mds2018](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mds2018](https://discourse.cakephp.org/u/mds2018)\
**Post date:** [April 23, 2021, 2:25pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/4 "2021-04-23T14:25:55Z")

</div>

> [@mainanthem](#):
>
> **RecordsTable.php**
> 
> ```auto
> $this->belongsTo('Users', [
> 'foreignKey' => 'user_id',
> 'className' => 'Users',
> 'joinType' => 'INNER',
> ]);
> 
> ```

Should be:

**RecordsTable.php**

```auto
$this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'className' => 'Users',
    ]);

```

And I think that to achieve what you want the field user\_id in the records table will need to be nullable.

---

<div class="post-metadata">

**Author:** ![mds2018](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mds2018](https://discourse.cakephp.org/u/mds2018)\
**Post date:** [April 23, 2021, 2:28pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/5 "2021-04-23T14:28:41Z")

</div>

And this:

> [@mainanthem](#):
>
> `$record = $this->Records->find('all')->contain('Users')->first();`

Should be:

> [@mainanthem](#):
>
> `$recordsQuery = $this->Records->find()->contain(['Users'])->all();`  
> `$records = $recordsQuery->toArray();`

---

<div class="post-metadata">

**Author:** ![mainanthem](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mainanthem](https://discourse.cakephp.org/u/mainanthem)\
**Post date:** [April 23, 2021, 2:41pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/6 "2021-04-23T14:41:14Z")

</div>

Thank you for your help, I edited my code according to your recommandation.

Still having the same **null** object when user\_id is empty even though the user\_id field is nullable.

---

<div class="post-metadata">

**Author:** ![mainanthem](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mainanthem](https://discourse.cakephp.org/u/mainanthem)\
**Post date:** [April 23, 2021, 2:49pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/7 "2021-04-23T14:49:10Z")

</div>

I got it working, it was removing the joinType that did the trick. I don’t know why it didn’t register before  
Thank you so much mds2018.

---

<div class="post-metadata">

**Author:** ![mds2018](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mds2018](https://discourse.cakephp.org/u/mds2018)\
**Post date:** [April 23, 2021, 2:50pm UTC](https://discourse.cakephp.org/t/is-there-any-way-to-to-still-get-a-result-if-the-contain-condition-is-not-respected/9285/8 "2021-04-23T14:50:39Z")

</div>

You’re welcome. Glad you got it working [mainanthem](https://discourse.cakephp.org/u/mainanthem).
