I am using Cakephp4 File Uploads but i will give me "Object of class Laminas\Diactoros\UploadedFile could not be converted to string [CORE/src/Database/Type/StringType.php, line 97]"

Hi,
I am uploading a file using cakphp4 “File uploads Function”. But i will give me this error " Object of class Laminas\Diactoros\UploadedFile could not be converted to string [ CORE/src/Database/Type/StringType.php , line 97 ]".

Controller code

public function add()
{
$undertaker = $this->Undertakers->newEmptyEntity();
if ($this->request->is(‘post’)) {
$data = $this->request->getData();
$logo = $data[‘logo’];
$undertaker = $this->Undertakers->patchEntity($undertaker, $data);
if(!$undertaker->getErrors()) {
$name = $logo->getClientFilename();//uploads/undertakers/logo/1/f30db346571d4bc99f1875e6f4cbf840d0e61c79.jpg
$ext = pathinfo($name, PATHINFO_EXTENSION);
$folder = Text::uuid();
$fileName = Text::uuid();
$dir = new Folder(env(‘TEMPFILE_URL’, WWW_ROOT . ‘uploads’).DS .‘undertakers’.DS.‘logo’. DS . $folder, true, 0755);
$savepath = $dir->path .DS . $fileName.’.’.$ext;
$logo->moveTo($savepath);
$undertaker->logo = ‘uploads/undertakers/logo/’.$folder.’/’.$fileName.’.’.$ext;
}

        if ($this->Undertakers->save($undertaker)) {
            $this->Flash->success(__('The undertaker has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The undertaker could not be saved. Please, try again.'));
    }
    $this->set(compact('undertaker'));
}

If you look further back in the call stack for that error, I’m guessing that you will find the line with the patchEntity call on it. $data['logo'] is clearly an object, you are extracting it and then calling functions like getClientFilename and moveTo on it. But that column in the database is a string. So it’s telling you that it tried to convert that object to a string, and failed.

Simply adding unset($data['logo']); immediately before the patch should resolve it.

Hi, thanks for your reply. if i unset($data[‘logo’]) immediately before the patch in this case model validation not work.

Hi. i resolved the issue. just set these confugration in app.php return [
// …
‘App’ => [
// …
‘uploadedFilesAsObjects’ => false,
],
// …
];

I think you made it complicated.
Why not go the normal Cake-Way:

public function add()
{
$undertaker = $this->Undertakers->newEmptyEntity();
if ($this->request->is(‘post’)) {
$undertaker = $this->Undertakers->patchEntity($undertaker, $this->request->getData());
$logo=$this->request->getData(‘logo’); //
debug($logo); //=> gives you all the information of the uploaded file
die();