PUGX / PUGXMultiUserBundle

An extension for FOSUserBundle to handle users of different types. Compatible with Doctrine ORM.
163 stars 96 forks source link

Error: Cannot instantiate abstract class UserBundle\Entity\User #150

Closed ahmed-sammari closed 7 years ago

ahmed-sammari commented 7 years ago

hello everybody, I tried to install PUGXMultiUserBundle, FOSUserBundle and VichUploaderBundle, now as I did all steps.

Entity Class

<?php

namespace UserBundle\Entity;

use Doctrine\ORM\Mapping\MappedSuperclass;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"user_candidat" = "UserCandidat", "user_recruteur" = "UserRecruteur"})
 * @MappedSuperclass
 */
abstract class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var Image
     *
     * @ORM\OneToOne(targetEntity="Image")
     */
    protected $image;

...........
}

Image Class

<?php

namespace UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @Vich\Uploadable
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ..................

}

My Error:

Error: Cannot instantiate abstract class UserBundle\Entity\User

            if (null !== $class) {
                return function (FormInterface $form) use ($class) {
                    return $form->isEmpty() && !$form->isRequired() ? null : new $class();
                };
            }

any help

garak commented 7 years ago

Remove "abstract" from your class definition

ahmed-sammari commented 7 years ago

Thanks for the reply, but when i remove it give me another problem

Entity 'UserBundle\Entity\User' has to be part of the discriminator map of 'UserBundle\Entity\User' to be properly mapped in the inheritance hierarchy. Alternatively you can make 'UserBundle\Entity\User' an abstract class to avoid this exception from occurring.

    public static function mappedClassNotPartOfDiscriminatorMap($className, $rootClassName)
    {
        return new self(
            "Entity '" . $className . "' has to be part of the discriminator map of '" . $rootClassName . "' " .
            "to be properly mapped in the inheritance hierarchy. Alternatively you can make '".$className."' an abstract class " .
            "to avoid this exception from occurring."
garak commented 7 years ago

Se, make Use part of the discriminator map

ahmed-sammari commented 7 years ago

Thank you so much. This step has solved a big problem. 👍

I think if they change it in documentation. 😉