Hi, I have a flying problem and can’t figure out what it is. I have an editUser method for the table User. The table user has a left join against the table Profile. In the table Profile I have the fields: userUuid, image, tags and describtion. And now to my problem. I can edit all fields together and generate revised entries in the table, but I cannot edit any field individually. Then an error occurs in my method. Here is my controller for a better understanding:
public function editUser($userUuid = null) {
$associated = ['Profiles','Settings'];
$user = $this->Users->get($userUuid, [
'contain' => $associated
]);
$uploadPath = WWW_ROOT . DS .'shared' . DS . 'upload'. DS ;
if ($this->request->is(['patch', 'post', 'put'])) {
$requestData = $this->request->getData();
$user = $this->Users->patchEntity($user, $requestData);
$image = $this->request->getUploadedFile('profile.image');
var_dump($image->getError());
if(isset($image) && $image->getError() !== UPLOAD_ERR_NO_FILE){
$name = $image->getClientFilename();
$targetPath = $uploadPath . $name;
$image->moveTo($targetPath);
$user->profile->image = '/shared/upload/' . $name;
}else{
log::error("File upload error: " . $image->getError());
}
if ($this->Users->save($user)) {
log::debug((string)$user);
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['controller' => 'Dashboards', 'action' => 'index', $userUuid]);
} else {
log::error((string)$user);
$this->Flash->error(__('The user could not be saved. Please try again.'));
}
}
$this->set(compact('user'));
}
And my Profile table Class:
class ProfilesTable extends Table
{
public function initialize(array $config): void {
parent::initialize($config);
$this->setTable('profiles');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'userUuid'
]);
$this->hasOne('Settings', [
'foreignKey' => 'userUuid'
]);
}
public function validationDefault(Validator $validator): Validator
{
$validator
->scalar('userUuid')
->maxLength('userUuid', 255)
->allowEmptyString('userUuid')
->add('userUuid', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->scalar('image')
->allowEmptyArray('image');
$validator
->scalar('tags')
->maxLength('tags', 245)
->allowEmptyString('tags');
$validator
->scalar('description')
->allowEmptyString('description');
return $validator;
}
}
Thanks for any advice