Cannot convert value to integer error attempting to save value to _joinTable

(cake3) I’m trying to save data to a join table with the following code in the controller

foreach ( $this->request->data['glass_products']['_ids'] as $key => $value ) {
    $this->request->data['glass_products']['_ids'][ (int) $key ] = array( 'id' => $value, '_joinData' => array( 'quantity' => (int) 99 ) );
}
$product = $this->Products->patchEntity( $product, $this->request->data );

But am getting a Cannot convert value to integer error. Screenshot of error: http://bit.ly/2nekPMx

I’ve followed the table naming conventions (glass_products_products in this case) and the error only occurs when the _joinData is added to the request object.

GlassProductsProducts

class GlassProductsProductsTable extends Table {
public function initialize( array $config ) {
parent::initialize( $config );
$this->table( 'glass_products_products' );
$this->primaryKey( 'id' );
$this->belongsTo( 'GlassProducts' );
$this->belongsTo( 'Products' );
}
}

GlassProducts
```class GlassProductsTable extends Table {public function initialize( array $config ) { parent::initialize( $config );`

	$this->table( 'glass_products' );
	$this->displayField( 'name' );
	$this->primaryKey( 'id' );

	$this->addBehavior( 'Timestamp' );

	$this->hasMany( 'GlassOrders', [
		'foreignKey' => 'glass_product_id'
	] );

	$this->belongsToMany( 'Products', [
		'foreignKey' => 'glass_product_id'
	] );
}

Products
class ProductsTable extends Table {

public function initialize( array $config ) {
	parent::initialize( $config );

	$this->table( 'products' );
	$this->displayField( 'name' );
	$this->primaryKey( 'id' );

	$this->addBehavior( 'Timestamp' );

	$this->hasMany( 'CustomerOrders', [
		'foreignKey' => 'product_id'
	] );

	$this->belongsTo( 'ProductTypes', [
		'foreignKey' => 'product_type_id'
	] );

	$this->belongsToMany( 'GlassProducts', [
		'foreignKey' => 'product_id'
	] );
}}

Can anyone help? I’m going round the houses with this one!

A bit more info on this, it is failing in Marshaller->merge: $value = $propertyMap[$key]($value, $entity);

$value =

Array
(
    [_ids] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [_joinData] => Array
                        (
                            [quantity] => 99
                        )

                )

            [1] => Array
                (
                    [id] => 5
                    [_joinData] => Array
                        (
                            [quantity] => 99
                        )

                )

        )

)

$key = “glass_products”;

Any ideas?

You cant combine _joinData with ids, you can do the foreach after the patchEntity call
or you can do it without the _ids key, i mean, the post data should look like this

'glass_products' => Array (
    ['0'] => Array (
        [id] => 4
        [_joinData] => Array
            (
                [quantity] => 99
            )
    ),
    ['1'] => Array (
        [id] => 5
        [_joinData] => Array
            (
                [quantity] => 99
            )
    )
)
1 Like

Raul, thank you SO much! I changed the array to not use the _ids key and it worked first time. I have been scratching my head with this one for so long, cheers!

you are right! Thank you

Hello
I have the same error in CakePHP 4 and php 8.0
If I pass the below its working fine
[glass_products] => Array
(
[_ids] => Array
(
[0] => 2
[1] => 1
)

    )

For an empty array, it does not work I pass an empty array in patchEntity

[glass_products] => Array
(

    )

It worked before but after upgrading to PHP 8 it is not work.
Can you please help me?