Skip to content
Advertisement

“Cannot convert value to string” when retrieving data (belongsToMany relationship)

I’m using CakePHP 3.3.6 and MySQL 5.7.13.

I have these three tables in my database (amongst others) : collections, tags and the join table collections_tags.

collections Table

JavaScript

tags Table

JavaScript

collections_tags Table

JavaScript

in my TableCollectionsTable.php:

JavaScript

in my TableTagsTable.php:

JavaScript

I can get all the Collections or all the Tags. It works. But if I try to get all the Collections with their associated tags, I have this error :

Cannot convert value to string

This error happens when I have this in my Collections controller :

JavaScript

and this in my TemplateCollectionsindex.ctp :

JavaScript

I have no idea why… I tried creating a TableCollectionsTagsTable.php file, but it did not make a difference.

Thanks for your help

EDIT : I tried changing the DATETIME fields by TIMESTAMP, and the TINYINT by INT, it did not change anything.

Advertisement

Answer

Tested it locally with similar setup. Seems like your primary key index "user_id" in collections table causes the problem here. By removing it, problem is gone.

I don’t actually have much knowledge of composite keys and their usage in CakePHP3, so maybe someone with more experience would be able to tell, why this is failing.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement