Welcome Guest, Not a member yet? Register   Sign In
Nesting level too deep - recursive dependency?
#1

Hi all,
For a project, I wrote a specific method that accepts posted data (via a javascript - by means of axios). 
The method is written to handle (ajax) file uploads and save file details in a database table. Since there are multiple file uploads in the, I tried to handle them all via this one method (below).
Following vars are send as formData:
  • (file upload) label : to handle validtaion errors 
  • file input name : corresponding a field in the database
  • type : there are 3 form types, so all starting with a different 3 letter code, followed by '_requests' (corresponding the model; e.g. 'con_requests')
  • id : the id of the record in the corresponding table (type, if form type is 'con', it will be the 'con_table', edited by means of the  'con_requests' model)
In the method below, the save($entity) action throws the 'Nesting level too deep - recursive dependency?' error. The weird thing is that some file uploads work perfect and are perfectly stored in the db table, where as others fail and throw this error. 
I can echo out the changed entity property, entity looks OK, even the hasChanged() method return TRUE... 
I don't have a clue how to fix it...  Huh

PHP Code:
public function axUpload()
    {
        $posted $this->request->getPost();
        
        
if(!array_key_exists("label"$posted) || !array_key_exists("file_input"$posted))
        {
            $data = [ 'success' => 'false''text' => 'No label or file input fieldname provided!' ];
            $res array_merge($data , ['statusText' => 'OK''request' => $posted]);
            return $this->respond($res);
        }

        $label $posted['label'];
        $filefield $posted['file_input'];
        $type $posted['type'];
        $id $posted['id'];

        $file $this->request->getFile($filefield); // CI File Object

        $rules = [
            $filefield => [
                'label' => $label,
                'rules' => [ 'uploaded['.$filefield.']''mime_in['.$filefield.',application/pdf]''max_size['.$filefield.',1500000]', ],
                'errors' => [ 'uploaded' => 'File was not uploaded''mime_in' => 'Only PDF files are allowed''max_size' => 'The uploaded file is too big', ]
            ],
        ];

        $validation service('validation');
        $validation->setRules($rules);


        if(! $this->validateData([$file], $rules))
        {
            $errors $validation->getErrors();
            $data = [ 'success' => 'false''errors' => $errors'mime' => $file->getMimeType(), 'size' => $file->getSize() ];

            $res array_merge($data , ['statusText' => 'OK''request' => $posted]);
            return $this->respond($res);
        }
        else
        {
            $fileExt $file->getClientExtension();
            $newName $this->user->initials '_' date('Ymd_His') . '__' strtolower($filefield) . '.' strtolower($fileExt);
            $path 'uploads/fcwo/'.$this->user->initials.'/'.date('Ymd').'/';

            // Move file to location and keep track of upload in DB
            if ($file->isValid() && !$file->hasMoved())
            {
                $file->move(WRITEPATH.$path$newName);
                $data = [ 'success' => 'true''filename' => $newName'path' => $path.$newName'size' => $file->getSize() ] ;

                // Save file info in DB, which table / type?
                $model $this->_createModelInstance($type); // creates corresponding model instance, if type = 'con', it returns the con_requests model
                $entity $model->find($id);

                unset($entity->$filefield);
                $entity->$filefield =  json_encode(['filename' => $newName'path' => $path.$newName]); // all ok until here!

                if ($entity->hasChanged()) // returns true
                {
                    if($model->save($entity)) 
                    
{
    /* And here, the script stops ...

    "Nesting level too deep - recursive dependency?
    Previous Exception
    CodeIgniter\\Database\\Exceptions\\DataException
    There is no data to update.
    #0 /earthweb2_ci4/CodeIgniter/system/BaseModel.php(1011): CodeIgniter\\Database\\Exceptions\\DataException::forEmptyDataset('update')
    #1 /earthweb2_ci4/CodeIgniter/system/Model.php(865): CodeIgniter\\BaseModel->update(Array, Array)
    #2 /earthweb2_ci4/CodeIgniter/system/BaseModel.php(749): CodeIgniter\\Model->update('6', Object(Modules\\Fcwo\\Entities\\ConEntity))
    #3 /earthweb2_ci4/CodeIgniter/modules/Fcwo/Controllers/Fcwo.php(249): CodeIgniter\\BaseModel->save(Object(Modules\\Fcwo\\Entities\\ConEntity))
    #4 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(933): Modules\\Fcwo\\Controllers\\Fcwo->axUpload()
    #5 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(509): CodeIgniter\\CodeIgniter->runController(Object(Modules\\Fcwo\\Controllers\\Fcwo))
    #6 /earthweb2_ci4/CodeIgniter/system/CodeIgniter.php(355): CodeIgniter\\CodeIgniter->handleRequest(NULL, Object(Config\\Cache), false)
    #7 /earthweb2_ci4/CodeIgniter/system/Boot.php(325): CodeIgniter\\CodeIgniter->run()
    #8 /earthweb2_ci4/CodeIgniter/system/Boot.php(67): CodeIgniter\\Boot::runCodeIgniter(Object(CodeIgniter\\CodeIgniter))
    #9 /earthweb2_ci4/WWW/index.php(56): CodeIgniter\\Boot::bootWeb(Object(Config\\Paths))\n#10 {main}",

        "file": "/earthweb2_ci4/CodeIgniter/system/Debug/Exceptions.php",
        "line": 602,
    */
                        return $this->respond($data);
                    }
                    else
                    {
                        // Info not saved in DB, remove uploaded file!
                        if(file_exists(WRITEPATH.$path.$newName))
                        {
                            unlink(WRITEPATH.$path.$newName);
                        }
                        $data = [ 'success' => 'false''errors' => [$filefield => 'Record could not be updated, try again''path' => $path.$newName] ] ;
                    }
                }
                die("Nothing has changed!");
            }

            $data = [ 'success' => 'false''errors' => [$filefield => 'File upload failed, invalid file or failed to move to uploads''path' => $path.$newName] ] ;
        }

        $res array_merge($data , ['statusText' => 'OK''request' => $posted]);

        return $this->respond($res);
    

Thanks in advance for your help!

Zeff
Reply
#2

Hi all,
If you have the same problem: check if your $allowedFields in your model are correct (corresponding with the DB table field names)!
You can indeed test by commenting them out or add the property $protectFields = false (documented here) to check if one (or more) of the fields in this allowedFields array is incorrect.
When it works after disabling the allowedFields property (uncomment $allowedFields or set $protectFields to false), just check with a getLastQuery() to see what is exactly in your query...
Good luck!

Zeff
Reply




Theme © iAndrew 2016 - Forum software by © MyBB