# Cannot convert value of type string to integer when joining multiple tables

**URL:** <https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105>\
**Category:** Need Help\
**Created:** [January 21, 2022, 7:43pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105 "2022-01-21T19:43:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [January 21, 2022, 7:43pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/1 "2022-01-21T19:43:41Z")

</div>

Hello everybody,

please could someone have a look on my probabely simple problem? I have three tables: People, Rulings and Direps\_Rulings, where direp is and alias for a person from People table.

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

I created a testing debug function in my SubjectsController.php which should list all people, who are related to subject through rulings for a given subject id:

 ![querry_code](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/979b275bb8a9e35dfd101fc80a66b357dc6a0454.png)

When I run function, I get correct SQL query:

 ![querry_code](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/979b275bb8a9e35dfd101fc80a66b357dc6a0454.png)

When I want to call the query and obtain all results(I just add -\>all() method), I get error:

 ![querry_code](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/979b275bb8a9e35dfd101fc80a66b357dc6a0454.png)

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

My question is? What’s wrong with my code?

When I run the SQL query in HeidiSQL, I am getting the correct results:

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

Please can anybody help me? Thank you in advance.

---

<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, 2022, 12:34am UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/2 "2022-01-22T00:34:38Z")

</div>

Seems Cake thinks that something is supposed to be an integer field, but it’s a string. Any chance you recently changed an integer field in the database to a string, but didn’t clear the ORM cache after doing so?

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [January 22, 2022, 2:48pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/3 "2022-01-22T14:48:29Z")

</div>

Thank you @Zuluru for your help. Finally, the error was in` 'conditions' =>['People.id' =>'dirrul.direp_id'];` It should be:` 'conditions' =>'People.id =dirrul.direp_id';` Here is the solution:

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

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 22, 2022, 2:57pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/4 "2022-01-22T14:57:29Z")

</div>

As you can see in the docs an associative array is only used if you want to use literal values (like `true`, a specific number, datetime or whatever) in your join condition.  
[https://book.cakephp.org/3/en/orm/query-builder.html#adding-joins](https://book.cakephp.org/3/en/orm/query-builder.html#adding-joins)

But if you want to reference other tables columns you need to put it all in one string.

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [January 22, 2022, 3:30pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/5 "2022-01-22T15:30:25Z")

</div>

Thank you 👍 I can’t believe that it took me several days to make one stupid join. But thanks to all of you and this forum I did it!

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [January 23, 2022, 1:25am UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/6 "2022-01-23T01:25:18Z")

</div>

You might feel even worse when you realize that once you’ve configured your associations properly, all you need is:

```php
$query = $this->People
    ->find()
    ->distinct()
    ->innerJoinWith('Rulings')
    ->where(['Rulings.subject_id' => $id]);

```

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 23, 2022, 12:00pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/7 "2022-01-23T12:00:30Z")

</div>

And using ‘bake’ 5 minutes?

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 23, 2022, 12:12pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/8 "2022-01-23T12:12:10Z")

</div>

Using the Bake CLI requires you to have a table structure named according to the CakePHP Naming Conventions.  
[https://book.cakephp.org/4/en/intro/conventions.html#cakephp-conventions](https://book.cakephp.org/4/en/intro/conventions.html#cakephp-conventions)

If you do that then all your associations etc. are auto-detected and added automatically.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 23, 2022, 2:04pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/9 "2022-01-23T14:04:30Z")

</div>

In this case, just (?):

- tablename ‘people’ to ‘peoples’
- fieldname ‘direp\_id’ to people\_id’

> [@Join tables error - need help please](https://discourse.cakephp.org/t/join-tables-error-need-help-please/10070/10):
>
> I offered to my boss to make a simple database system based on CakePHP

So, I guess since user\_looser is building the database, it’s not a problem to do the renaming?

> [@Join tables error - need help please](https://discourse.cakephp.org/t/join-tables-error-need-help-please/10070/10):
>
> I know very little from cakephp but I like it very much.

As I replied in the other post, my advice is to focus on the database and do multiple runs of the bake-command until you’re stuck on functionality (Think you still have to get the subjects into the play?).

There won’t be bugs, code is on the place where it has to be (e.g. joins in models), you can show your boss a first take of the app.

At that point there will be the need for more functionality (e.g. authentication etc.), at that point you can start work on that.

Just pointing to this because I hope you will keep liking CakePHP. Working several days on a join wouldn’t make me happy.

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [January 25, 2022, 11:01am UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/10 "2022-01-25T11:01:56Z")

</div>

Thank you for your advices. [drtrppr](https://discourse.cakephp.org/u/drtrppr) why do you propose to use “peoples”? Is not People plural form of Person?

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [January 25, 2022, 11:02am UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/11 "2022-01-25T11:02:54Z")

</div>

Wonderful, thank you [ndm](https://discourse.cakephp.org/u/ndm). You must always have a idol and your code is idol code for me now. I try to make it your way.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 25, 2022, 12:36pm UTC](https://discourse.cakephp.org/t/cannot-convert-value-of-type-string-to-integer-when-joining-multiple-tables/10105/12 "2022-01-25T12:36:05Z")

</div>

oops!  
person will be fine.
