4spacesdk/CI4OrmExtension

Failed to find relation error

Closed this issue · 1 comments

May be there is something wrong in my code, but I need to copy an item with all relations into another one and I receive this error:

Failed to find relation App\Models\Cms\AttachModel for App\Models\Cms\ContentModel
#0 /Users/.../cms-2022/appstarter/vendor/4spacesdk/ci4ormextension/DataMapper/EntityTrait.php(69): OrmExtension\Extensions\Model->getRelation('App\\Models\\Cms\\...')

This is the copy, I receive the error in the line where I save atts (attaches) $content->save($content->atts);

            ...
            $content->version++;
            $content->sects = $prevContentVersion->sects->find();
            $content->imgs = $prevContentVersion->imgs->find();
            $content->navs = $prevContentVersion->navs->find();
            $content->atts = $prevContentVersion->atts->find();
            $content->cats = $categoryContentModel->getCategoriesByContentId($prevContentVersion->content_id);
            
            $this->db->transStart();
            if ($content->publish) {
                $this->unpublishOtherVersions($content->master_id);
            }
            
            $master->save();
            $content->save();
            $content->save($content->secs);
            $content->save($content->imgs);
            $content->save($content->navs);
            $content->save($content->atts);
            $categoryContentModel->setCategoriesContent($content->cats, $content->content_id);
            ...

This is the relation in my AttachModel:

public $hasMany = [
	    'cont' => [
	        'class' => ContentModel::class,
	        'joinTable' => 'ptv_attaches_content',
	        'otherField' => 'att',
	        'joinSelfAs' => 'attach_fk',
	        'joinOtherAs' => 'content_fk'
	    ],
	];

And this is the relations list in my ContentModel:

public $hasMany = [
	    'nav' => [
	        'class' => NavigationModel::class,
	        'joinTable' => 'ptv_navigation_contents',
	        'otherField' => 'cont',
	        'joinSelfAs' => 'content_fk',
	        'joinOtherAs' => 'navigation_fk'
	    ],
	    'sect' => [
	        'class' => SectionModel::class,
	        'joinTable' => 'ptv_contents_sections',
	        'otherField' => 'cont',
	        'joinSelfAs' => 'content_fk',
	        'joinOtherAs' => 'section_fk'
	    ],
	    'img' => [
	        'class' => ImageModel::class,
	        'joinTable' => 'ptv_contents_images',
	        'otherField' => 'cont',
	        'joinSelfAs' => 'content_fk',
	        'joinOtherAs' => 'image_fk'
	    ],
	    'att' => [
	        'class' => AttachModel::class,
	        'joinTable' => 'ptv_attaches_content',
	        'otherField' => 'cont',
	        'joinSelfAs' => 'content_fk',
	        'joinOtherAs' => 'attach_fk'
	    ],
	    'categ' => [
	        'class' => CategoryModel::class,
	        'joinTable' => 'categories_contents',
	        'otherField' => 'content',
	        'joinSelfAs' => 'content_fk',
	        'joinOtherAs' => 'category_fk'
	    ],
	    
	];

PS I'm getting and setting manually categories because this relation doesn't work see #22.

@etcware
Since you are using custom relation names, you have to specify which relation you trying to save.
$content->save($content->atts, 'att');
Check the second argument.