Cannot install plugin after add data through API Resource

Hi there,

 

I’m trying to create some PropertyGroup during installation of my plugin. I can create the data I want but I get an exception after and the plugin isn’t installed.

On my bootstrap.php I have

 public function install()
    {
        $this->configurePropertiesGroups();

        return array('success' => true, 'invalidateCache' => array('backend'));
    }


protected function configurePropertiesGroups()
    {
        $apiResourcePropertyGroup = new \Shopware\Components\Api\Resource\PropertyGroup();
        $apiResourcePropertyGroup->setManager($this->getEntityManager());

        
        // add Material Properties
        $this->createPropertyGroup($apiResourcePropertyGroup, 'Material', 1);
    }

    private function getEntityManager()
    {
        return Shopware()->Models();
    }

    protected function createPropertyGroup($apiResourcePropertyGroup, $name, $position)
    {

        $properties = array(
            'name' => $name,
            'position' => $position,
            'comparable' => 1,
            'sortmode' => 2
        );

        $apiResourcePropertyGroup->create($properties);

    }

And the exception I get is

„Unable to install, got exception: Entity has to be managed or scheduled for removal for single computation Shopware\Models\Plugin\Plugin@00000000231afc1800007f28a267451c“

After debug I check that exception happen on execution of Plugin/Namespace->flush($plugin).

 

As I see this is related to EntityManager, because on \Shopware\Components\Api\Resource\PropertyGroup()->create() method is executed the $em->flush($property).

Anyone can help how to create the PropertyGroup items using API on install method and get the plugin installed without errors?

 

Thanks

 

 

Hello,

you can use the entity manager directly. Example:

$property = new \Shopware\Models\Property\Group();
$property->fromArray(properties);

$this->getEntityManager()->persist($property);
$this->getEntityManager()->flush($property);

Then you doesn’t need the API ressource and doesn’t get any problems with the plugin entity.

Heiner

1 „Gefällt mir“