IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

[Form] Formulaire imbriqué avec des uploads [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 85
    Points : 77
    Points
    77
    Par défaut [Form] Formulaire imbriqué avec des uploads
    Bonjour ,

    Je voudrai avoir un formulaire avec ajout d'une marque un upload de son logo et ajout x de photo de diaporama pour celle-ci.

    J'ai bien sur étais sur la doc et autre site avant de poste et voici ce que j'ai fais.

    Dans un premier temps j'ai mes deux entités :
    Brands :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    <?php
     
    namespace Ilan\BackBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
     
    /**
     * Brands
     *
     * @ORM\Table(name="brands")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Brands
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="brands_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $brandsId;
     
        /**
         * @var string
         *
         * @ORM\Column(name="brands_title", type="string", length=255, nullable=true)
         */
        private $brandsTitle;
     
        /**
         * @var string
         * 
         * @ORM\Column(name="brands_logo", type="string", length=255, nullable=true)
         * @Assert\File(maxSize="6000000")
         */
        private $brandsLogo;
     
        /**
         * @var string
         *
         * @ORM\Column(name="brands_slug", type="string", length=255, nullable=true)
         */
        private $brandsSlug;
     
     
        /**
         * @var \SlideshowBrands
         * @ORM\OneToMany(targetEntity="SlideshowBrands")
         */
        private $slideshowBrands;
     
     
        /**
         * 
         */
        public function __construct()
        {
            $this->slideshowBrands = new ArrayCollection();
        }
     
        /**
         * Get brandsId
         *
         * @return integer 
         */
        public function getBrandsId()
        {
            return $this->brandsId;
        }
     
        /**
         * Set brandsTitle
         *
         * @param string $brandsTitle
         * @return Brands
         */
        public function setBrandsTitle($brandsTitle)
        {
            $this->brandsTitle = $brandsTitle;
     
            return $this;
        }
     
        /**
         * Get brandsTitle
         *
         * @return string 
         */
        public function getBrandsTitle()
        {
            return $this->brandsTitle;
        }
     
        /**
         * Set brandsLogo
         *
         * @param string $brandsLogo
         * @return Brands
         */
        public function setBrandsLogo(UploadedFile $brandsLogo)
        {
            $this->brandsLogo = $brandsLogo;
     
            return $this;
        }
     
        /**
         * Get brandsLogo
         *
         * @return string 
         */
        public function getBrandsLogo()
        {
            return $this->brandsLogo;
        }
     
        /**
         * Set brandsSlug
         *
         * @param string $brandsSlug
         * @return Brands
         */
        public function setBrandsSlug($brandsSlug)
        {
            $this->brandsSlug = $brandsSlug;
     
            return $this;
        }
     
        /**
         * Get brandsSlug
         *
         * @return string 
         */
        public function getBrandsSlug()
        {
            return $this->brandsSlug;
        }
     
        /**
         * Set slideshowbrands
         *
         * @param \Ilan\BackBundle\Entity\SlideshowBrands $slideshowbrands
         * @return 
         */
        public function setSlideshowBrands(ArrayCollection $slideshowbrands )
        {
            $this->slideshowBrands = $slideshowbrands;
     
            return $this;
        }
     
        /**
         * Get slideshowbrands
         *
         * @return \Ilan\BackBundle\Entity\SlideshowBrands 
         */
        public function getSlideshowBrands()
        {
            return $this->slideshowBrands;
        }
     
     
        public function getUploadDir()
        {
          return 'uploads/img';
        }
     
        protected function getUploadRootDir()
        {
          return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }
     
        /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
        public function preUpload()
        {
     
          if (null === $this->brandsLogo) {
            return;
          }
     
        }
     
        /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
        public function upload()
        {
     
          if (null === $this->brandsLogo) {
            return;
          }
     
          $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->brandsLogo->getClientOriginalName() );
     
          $this->brandsLogo->move($this->getUploadRootDir(), $name );
     
        }
     
     
     
    }
    SlideshowBrands :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    <?php
     
    namespace Ilan\BackBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
     
    /**
     * SlideshowBrands
     *
     * @ORM\Table(name="slideshow_brands")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class SlideshowBrands
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="slideshow_brands_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $slideshowBrandsId;
     
        /**
         * @var string
         *
         * @ORM\Column(name="slideshow_brands_picture", type="string", length=255, nullable=true)
         * @Assert\File(maxSize="6000000")
         */
        private $slideshowBrandsPicture;
     
        /**
         * @var \Brands
         *
         * @ORM\ManyToOne(targetEntity="Brands")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="brands_id", referencedColumnName="brands_id")
         * })
         */
        private $brands;
     
     
     
        /**
         * Get slideshowBrandsId
         *
         * @return integer 
         */
        public function getSlideshowBrandsId()
        {
            return $this->slideshowBrandsId;
        }
     
        /**
         * Set slideshowBrandsPicture
         *
         * @param string $slideshowBrandsPicture
         * @return SlideshowBrands
         */
        public function setSlideshowBrandsPicture(UploadedFile $slideshowBrandsPicture)
        {
            $this->slideshowBrandsPicture = $slideshowBrandsPicture;
     
            return $this;
        }
     
        /**
         * Get slideshowBrandsPicture
         *
         * @return string 
         */
        public function getSlideshowBrandsPicture()
        {
            return $this->slideshowBrandsPicture;
        }
     
        /**
         * Set brands
         *
         * @param \Ilan\BackBundle\Entity\Brands $brands
         * @return SlideshowBrands
         */
        public function setBrands(\Ilan\BackBundle\Entity\Brands $brands = null)
        {
            $this->brands = $brands;
     
            return $this;
        }
     
        /**
         * Get brands
         *
         * @return \Ilan\BackBundle\Entity\Brands 
         */
        public function getBrands()
        {
            return $this->brands;
        }
     
     
        public function getUploadDir()
        {
          return 'uploads/img';
        }
     
        protected function getUploadRootDir()
        {
          return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }
     
     
     
        /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
        public function preUpload()
        {
     
          if (null === $this->slideshowBrandsPictur) {
            return;
          }
     
        }
     
        /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
        public function upload()
        { 
     
          if (null === $this->slideshowBrandsPictur) {
            return;
          }
     
          $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->slideshowBrandsPictur->getClientOriginalName() );
     
          $this->slideshowBrandsPictur->move($this->getUploadRootDir(), $name );
     
        }
     
    }
    Mon doctrine à généré automatiquement dans SlideshowBrands le getBrands la relation ManyToOne. J'ai alors ajouter un OneToMany car j'avais une erreur comme quoi ce n'est pas une instance...

    Voici mes deux classes de formulaire:

    BrandssAddType:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <?php
     
    namespace Ilan\BackBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
     
    class BrandsAddType extends AbstractType {
     
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('brands_title', 'text')
                    ->add('brands_logo',  'file')
                    ->add('slideshowbrands', 'collection', array('type' => new \Ilan\BackBundle\Form\Type\SlideshowBrandsAddType(),
                                                  'allow_add'    => true,
                                                  'allow_delete' => true,));
        }
     
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Ilan\BackBundle\Entity\Brands'
            ) );
        }
     
     
        public function getName()
        {
            return 'brandsAdd';
        }
     
    }
    SlideshowBrandsAddType:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <?php
     
    namespace Ilan\BackBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
     
    class SlideshowBrandsAddType extends AbstractType {
     
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('slideshowBrandsPicture', 'file');
     
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Ilan\BackBundle\Entity\SlideshowBrands'
            ) );
        }
     
     
        public function getName()
        {
            return 'slideshowBrandsAdd';
        }
     
    }
    Ici j'ai suivie la doc de symfony. Normalement tout est bon.

    Ensuite voici mon controller:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?php
     
    namespace Ilan\BackBundle\Controller;
     
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Ilan\BackBundle\Entity\Brands;
    use Ilan\BackBundle\Form\Type\BrandsAddType;
    use Symfony\Component\HttpFoundation\Request;
     
    class BrandsController extends Controller {
     
        public function indexAction() {
            return $this->render('IlanBackBundle:Brands:index.html.twig');
        }
     
        public function addAction(Request $request) {
     
            $brands = new Brands();
     
            $form = $this->createForm(new BrandsAddType(), $brands);
     
     
            if ($request->isMethod('POST')) {
     
                $form->bind($request);
     
                if ($form->isValid()) {
     
                    $this->redirect($this->generateUrl('/admin/marques'));
                }
            }
     
     
     
            return $this->render('IlanBackBundle:Brands:add.html.twig', array(
                        'form' => $form->createView(),
            ));
        }
     
    }
    Et la mon problème c'est que je ne rentre pas dans mon isValid. Je ne vois pas d'ou cela viens, car je rempli bien tout mon formulaire.

    Voici mon twig

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    {% extends "::admin_layout.html.twig" %}
    {% block body %}
        {# Inclusion template Header #}
        {% include "IlanBackBundle:Default:header.html.twig" %}
     
    <div class="row-fluid">
        <div class="span7">
            <div class="box paint color_3">
                <div class="title">
                    <h4><span>Ajout d'une marque</span> </h4>
                </div>
                <div class="content">
                    <form action="{{ path('ilan_back_marques_add') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal row-fluid" >
                        <div class="form-row control-group row-fluid">
                            <label class="control-label span3" for="normal-field">{{ form_label(form.brands_title, "Titre de la marque") }}</label>
                            <div class="controls span9">
                                    {{ form_widget(form.brands_title) }}
                            </div>
                        </div>
                        <div class="form-row control-group row-fluid">
                            <label class="control-label span3" for="search-input">{{ form_label(form.brands_logo, "Ajouter un logo") }}</label>
                            <div class="controls span9">
                                <div class="input-append row-fluid">
                                    {{ form_widget(form.brands_logo) }}
                                </div>
                            </div>
                        </div>
                        <div class="form-row control-group row-fluid">
                            <label class="control-label span3" id="labelslideshowbrands" for="search-input">{{ form_label(form.slideshowbrands, "Ajouter une/des photos pour le diaporama") }}</label>
                            <div class="controls span9">
                              <div class="input-append row-fluid">
                                {{ form_widget(form.slideshowbrands) }}
                              </div>
                            </div>
                       </div>
                        <div class="form-actions row-fluid">
                            <div class="span3 visible-desktop"></div>
                            <div class="span7 ">
                                <button type="submit" class="btn btn-primary">Enregister</button>
                            </div>
                        </div>
                        </form>
                    </div>
                </div>
                <!-- End .box --> 
            </div>
        </div>
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
     
                var $container = $('div#brandsAdd_slideshowbrands');
     
                var $lienAjout = $('<a href="#" id="ajout_marque" class="btn btn-success">Ajouter</a>');
     
                $container.append($lienAjout);
     
                $lienAjout.click(function(e) {
                    ajouterCategorie($container);
                    e.preventDefault(); 
                    return false;
                });
     
     
                var index = $container.find(':input').length;
     
                if (index == 0) {
     
                    ajouterCategorie($container);
                } else {
     
                    $container.children('div').each(function() {
                        ajouterLienSuppression($(this));
                    });
     
                }
     
                function ajouterCategorie($container) {
                    // Dans le contenu de l'attribut « data-prototype », on remplace :
                    // - le texte "__name__label__" qu'il contient par le label du champ
                    // - le texte "__name__" qu'il contient par le numéro du champ
                    var $prototype = $($container.attr('data-prototype').replace(/__name__label__/g, '')
                                                            .replace(/__name__/g, index));
     
     
     
                    ajouterLienSuppression($prototype);
     
                    $container.append($prototype);
     
                    $("#brandsAdd_slideshowbrands_"+index+" label").html('Image'); 
     
                    index++;
                }
     
                function ajouterLienSuppression($prototype) {
     
                    $lienSuppression = $('<a href="#" class="btn btn-danger">Supprimer</a>');
     
                    $prototype.append($lienSuppression);
     
                    $lienSuppression.click(function(e) {
                        $prototype.remove();
                        e.preventDefault(); // évite qu'un # apparaisse dans l'URL
                        return false;
                    });
                }
            });
            </script>
        {# Inclusion template Footer #}
        {% include "IlanBackBundle:Default:footer.html.twig" %}
     
    {% endblock %}
    Je ne vois pas le problème si quelqu'un peut m'aider.
    Merci

  2. #2
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    Bonjour,

    je ne rentre pas dans mon isValid
    probablement le jeton de sécurité,
    Pour savoir ce qui se passe, le mieux serait d'afficher les erreurs du formulaire dans ton twig avec des {{form_errors(form.leChamp)}}.
    il faut également ajouter {{form_errors(form)}} pour voir les erreurs globales du formulaire ainsi que {{form_rest(form)}}
    cf: http://symfony.com/fr/doc/current/bo...ns-un-template

    D'autre part dans ton classe tu as:
    /**
    * @var string
    *
    * @ORM\Column(name="brands_logo", type="string", length=255, nullable=true)
    * @Assert\File(maxSize="6000000")
    */
    private $brandsLogo;
    ce n'est pas cohérent ton attribut est de type string ou de type File mais pas les deux, consulte bien la doc, il te faut deux champs dans ton entité.

    http://symfony.com/fr/doc/current/co...e_uploads.html

  3. #3
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 85
    Points : 77
    Points
    77
    Par défaut
    Effectivement c'est le token => "The CSRF token is invalid. Please try to resubmit the form."
    Je n'y avais pas pensé merci !!! Et donc j'ai mise {{ form_row(form._token) }} et il passe le isValid


    Pour l'entité il est bien de type file. Je viens d'enlever l'annontations type string... Ensuite j'ai du rajouter un => mappedBy="brands", cascade={"persist", "remove"} dans brands.php (entity).
    Et la, mon formulaire ce soumet et la j'ai pensé que tout était fini mais non !!!!

    Alors dans ma table brands j'ai bien id de rentré, mais dans brandsLogo j'ai /tmp/php6cFGIz qui est enregistrer. Je pense savoir pourquoi mais ne sais pas comment résoudre ceci. Comme dans mon entity Brands.php j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public function setBrandsLogo(UploadedFile $brandsLogo)
        {
     
            $this->brandsLogo = $brandsLogo;
     
            return $this;
        }
    Je pense que c'est la mon problème mais je ne suis pas sur.
    Deuxième chose. Dans ma table slideshow_brands bah j'ai l'id qui ce rempli mais pareil que brands dans picture il me rajoute /tmp/php6cFGIz et dans la colonne brands_id => Null. Par contre la je suis en cours d'investigation mais je ne vois pas du tout pourquoi le brands_id est null !

    Merci

  4. #4
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 85
    Points : 77
    Points
    77
    Par défaut
    Bon j'avance !!!

    j'ai réussi à enregistrer les name comme ils faut dans mes tables. j'ai revue mes deux entity.
    Brands
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    <?php
     
    namespace Ilan\BackBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
     
    /**
     * Brands
     *
     * @ORM\Table(name="brands")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class Brands
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="brands_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $brandsId;
     
        /**
         * @var string
         *
         * @ORM\Column(name="brands_title", type="string", length=255, nullable=true)
         */
        private $brandsTitle;
     
        /**
         * @var string
         * 
         * @ORM\Column(name="brands_logo", type="string", length=255, nullable=true)
         */
        private $brandsLogo;
     
     
        /**
         * @Assert\File(maxSize="6000000")
         */
        public $file;
     
     
        /**
         * @var string
         *
         * @ORM\Column(name="brands_slug", type="string", length=255, nullable=true)
         */
        private $brandsSlug;
     
     
        /**
         * @var \SlideshowBrands
         * @ORM\OneToMany(targetEntity="SlideshowBrands", mappedBy="brands", cascade={"persist" , "remove"})
         */
        private $slideshowBrands;
     
     
        /**
         * 
         */
        public function __construct()
        {
            $this->slideshowBrands = new ArrayCollection();
        }
     
        /**
         * Get brandsId
         *
         * @return integer 
         */
        public function getBrandsId()
        {
            return $this->brandsId;
        }
     
        /**
         * Set brandsTitle
         *
         * @param string $brandsTitle
         * @return Brands
         */
        public function setBrandsTitle($brandsTitle)
        {
            $this->brandsTitle = $brandsTitle;
     
            return $this;
        }
     
        /**
         * Get brandsTitle
         *
         * @return string 
         */
        public function getBrandsTitle()
        {
            return $this->brandsTitle;
        }
     
        /**
         * 
         * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
         * @return \Ilan\BackBundle\Entity\Brands
         */
        public function setFile(UploadedFile $file)
        {
            $this->file = $file;
     
            return $this;
        }
     
        /**
         * Set brandsLogo
         *
         * @param string $brandsLogo
         * @return Brands
         */
        public function setBrandsLogo($brandsLogo)
        {
     
            $this->brandsLogo = $brandsLogo;
     
            return $this;
        }
     
     
        /**
         * Get brandsLogo
         *
         * @return string 
         */
        public function getBrandsLogo()
        {
            return $this->brandsLogo;
        }
     
        /**
         * Set brandsSlug
         *
         * @param string $brandsSlug
         * @return Brands
         */
        public function setBrandsSlug($brandsSlug)
        {
            $this->brandsSlug = $brandsSlug;
     
            return $this;
        }
     
        /**
         * Get brandsSlug
         *
         * @return string 
         */
        public function getBrandsSlug()
        {
            return $this->brandsSlug;
        }
     
        /**
         * Set slideshowbrands
         *
         * @param \Ilan\BackBundle\Entity\SlideshowBrands $slideshowbrands
         * @return 
         */
        public function setSlideshowBrands($slideshowbrands )
        {
            $this->slideshowBrands = $slideshowbrands;
     
            return $this;
        }
     
        /**
         * Get slideshowbrands
         *
         * @return \Ilan\BackBundle\Entity\SlideshowBrands 
         */
        public function getSlideshowBrands()
        {
            return $this->slideshowBrands;
        }
     
     
        public function getUploadDir()
        {
     
          return 'uploads/img/';
     
        }
     
        protected function getUploadRootDir()
        {
          return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }
     
        /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
        public function preUpload()
        {
     
          if (null === $this->file) {
            return;
          }
     
          $this->setBrandsLogo(str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->file->getClientOriginalName() ));
          $this->setBrandsSlug(str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->brandsTitle ));
     
        }
     
        /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
        public function upload()
        {
     
          if (null === $this->file) {
            return;
          }
     
          $brandsTitle = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->brandsTitle );
     
          if(!is_dir($this->getUploadRootDir().$brandsTitle) )
          {
              mkdir($this->getUploadRootDir().$brandsTitle);
              $pathFiles = $this->getUploadRootDir().$brandsTitle;
          }
     
          $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->file->getClientOriginalName() );
     
     
          $this->file->move($pathFiles, $name );
     
     
        }
     
     
     
    }
    Et slideshowbrands:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    <?php
     
    namespace Ilan\BackBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
     
    /**
     * SlideshowBrands
     *
     * @ORM\Table(name="slideshow_brands")
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks
     */
    class SlideshowBrands
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="slideshow_brands_id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $slideshowBrandsId;
     
        /**
         * @var string
         *
         * @ORM\Column(name="slideshow_brands_picture", type="string", length=255, nullable=true)
         */
        private $slideshowBrandsPicture;
     
     
        /**
         * @Assert\File(maxSize="6000000")
         */
        public $files;
     
        /**
         * @var \Brands
         *
         * @ORM\ManyToOne(targetEntity="Brands")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="brands_id", referencedColumnName="brands_id")
         * })
         */
        private $brands;
     
     
     
        /**
         * Get slideshowBrandsId
         *
         * @return integer 
         */
        public function getSlideshowBrandsId()
        {
            return $this->slideshowBrandsId;
        }
     
        /**
         * 
         * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
         * @return \Ilan\BackBundle\Entity\SlideshowBrands
         */
        public function setFiles(UploadedFile $files)
        {
            $this->files = $files;
     
            return $this;
        }
     
        /**
         * Set slideshowBrandsPicture
         *
         * @param string $slideshowBrandsPicture
         * @return SlideshowBrands
         */
        public function setSlideshowBrandsPicture($slideshowBrandsPicture)
        {
            $this->slideshowBrandsPicture = $slideshowBrandsPicture;
     
            return $this;
        }
     
        /**
         * Get slideshowBrandsPicture
         *
         * @return string 
         */
        public function getSlideshowBrandsPicture()
        {
            return $this->slideshowBrandsPicture;
        }
     
        /**
         * Set brands
         *
         * @param \Ilan\BackBundle\Entity\Brands $brands
         * @return SlideshowBrands
         */
        public function setBrands(\Ilan\BackBundle\Entity\Brands $brands = null)
        {
            $this->brands = $brands;
     
            return $this;
        }
     
        /**
         * Get brands
         *
         * @return \Ilan\BackBundle\Entity\Brands 
         */
        public function getBrands()
        {
            return $this->brands;
        }
     
     
        public function getUploadDir()
        {
          return 'uploads/img';
        }
     
        protected function getUploadRootDir()
        {
          return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }
     
     
     
        /**
       * @ORM\PrePersist()
       * @ORM\PreUpdate()
       */
        public function preUpload()
        {
     
          if (null === $this->files) {
            return;
          }
     
          $this->setSlideshowBrandsPicture(str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->files->getClientOriginalName() ));
        }
     
        /**
       * @ORM\PostPersist()
       * @ORM\PostUpdate()
       */
        public function upload()
        { 
     
          if (null === $this->files) {
            return;
          }
     
     
          $name = str_replace(array("","é","è","à","ç"," "),array("-","e","e","a","c","-"),$this->files->getClientOriginalName() );
     
          $this->files->move($this->getUploadRootDir(), $name );
     
        }
     
    }
    Bon avec de pas beau str_replace. j'ai créer un service slug mais je ferai ceci plus tard.
    Bon maintenant mon problème c'est que dans ma table slideshow_brans brands_id est toujours egale a null !!!

  5. #5
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2011
    Messages
    85
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 85
    Points : 77
    Points
    77
    Par défaut
    Merci la doc !!!

    Voici la réponse !

    J'ai juste fait ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    /**
         * Set slideshowbrands
         *
         * @param \Ilan\BackBundle\Entity\SlideshowBrands $slideshowbrands
         * @return 
         */
        public function setSlideshowBrands($slideshowbrands)
        {
     
     
            foreach ($slideshowbrands as $slideshowbrand) {
                $slideshowbrand->setBrands($this);
            }
     
            return $this->slideshowBrands = $slideshowbrands;
        }
    Et voila !!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 30/05/2012, 21h05
  2. [FPDF] Formulaire FDF avec des champs en gras
    Par PrinceMaster77 dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 19/12/2007, 09h33
  3. [FORMULAIRE]Problème avec des Formulaires
    Par choups76 dans le forum IHM
    Réponses: 17
    Dernier message: 12/04/2007, 12h27
  4. [CR9] Mise en forme de champ avec des formats
    Par Sytchev3 dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 01/03/2007, 16h35
  5. [Débutant]Boucle imbriquée avec des bornes différentes
    Par Hayato dans le forum Algorithmes et structures de données
    Réponses: 2
    Dernier message: 29/08/2005, 16h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo