Search code examples
phpsymfonycommandezpublish

eZ Publish 5 API - CreateContent Command error


I'm trying to create content following this example on the cookbook :

https://github.com/ezsystems/CookbookBundle/blob/master/Command/CreateContentCommand.php

For the moment i'm just trying to create 'folder' content, i have the following error while executing the script :

PHP Fatal error: Call to a member function attribute() on a non-object in /vagrant/application/ezpublish_legacy/kernel/search/plugins/ezsearchengine/ezsearchengine.php on line 53

Here is the code :

     foreach ($tabPreImportData as $object) {
        $output->writeln('<info> Object ID: ' . $object['id_object'] . '</info>');

        $objectToMigrate = eZContentObject::fetch($object['id_object']);

        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');

        foreach ($objectToMigrate->dataMap() as $attrIdentifier => $attrValue) {
            $contentCreateStruct->setField($attrIdentifier, $attrValue->DataText);
        }

        $locationCreateStruct = $locationService->newLocationCreateStruct(2);
        // create a draft using the content and location create struct and publish it
        $draft = $contentService->createContent($contentCreateStruct, array($locationCreateStruct));
        $content = $contentService->publishVersion($draft->versionInfo);
    }

The last line :

$content = $contentService->publishVersion($draft->versionInfo);

is causing the problem when i check the stack trace.

More info :

  • The Object is obtained from an eZ 4 Database
  • To be recreated in a eZ 5 Database.

Thanks.


Solution

  • You are mixing legacy code within new stack code, Re: 'eZContentObject::fetch'.

    I do not think this is necessary or correct as you seem to be running legacy code outside of a legacy context. You should be able to write your code without using legacy code. From what code you have shared, I do not see any need for legacy code and if I were you I would not use legacy code at all for this task.

    If you insist on using legacy code within new stack code you must only run it within a legacy kernel enclosure or else a great many unpredictable things may go wrong or plain not work at all.

    https://doc.ez.no/display/EZP/Legacy+code+and+features#Legacycodeandfeatures-Runninglegacycode

    This I think is the line which is erroring that you made reference to, https://github.com/ezsystems/ezpublish-legacy/blob/master/kernel/search/plugins/ezsearchengine/ezsearchengine.php#L53

    I would also no longer trust the database you are running the code you shared. You should drop the database as very likely corrupt, install a fresh copy from production backups, rewrite your code to not use legacy code since it's not required and try again.

    Also remember that eZ Platform is Data Compatible with eZ Publish meaning that the new stack code can read (and alter) database content created with eZ Publish since their is no difference at all in the new stack. Which also means that their is no need to use 'eZContentObject::fetch' even if it was within a legacy enclosure.

    PS. This thread is cross-posted from: http://share.ez.no/forums/ez-publish-5-platform/creating-content-with-ez-5-api-error