# Query with 'contain' does not include the associated objects in the result

**URL:** <https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979>\
**Category:** Need Help\
**Tags:** help\
**Created:** [February 8, 2021, 3:56pm UTC](https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979 "2021-02-08T15:56:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eMarcus](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@eMarcus](https://discourse.cakephp.org/u/eMarcus)\
**Post date:** [February 8, 2021, 3:56pm UTC](https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979/1 "2021-02-08T15:56:32Z")

</div>

Hi,

I have a simple table with earnings and outstanding payments and the customer\_id of the client who owes that payment.

For managing the outstanding payments, I want a View that lists all outstanding payments and the fullname of the customer.

In the controller I created a query that selects the earnings records with outstanding payments:

> $query = $this-\>Earnings-\>find();  
> $query-\>select([‘id’, ‘date’, ‘customer\_id’, ‘service’, ‘outstanding’, ‘payment\_id’]);  
> $query-\>where([‘outstanding IS NOT’ =\> 0]);

BTW: I was wondering if there is any better way to filter the records just by finding all where outstanding \> 0?

Anyway, I then store the result in a variable which I pass to the View:

> $outstandings = $this-\>paginate($query, [‘contain’ =\> [‘Customers’, ‘Payments’]]);  
> $this-\>set(‘outstandings’, $outstandings);

Although I included ‘Customers’ in ‘contain’, I don’t get any associated customer records. The resultset just holds the customer\_id, but no further Customer object. I am therefore not able to access the Customer fields like its fullname.

what’s wrong? or what would be the proper way?

Thanks,  
Marcus.

---

<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:** [February 8, 2021, 5:54pm UTC](https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979/2 "2021-02-08T17:54:03Z")

</div>

I believe that `IS NOT` is only for use with `NULL`, you should be using `'outstanding >' => 0`.

Your contained records are not showing up, because you have used `select` to tell it the fields you want. I personally never bother with this, I think that any performance benefit gained by limiting the fields pulled over from the database is going to be minimal, compared to the time I’ll waste when some calculation or display somewhere changes to include a field that is included in most queries but not all of them and I have to track down what’s going wrong.

But if you insist on using it (there are, after all, times when it’s required), then you can add `->enableAutoFields(true)` to your query, and that will pull in the associated records in their entirety, or I think you can use `queryBuilder` arguments in your containment if you want to further limit what fields are selected from those records.

---

<div class="post-metadata">

**Author:** ![eMarcus](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@eMarcus](https://discourse.cakephp.org/u/eMarcus)\
**Post date:** [February 9, 2021, 7:54am UTC](https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979/3 "2021-02-09T07:54:19Z")

</div>

Hi,

Thanks for the hint with the -\>enableAutoFields(true) - this really did the trick. I also removed the select statement from the query. Honestly, there was no real intention to limit the columns, I was just trying to find out how to create a proper query at all and followed the documentation.

Another obstacle was, that the associated objects are spelled lowercase in the View - which took me some time to find out.

But now the View works as intended!  
Thanks a lot,  
Marcus.

---

<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:** [February 9, 2021, 5:26pm UTC](https://discourse.cakephp.org/t/query-with-contain-does-not-include-the-associated-objects-in-the-result/8979/4 "2021-02-09T17:26:28Z")

</div>

If you don’t have a `select` clause any more, you don’t need `enableAutoFields`. And yes, associated objects are always spelled with lowercase. You can use `debug($whatver)` to look at objects whenever you run into situations where you’re not sure exactly what structure the data has.
