Association is not defined

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

Thi`s is my page :

1

It`s displays table from DB.

image

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 ???

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

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.

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

The table is called like this “clients_products”

The clients association is not defined on Clients_products.

How to name the class classTable if the table is called “clients_products” ?

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

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

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

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 :

in the view :

seven people from 13 to 20 values id have disappeared

I made a debug($ordsList)

and received

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

How to fix it ?

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

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

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

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?

yes I want to display the entire list

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’]]);