Migrating 4 to 5: Custom models


#1

I am trying to upgrade a Pimcore 4 website to Pimcore 5. The website has a news section which uses class objects and Dependency Injection. I have followed the migration instructions as best as I could, but I am having trouble with the namespaces.

I am getting the following error:

ClassNotFoundException: Attempted to load class "News" from namespace "Pimcore\Model\DataObject". Did you forget a "use" statement for "Pimcore\Model\Object\News"?

I do not understand this exception. “DataObject” is not something present in my own or in the generated classes. What do I need to do to get the depency injection working?

This is the code I have:

Dependency Injection configuration:
app/config/pimcore/di.php:

return [
    'Pimcore\Model\Object\News' => DI\object('Website\Model\News'),
    'Pimcore\Model\Object\News\List' => DI\object('Website\Model\News\Listing')
];

My Listing file at /src/Website/Model/News/Listing.php:

<?php

namespace Website\Model\News;

use Pimcore\Model\Object;

class Listing extends \Pimcore\Model\Object\News\Listing {
}

Generated listing file at /var/classes/Object/News/Listing.php:

<?php 

namespace Pimcore\Model\Object\News;

use Pimcore\Model\Object;

/**
 * @method Object\News current()
 */

class Listing extends Object\Listing\Concrete {

public $classId = 1;
public $className = "news";

}

My news.php at /src/Website/Model/News.php

<?php

namespace Website\Model;

use Pimcore\Model\Object;
use Carbon\Carbon;

class News extends Object\News {
}

#2

Hi,

Pimcore changed the Namespace for Objects in 5. ‘Object’ is a reserved keyword in PHP 7.1 and can’t be used anymore, so they changed it to DataObject instead. Can you try changing all your references to DataObject instead of Object?

Cheers
Dominik


#3

Hi Dominik,

Thank you very much for the answer. I have now managed to get things working!

For anyone else experiencing a similar issues, here are some pointers:

  • The class mapping that used to go in di.php now goes in /app/config/config.yml
  • In Pimcore admin interface, open every class and save it to regenerate the class.
  • Empty the cache after doing this
  • Put the PHP classes in /src/AppBundle/Model/DataObject
  • Use the namespace AppBundle\Model\DataObject for the class.
  • The class should extend \Pimcore\Model\DataObject\News (for news)

#4

Hi,
thank you very much for your summary. Also have a look at https://pimcore.com/docs/5.0.x/Extending_Pimcore/Overriding_Models.html

Cheers…