简体   繁体   中英

JMS Serializer Annotation

I'm using Doctrine2 and JMS Serializer , but the serializer is throwing a weird error:

AnnotationException: [Semantical Error] The class "Doctrine\\ORM\\Mapping\\PrePersist" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the class doc comment of "Doctrine\\ORM\\Mapping\\PrePersist". If it is indeed no annotation, then you need to add @IgnoreAnnotation("ORM\\PrePersist") to the class doc comment of method User\\User::prePersist().

That happens when I try to serialize an entity object. These are the relevant bits of the entity class:

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 * @ORM\Table(name="products", indexes={
 *     @ORM\Index(columns={"price"}),
 * })
 * @ORM\HasLifecycleCallbacks
 */
class Product
{
    // ...

    /**
     * @ORM\PrePersist
     */
    public function prePersist()
    {
        $this->createdAt = new \DateTime;
        $this->updatedAt = $this->createdAt;
    }

    // ...
}

I opened Doctrine\\ORM\\Mapping\\PrePersist and it is annotated with @Annotation . So the bug seems to be on JMS's side, not Doctrine2.

What can be causing this?

Note: The correct tag in this case would be 'jmsserializer', not 'jmsserializerbundle'. Someone please create it if appropriate and/or remove this note.

You're mixing up 2 things: Doctrine and JMSSerializer.

You should tell JMSSerializer exactly what you want to be serialized. So try to exclude all and annotate everything you want to have serialized. Like that:

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 * @ORM\Table(name="products", indexes={
 *     @ORM\Index(columns={"price"}),
 * })
 * @ORM\HasLifecycleCallbacks
 * @JMS\ExclusionPolicy("all")
 */
class Product
{
    
    /**
     * @var string
     *
     * @ORM\Column(name="serializableProperty1", type="guid")
     *
     * @JMS\Expose
     */
    private $serializableProperty1;

    /**
     * @ORM\PrePersist
     */
    public function prePersist()
    {
        $this->createdAt = new \DateTime;
        $this->updatedAt = $this->createdAt;
    }

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM