[Tuto Annotation Uploade Symfony] Annotation non détecté.

Par shensui, il y a 9 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.
j'ai suivi le tutorielle sur l'uploade de fichier grace aux annotation, sauf que arrivée a la partie sur l'annotation reader il ne détecte pas mes proprietée annotée.
pouvez vous me donner des piste pour résoudre mon problème.

mon entité

<?php namespace MangaBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Shensui\ExternalBundle\Annotation\Uploadable; use Shensui\ExternalBundle\Annotation\UploadableField; use Symfony\Component\HttpFoundation\File\File; /** * Manga * * @ORM\Table(name="manga") * @Gedmo\SoftDeleteable(fieldName="deleteAt", timeAware=false) * @ORM\Entity(repositoryClass="MangaBundle\Repository\MangaRepository") * @Uploadable() */ class Manga { /*mes champs*/ /** * @var \DateTime * @Gedmo\Timestampable(on="create") * @ORM\Column(name="created", type="datetime") */ private $created; /** * @var \DateTime * @Gedmo\Timestampable(on="update") * @ORM\Column(name="updated", type="datetime") */ private $updated; /** * @var dateTime * * @ORM\Column(name="deleteAt", type="datetime", nullable=true) */ private $deleteAt; /** * @var string * * @ORM\Column(name="cover", type="string", length=255, nullable=true) */ private $filename; /** * @UploadableField(filename="filename", path="covers") */ private $file; /*les geteur et seteur de l’entité*/ /** * @return File|null */ public function getFile() { return $this->file; } /** * @param File $file|null */ public function setFile($file) { $this->file = $file; } /** * @return string */ public function getFilename() { return $this->filename; } /** * @param string $filename */ public function setFilename($filename) { $this->filename = $filename; } }

mon UploadableField

<?php namespace Shensui\ExternalBundle\Annotation; use Doctrine\Common\Annotations\Annotation\Target; use InvalidArgumentException; /** * @Annotation * @Target("PROPERTY") */ class UploadableField { private $filename; private $path; public function __construct(array $options) { if (empty($options['filename'])) { throw new InvalidArgumentException("L'annotation UplodableField doit avoir un attribut 'filename'"); } if (empty($options['path'])) { throw new InvalidArgumentException("L'annotation UplodableField doit avoir un attribut 'path'"); } $this->filename = $options['filename']; $this->path = $options['path']; } /*les geteur et seteur*/ }

mon UploadAnnotationReader

<?php namespace Shensui\ExternalBundle\Annotation; use Doctrine\Common\Annotations\AnnotationReader; class UploadAnnotationReader { /** * @var AnnotationReader */ private $reader; public function __construct(AnnotationReader $reader) { $this->reader = $reader; } /** * @param $entity * @return bool */ public function isUploadable($entity) { $reflection = new \ReflectionClass(get_class($entity)); return $this->reader->getClassAnnotation($reflection, UploadableField::class) !== null; } /** * Liste les champs uploadable d'une entité (sous forme de tableau associatif) */ public function getUploadableFields($entity) { $reflection = new \ReflectionClass(get_class($entity)); if (!$this->isUploadable($entity)){ return []; } $properties = []; foreach($reflection->getProperties() as $property) { $annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class); if ($annotation !== null) { $properties[$property->getName()] = $annotation; } } return $properties; } } )

merci d'avence de votre aide.

9 réponses

betaWeb, il y a 9 ans

Salut,

Quelle est l'erreur retournée par Symfony ?

Lartak, il y a 9 ans

Bonsoir.
Tu devrais déplacer ton sujet dans le forum Symfony.

shensui, il y a 9 ans

@betawweb : justement aucunne, j'ai suivi le tuto et quant je dump:

/** * Liste les champs uploadable d'une entité (sous forme de tableau associatif) */ public function getUploadableFields($entity) { $reflection = new \ReflectionClass(get_class($entity)); if (!$this->isUploadable($entity)){ return []; } $properties = []; foreach($reflection->getProperties() as $property) { $annotation = $this->reader->getPropertyAnnotation($property, UploadableField::class); if ($annotation !== null) { $properties[$property->getName()] = $annotation; } } return $properties; }

il me retoutne au tableaux vide, alors que j'ai bien "fait" mes annotations.

betaWeb, il y a 9 ans

Tu as bien tout configurer ? Services etc ?

shensui, il y a 9 ans

nomalement oui, c'est pour ça que je comprend pas ??

betaWeb, il y a 9 ans

Beh tu as du faire une erreur qqpart, il te faut reprendre ton code de 0. C'est long mais c'est comme ça ^^

shensui, il y a 9 ans

je vais essayer, merci déjà de vous être poser sur mon problème.

Balbert, il y a 9 ans

Hello,

Si le problème persiste tu peux voir ce bundle qui fait pareil ->

Chen Roche, il y a 7 ans

Bonjour,

J'ai reglé mon pb avec une déclaration de service dans mon service.yml
upload.annotation_field:
class: UploadBundle\Annotation\UploadableField