Can not use entities when insert or update

I’m trying to add or update the data on the current model’s table but it’s not working.

$employeesTable = TableRegistry::get('Employees'); 
$entity = $employeesTable->newEntity($data);
$employeesTable->save($entity);
// Tried also with $this instead of $employeesTable 

When I insert/update with this I’m getting this error:

Error: [InvalidArgumentException] Unable to call method "" in "default" provider for field "name"
Request URL: /myproject/employees/saveDetails/18
Stack Trace:
#0 C:xampphtdocsmyprojectvendorcakephpcakephpsrcValidationValidator.php(567): CakeValidationValidationRule->process('xD0x98xD0xB2xD0xB0xD0xBD', Array, Array)
#1 C:xampphtdocsmyprojectvendorcakephpcakephpsrcValidationValidator.php(131): CakeValidationValidator->_processRules('name', Object(CakeValidationValidationSet), Array, true)
#2 C:xampphtdocsmyprojectvendorcakephpcakephpsrcORMMarshaller.php(181): CakeValidationValidator->errors(Array, true)
#3 C:xampphtdocsmyprojectvendorcakephpcakephpsrcORMMarshaller.php(119): CakeORMMarshaller->_validate(Array, Array, true)
#4 C:xampphtdocsmyprojectvendorcakephpcakephpsrcORMTable.php(1901): CakeORMMarshaller->one(Array, Array)
#5 C:xampphtdocsmyprojectsrcModelTableEmployeesTable.php(550): CakeORMTable->newEntity(Array)
#6 C:xampphtdocsmyprojectsrcControllerEmployeesController.php(145): AppModelTableEmployeesTable->saveDetails('18', Array)
#7 [internal function]: AppControllerEmployeesController->saveDetails('18')
#8 C:xampphtdocsmyprojectvendorcakephpcakephpsrcControllerController.php(410): call_user_func_array(Array, Array)
#9 C:xampphtdocsmyprojectvendorcakephpcakephpsrcRoutingDispatcher.php(114): CakeControllerController->invokeAction()
#10 C:xampphtdocsmyprojectvendorcakephpcakephpsrcRoutingDispatcher.php(87): CakeRoutingDispatcher->_invoke(Object(AppControllerEmployeesController))
#11 C:xampphtdocsmyprojectwebrootindex.php(37): CakeRoutingDispatcher->dispatch(Object(CakeNetworkRequest), Object(CakeNetworkResponse))
#12 {main}

When I use the query builder it’s working. I’m also able to use this code for other models, it’s not working only on the current model where I’m writing my code.

So for now I’m inserting the current model’s data with the query builder and the related data on the other tables with entities. It’s working like this:

$this->connection()->begin();
$query = $this->query()
    ->update()
    ->set([
        'name'        => $data['name'],
        'surname'     => $data['surname'],
        'family'      => $data['family'],
        'pin'         => $data['pin'],
        'id_card_number' => $data['id_card_number'],
        'id_card_issued_from'   => $data['id_card_issued_from'],
        'id_card_valid_to'   => $data['id_card_valid_to'],
        'id_card_issued_by'   => $data['id_card_issued_by'],
        'education_id'   => $data['education_id'],
        'updated'   => new DateTime('now'),
    ])
    ->where(['id' => $data['id']])
    ->execute();

// Update the related user
$usersData = $data['users'];
$usersData['id'] = $data['user_id'];
$usersData['updated'] = new DateTime('now');

$usersTable = TableRegistry::get('Users');
$entity = $usersTable->newEntity($usersData);
$usersTable->save($entity);

$this->connection()->commit();

Any ideas why it’s not working?


Source: mysql

Leave a Reply