Jmsserializerbundle: Anotation "@Enum" was never imported error

Created on 24 Apr 2014  路  26Comments  路  Source: schmittjoh/JMSSerializerBundle

Hi guys,

I'm having an error trying to serialize an Doctrine Entity. I get the following error:

[Semantical Error] The annotation "@Enum" in property Doctrine\ORM\Mapping\ManyToMany::$fetch was never imported. Did you maybe forget to add a "use" statement for this annotation?

The entity has excluded all the properties explicitly, and some are exposed with the @Expose method. Here is the code:

<?php

namespace Aaa\Bundle\CoreBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

use FOS\UserBundle\Model\User as BaseUser;
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;


/**
 *
 * @ORM\Entity
 * @ORM\Table(name="aaa_as_user")
 * @ExclusionPolicy("all")
 *
 */
class AsUser extends BaseUser
{
    /**
     * @var integer $id
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Expose
     */
    protected $id;

    /**
     * @var integer $aaaId
     * @ORM\Column(type="string", length=13, nullable=true)
     */
    protected $aaaId;

    /**
     * @var string $firstName
     * @ORM\Column(type="string", length=255)
     * @Expose
     */
    protected $firstName;

    /**
     * @var string $lastName
     * @ORM\Column(type="string", length=255)
     */
    protected $lastName;

    /**
     * @var DateTime $createdAt
     * @ORM\Column(name="created_at", type="datetime")
     * @Gedmo\Timestampable(on="create")
     * @Expose
     */
    protected $createdAt;
...
}

So, I don't have any ideas what could be wrong. It seems that the doctrine anotations are not being loaded correctly in the anotations I use here.

I've read a similar issue here but it's not a proxy issue so I have no idea how to solve this (as the property that has the ManyToOne is excluded, as you can see in the code above; only the present properties are exposed).

So.. is this a bug? Or I'm missing some config? Any ideas?

Thanks a lot for your help!

Most helpful comment

I had the same error, the problem was my fault, because after using createQueryBuilder(), I forgot to add ->getQuery()->getResult().
A sane error message would be nice, though.

All 26 comments

@schmittjoh I'm closing this as this issue seems not to be related with this bundle but with sylius resource bundle, sorry about this!

Hi,
I'm not using sylius resource bundle, yet I get the error :

[Semantical Error] The annotation "@Enum" in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a "use" statement for this annotation?

@SpadesTheOriginal which version of doctrine/annotations are you using ? @Enum is part of Doctrine annotations itself

I'm not using doctrine/annotations. That's why I'm confused, yet I have the same error as the OP.

I'm using JMS\Serializer\Annotation

@SpadesTheOriginal JMSSerializer uses the doctrine/annotations package to parse the annotations

Do I need to specify "use doctrine\annotations" in my user class then ?

It is not a class. It is a composer package. I'm asking you which version of this package is used in your project.
composer show -i will tell you

doctrine/annotations v1.2.0

then it is really weird

@stof I don't see @Enum in documentation but the class exists.
Same issue on my side :/
edit: fixed using jsm/serializer-bundle at 0.13.0 AND getArrayResult() instead of getResult()

i have same issue

[Semantical Error] The annotation \"@Enum\" in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a \"use\" statement for this annotation?

and only when using Doctrine\ORM\Tools\Pagination\Paginator;

I had the same error, the problem was my fault, because after using createQueryBuilder(), I forgot to add ->getQuery()->getResult().
A sane error message would be nice, though.

@solazs, you just save me some time wasting!

@solazs Same here, thanks ! +1 for a comprehensive error message :)

@solazs can you write down an example? I'm also in trouble...
I tried some ways, but still...error promped.

I checked composer show -i. And as I just checked I'm using the last of everything :(
doctrine/annotations 1.2.3
jms/serializer 0.16.0
jms/serializer-bundle 0.13.0

(message deleted to avoid confusion)

@solazs never mind. Solved :'p

I was trying to parse an Paginator instance with JMSSearializer (I'm using FOS Rest).

//...on the repo
        $query->orderBy('o.created', 'ASC')
                ->setMaxResults($maxResults)
                ->setFirstResult($firstResult);
        $paginator = new Paginator($query, true);
        try {
            return $paginator;
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }


//...on the fos rest controller
        //Paginator object returned
        $paginatorResult = $em->getRepository('BicEntityBundle:Whatever')->findSearched($what, $firstResult, $nResults);

        return $this->handleView(
                $this->view(
                        //because Paginator class implements IteratorAggregate
                        $paginatorResult->getIterator()->getArrayCopy(), 200
                        )
                //just to create the pagination
                ->setHeader('n_results', count($paginatorResult)));

Hope it helps someone.

Im still gettig this error:

The annotation '@Enum' in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a 'use' statement for this annotation?

Im just trying to serialize a paginator object:

$paginator = new Paginator($query, $fetchJoinCollection = true);
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
$jsonContent = $serializer->serialize($paginator, 'json');

These are my composer.json packages installed:

doctrine/annotations v1.2.3 Docblock Annotations Parser

You can not parse paginator object straight away (JMSSerializer is not prepared for that), do the following:

$paginatorResult->getIterator()->getArrayCopy()

If you are using PagerFanta as your paginator, you must do something like this:

```$objPager = new Pagerfanta(new DoctrineORMAdapter($objQuery));
$objPager->setMaxPerPage(100);
$objPager->setCurrentPage(1);
$objPager->getCurrentPageResults()->getArrayCopy()


@solazs :+1: for saving my time :)

Very helpful guys Thanks!!

Hi, i have the same issue but when i use DoctrineCommonCollectionsCriteria to find rows:

[Semantical Error] The annotation "@Enum" in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a "use" statement for this annotation?

When i use

$oItemsRows = $this->getEntityManager()->getRepository('Application\Entity\Item')->findBy(array(), $aOrder, $iLimit);

is serialized right but when i use:

$oCriteria = Criteria::create();
$oCriteria->setMaxResults(2);

$oItemsRows = $this->getEntityManager()->getRepository('Application\Entity\Item')->matching($oCriteria);

i got error... Any suggestion?

Regards

I guess that the object returned is not an array.
Try to convert it to an array, then you'll be able to parse it.

@solazs solution worked for me :+1:

@solazs TY man, it's help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Chrysweel picture Chrysweel  路  7Comments

grimabe picture grimabe  路  6Comments

yyaremenko picture yyaremenko  路  4Comments

pdugas picture pdugas  路  9Comments

simshaun picture simshaun  路  9Comments