I stored in session object entity, is it possible to convert this object back to an array?
Because the patch entity does not merge the data.
$data = [
'id' => 1,
'users_documents_items' => $ContentArray['users_documents_items'], **<< this is object entity, if it array - patch entity works, if entity object, doestn patch entity...**
];
$ContentUserDocument = $this->UsersDocuments->newEmptyEntity();
$ContentUserDocument = $this->UsersDocuments->patchEntity($ContentUserDocument, $data, [
'associated' => ['UsersDocumentsItems']
]);
Thank you!
There is a toArray()
method available for entities.
$entityObj->toArray()
If applicate to
$ContentArray[‘users_documents_items’]->toArray();
it doesnt work… (Call to a member function toArray() on array)
This variable from session looks like this
[
(int) 0 => object(App\Model\Entity\UsersDocumentsItem) {
'name' => 'test',
'type' => 'black',
... etc
},
(int) 1 => object(App\Model\Entity\UsersDocumentsItem) {
... etc
This is works, but get only one item…
$ContentArray['users_documents_items'][0]->toArray();
Is it possible to get all the records in the array? No object?
If you’re trying to do more than one entity:
$TableObj->patchEntities($array_of_records)
note the plural ‘Entities’
Its doesnt works, because the array has nested entity objects. If I insert a clean array without an object, it works … so I need to convert an array with entity objects to a clean array.
The solution to how to do this is as follows:
foreach($ContentArray['users_documents_items'] as $arr) {
$arr2[] = $arr->toArray();
}
Is there any other solution to the internal function of phpcake?
Loop through your containing array and array-ify each object. Then you will have an array of arrays.
Use any php loop structure you want; foreach, for, while… or use Cake’s Collection::map or Collection::reduce.
https://book.cakephp.org/4/en/core-libraries/collections.html
2 Likes