patchEntity create new entity on associated model


#1

Hi,

I’ve got a really weird bug. I analyzed all model and didn’t find where is this issue.

When I use patchentity, one the entity associated got a new empty item

        $this->Kpiprofiles->patchEntity($kpiprofile, $this->request->data,[
            'validate' => true,
            'associated' => ['Teams','Kpitypes','Charts']
        ]);

This action add this entity to kpitypes object:
,
(int) 2 => object(App\Model\Entity\Kpitype) {

		'[new]' => true,
		'[accessible]' => [
			'*' => true,
			'id' => false,
			'_joinData' => true
		],
		'[dirty]' => [],
		'[original]' => [],
		'[virtual]' => [],
		'[errors]' => [],
		'[invalid]' => [],
		'[repository]' => 'Kpitypes'

I don’t understand why ? All models seems to be correct…and no errors from validators. Data stored into “this->request->data” are also correct…

So my workaround is to unset the last value (as it’s always empty) with:

unset($kpiprofile->kpitypes[count($kpiprofile->kpitypes)-1]);

Do you have any idea ?


#2

what is your request data?

Maybe you can/should trim post data (second example)


#3

Yes I could and I thought about that but almost all are integer so no needs to trim it ;))) And I also have two others associated models which are saved in same time, but no issue with.

[
	'name' => 'Test profile',
	'teams' => [
		'_ids' => [
			(int) 0 => '3',
			(int) 1 => '4',
			(int) 2 => '26',
			(int) 3 => '76'
		]
	],
	'charts' => [
		'_ids' => [
			(int) 0 => '1',
			(int) 1 => '2'
		]
	],
	'description' => 'test',
	'author_id' => '334',
	'kpitypes' => [
		(int) 1 => [
			'id' => '1'
		],
		(int) 2 => [
			'id' => '2'
		],
		(int) 3 => [
			'id' => '0'
		],
		(int) 4 => [
			'id' => '0'
		],
		(int) 5 => [
			'id' => '0'
		],
		(int) 6 => [
			'id' => '0'
		],
		(int) 7 => [
			'id' => '0'
		],
		(int) 8 => [
			'id' => '0'
		],
		(int) 9 => [
			'id' => '0'
		],
		(int) 10 => [
			'id' => '0'
		],
		(int) 11 => [
			'id' => '0'
		],
		(int) 13 => [
			'id' => '0'
		],
		(int) 14 => [
			'id' => '0'
		],
		(int) 15 => [
			'id' => '0'
		],
		(int) 16 => [
			'id' => '0'
		],
		(int) 17 => [
			'id' => '0'
		],
		(int) 18 => [
			'id' => '0'
		],
		(int) 19 => [
			'id' => '0'
		],
		(int) 20 => [
			'id' => '0'
		],
		(int) 21 => [
			'id' => '0'
		],
		(int) 22 => [
			'id' => '0'
		],
		(int) 23 => [
			'id' => '0'
		]
	]
]

This is my request->data and in index ‘kpitypes’, after the patchentity, this null item will be added. I checked all my models, everything seems to be correct…Maybe a bug from cakephp ? I have never seen that before, I don’t understand why, one null item is added when I edit form…(For information, I also have no triggers into my tables)


#4

My guess is that since the id passed is 0, it is marke as new (not bad), but then the kpitypes model allows it in the validation.
You should enforce some other validation to the creation of a kpitype (e.g: name?)

You can also filter out those ids like the first example of the link i posted.


#5

Yes i thought about that, but it raises an exception, and if I use no validate, patchEntity create this null entry …
I will check around “beforeMarshal” :wink: