6
votes

I'm quite new to ZF2. I have a bunch of doctrine entities from my project in ZF1, one being a User entity.

I'm trying to extend \ZfcUserDoctrineORM\Entity\User so that I can inclue my old relationships - but with no great success.

If I try and run the schema generator ./doctrine-module orm:schema-tool:create I get an error saying the table name user already exists.

So far I've tried extending the class and setting my class as the UserEntityClass

return array(
    'zfcuser' => array(
        'UserEntityClass' => '\Application\Entity\User',
    ),
);

There doen't seem to be any documentation for the module as yet.

3

3 Answers

7
votes

So the problem was that the default ZfcUserDoctrineORM entities were still in play. To solve this you can set EnableDefaultEntities to false like so :

return array(
    'zfcuser' => array(
        'UserEntityClass' => '\Acme\Entity\User',
        'EnableDefaultEntities' => false
    ),
);
2
votes

I'm going from memory here, as I played around with ZfcUser[DoctrineORM] some time back.

You don't want to extend anything. Instead, just write your own User entity and make it implement ZfcUser\Entity\UserInterface.

Then make sure you set your configuration correctly (to use your own implementation of UserInterface, instead of the default), and you should be good to go.

0
votes

Though the OP's answer is correct, it's not complete. Below what I've done and why.

Create own User Entity. AbstractEntity is one used for all my Entities, contains just the ID property, getters/setter and some global functionality for Entity debugging.

User.php

use Doctrine\ORM\Mapping as ORM;
use Keet\Mvc\Entity\AbstractEntity;
use ZfcUser\Entity\UserInterface;

/**
 * Entity Class representing a post of our User module.
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Keet\User\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks
 */
class User extends AbstractEntity implements UserInterface
{
    /**
     * @var string
     * @ORM\Column(name="username", type="string", length=255, unique=true, nullable=false)
     */
    protected $username;

    /**
     * @var string
     * @ORM\Column(name="email", type="string", length=255, unique=true, nullable=false)
     */
    protected $email;

    /**
     * @var string
     * @ORM\Column(name="display_name", type="string", length=255, nullable=false)
     */
    protected $displayName;

    /**
     * @var string
     * @ORM\Column(name="`password`", type="string", length=255, nullable=false)
     */
    protected $password;

    /**
     * @var int
     * @ORM\Column(name="state", type="integer", length=3, nullable=false)
     */
    protected $state = 1;

    // Getters/Setters
}

zfcuser.config.php

Contains the same config as OP's answer:

'zfcuser' => [
    //Some other config
    'userEntityClass' => User::class,
    'EnableDefaultEntities' => false,
],

module.config.php

IMPORTANT: Overwrite the default doctrine entity config of zfcuser!

'doctrine' => [
    'orm_autoload_annotations' => true,
    'driver' => [
        __NAMESPACE__ . '_driver' => [
            'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',
            'cache' => 'array',
            'paths' => [
                __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'
                . DIRECTORY_SEPARATOR . 'Entity',
            ]
        ],
        'orm_default'             => [
            'drivers' => [
                __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver',
                'ZfcUser\Entity'  => __NAMESPACE__ . '_driver'
            ],
        ],
        'zfcuser_entity' => [ // Section overwrites the default config for location of Annotation. Original found
            // in vendor ZfcUserDoctrineModule /config/xml/zfcuser/ZfcUser.Entity.User.dcm.xml
            'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',
            'paths' =>
                __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src'
                . DIRECTORY_SEPARATOR . 'Entity',
        ],
    ],
],

In the above you'd normally just have the 'driver' and 'orm_default' configs. ZfcUser module contains a the 'zfcuser_entity' config, but points it to the driver within it's own module. To not have a floating reference to a different driver, overwrite it and point it towards your own defined driver, defined by __NAMESPACE__ . '_driver'. Also, in my example I use the AnnotationDriver for reading annotation, whereas the ZfcUser module uses the XmlDriver. ZfcUser module config below

This is the original config, overwritten in the above example

'zfcuser_entity' => array(
    'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
    'paths' => __DIR__ . '/xml/zfcuser'
),

This config allows you to fully use your own Entity, but still use the ZfcUser module.

In my case I can use my own AbstractActionController with my self-written CRUD actions, which expect a child of AbstractEntity. This saves a lot of writing/thinking ;)