# Association is not defined

**URL:** <https://discourse.cakephp.org/t/association-is-not-defined/7153>\
**Category:** Uncategorized\
**Created:** [January 22, 2020, 10:23pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153 "2020-01-22T22:23:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 22, 2020, 10:23pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/1 "2020-01-22T22:23:46Z")

</div>

Hello, help my please, I can’t link tables.

Thi`s is my page :

![1](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/14c740039b5791e61eb412cc864c702b5d4631b1.png)

It`s displays table from DB.

![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/07b63815cd1a4e1e93b986d5fb17ec38044ff4a0.png)

This is code from my controller :

```
public function index()
{
    $title = 'Order list';

    $orders = $this->Paginator->paginate($this->Clients_products->find('all', ['contain' => ['Clients', 'products']]));

    $this->set(compact('title', 'orders'));
}

```

This is code my model :

\<?php // src/Model/Table/Clients\_productsTable.php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class Clients\_productsTable extends Table { public function initialize(array $config) { // $this-\>belongsTo('Clients'); // I tried this is too $this-\>belongsToMany('Clients'); $this-\>belongsToMany('Products'); } } ?\>

But I always get the error :

# The clients association is not defined on Clients\_products.

what am I doing wrong ???

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 22, 2020, 10:31pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/2 "2020-01-22T22:31:05Z")

</div>

I forgot to say. This is an interim many-to-many relationship table.

---

<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:** [January 22, 2020, 11:45pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/3 "2020-01-22T23:45:59Z")

</div>

Check whether `$this->Clients_products` is of the class that you expect it to be. `$this->ClientsProducts` would be more standard, and the class called `ClientsProductsTable`.

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 6:57pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/4 "2020-01-26T18:57:17Z")

</div>

like this ?

\<?php // src/Model/Table/Clients\_productsTable.php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class ClientsProductsTable extends Table { public function initialize(array $config) { // $this-\>belongsTo('Clients'); $this-\>belongsToMany('Clients'); $this-\>belongsToMany('Products'); } } The result is the same # The clients association is not defined on Clients\_products.InvalidArgumentException

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 7:03pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/5 "2020-01-26T19:03:33Z")

</div>

The table is called like this “clients\_products”

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 9:25pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/8 "2020-01-26T21:25:18Z")

</div>

# The clients association is not defined on Clients\_products.

How to name the class classTable if the table is called “clients\_products” ?

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 9:36pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/9 "2020-01-26T21:36:36Z")

</div>

according to the documentation :

## Model Conventions

Table class names are plural, PascalCased and end in `Table` . `UsersTable` , `ArticleCategoriesTable` , and `UserFavoritePagesTable` are all examples of table class names matching the `users` , `article_categories` and `user_favorite_pages` tables respectively.

Entity class names are singular PascalCased and have no suffix. `User` , `ArticleCategory` , and `UserFavoritePage` are all examples of entity names matching the `users` , `article_categories` and `user_favorite_pages` tables respectively.

but name “ClientsProductsTable” not suitable

---

<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:** [January 26, 2020, 11:14pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/10 "2020-01-26T23:14:02Z")

</div>

ClientsProductsTable is exactly what the conventions would have you name it. What’s not suitable about it?

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 11:25pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/11 "2020-01-26T23:25:00Z")

</div>

The problem was solved after I realized the connection BelongsToMany Associations according to the documentation.

[https://book.cakephp.org/3/en/orm/associations.html](https://book.cakephp.org/3/en/orm/associations.html)

But another appeared. I do not have all the data displayed.

$ordsList = $this-\>Paginator-\>paginate($this-\>Clients\_products-\>find(‘all’, [‘contain’ =\> [‘clients’, ‘products’]]));

in the database it :

 ![1](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/b09fefc990630bc637fcca9fdf730ada3f721c31.png)

in the view :

 ![2](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/e/eb8ddbb1ac0b94dff7809641492f3ae771ff857d.png)

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 11:27pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/12 "2020-01-26T23:27:02Z")

</div>

seven people from 13 to 20 values id have disappeared

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 26, 2020, 11:32pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/13 "2020-01-26T23:32:24Z")

</div>

I made a debug($ordsList)

and received

Object of class Cake\ORM\ResultSet could not be converted to string

How to fix it ?

---

<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:** [January 27, 2020, 4:35am UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/14 "2020-01-27T04:35:20Z")

</div>

You’ve used `paginate`, which returns results in blocks instead of all at once.

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 27, 2020, 9:24pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/15 "2020-01-27T21:24:25Z")

</div>

I understand the problem is -\>find(‘all’) …  
I can’t solve it. I read about pagination.

[https://book.cakephp.org/3/ru/controllers/components/pagination.html](https://book.cakephp.org/3/ru/controllers/components/pagination.html)

but nothing came of what I tried.  
please tell me how to do it right ?

---

<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:** [January 27, 2020, 9:46pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/16 "2020-01-27T21:46:54Z")

</div>

You have 24 records in your database. Do you want them all on one page? If you get to 500 records, do you want them all on one page?

---

<div class="post-metadata">

**Author:** ![Anatoliy](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Anatoliy](https://discourse.cakephp.org/u/Anatoliy)\
**Post date:** [January 28, 2020, 1:54pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/17 "2020-01-28T13:54:32Z")

</div>

yes I want to display the entire list

---

<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:** [January 28, 2020, 3:25pm UTC](https://discourse.cakephp.org/t/association-is-not-defined/7153/18 "2020-01-28T15:25:44Z")

</div>

Then you shouldn’t be using pagination, which inherently will get only partial results. Instead of

`$ordsList = $this->Paginator->paginate($this->Clients_products->find(‘all’, [‘contain’ => [‘clients’, ‘products’]]));`

use

`$ordsList = $this->Clients_products->find(‘all’, [‘contain’ => [‘clients’, ‘products’]]);`

Though again, Cake’s naming standards would generally have this instead as:

`$ordsList = $this->ClientsProducts->find(‘all’, [‘contain’ => [‘Clients’, ‘Products’]]);`
