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 :

QueryBuilder et Join


Sujet :

Symfony PHP

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Novembre 2011
    Messages : 1
    Points : 1
    Points
    1
    Par défaut QueryBuilder et Join
    Bonjour, j'ai suivit un cours sur Symfony 2 et j'ai lut la documentation sur le querybuilder et les joins. Pourtant j'ai toujours ce problème :

    Pour commencer voici mes deux entités :

    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
     
    <?php
     
    namespace Rendfort\ExerciceBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Rendfort\ExerciceBundle\Entity\Etape
     *
     * @ORM\Table(name="etape_rendfort")
     * @ORM\Entity(repositoryClass="Rendfort\ExerciceBundle\Entity\EtapeRepository")
     */
    class Etape
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="etap_id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string $nom
         *
         * @ORM\Column(name="nom", type="string", length=150)
         */
        private $nom;
     
    	 /**
         * @var integer $ordre
         *
         * @ORM\Column(name="etap_ordre", type="integer")
         */
        private $ordre;
    	/**
         * @var string $label
         *
         * @ORM\Column(name="etap_label", type="string", length=100)
         */
        private $label;
    	/**
         * @var string $labelColor
         *
         * @ORM\Column(name="etap_label_color", type="string", length=100)
         */
        private $labelColor;
    	/**
         * @var string $labelType
         *
         * @ORM\Column(name="etap_label_type", type="string", length=100)
         */
        private $labelType;	
    	/**
         * @var integer $cons_id
    	*@ORM\OneToOne(targetEntity="Rendfort\HomeBundle\Entity\HibouConsigne")
         * @ORM\JoinColumn(name="etap_cons_id", referencedColumnName="hibo_cons_id")
         */
        private $cons_id;
    	/**
         * @var integer $exercices
         *@ORM\ManyToOne(targetEntity="Rendfort\ExerciceBundle\Entity\Exercices", inversedBy="exercices", cascade={"persist"})
         * @ORM\JoinColumn(name="etape_exer_id", referencedColumnName="exer_id")
         */
        protected $exer_id;
     
    	public function __construct() {
     
     
    	}
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set nom
         *
         * @param string $nom
         */
        public function setNom($nom)
        {
            $this->nom = $nom;
        }
     
        /**
         * Get nom
         *
         * @return string 
         */
        public function getNom()
        {
            return $this->nom;
        }
     
        /**
         * Set ordre
         *
         * @param integer $ordre
         */
        public function setOrdre($ordre)
        {
            $this->ordre = $ordre;
        }
     
        /**
         * Get ordre
         *
         * @return integer 
         */
        public function getOrdre()
        {
            return $this->ordre;
        }
     
        /**
         * Set cons_id
         *
         * @param Rendfort\HomeBundle\Entity\HibouConsigne $consId
         */
        public function setConsId(\Rendfort\HomeBundle\Entity\HibouConsigne $consId)
        {
            $this->cons_id = $consId;
        }
     
        /**
         * Get cons_id
         *
         * @return Rendfort\HomeBundle\Entity\HibouConsigne 
         */
        public function getConsId()
        {
            return $this->cons_id;
        }
     
        /**
         * Set label
         *
         * @param string $label
         */
        public function setLabel($label)
        {
            $this->label = $label;
        }
     
        /**
         * Get label
         *
         * @return string 
         */
        public function getLabel()
        {
            return $this->label;
        }
     
        /**
         * Set labelType
         *
         * @param string $labelType
         */
        public function setLabelType($labelType)
        {
            $this->labelType = $labelType;
        }
     
        /**
         * Get labelType
         *
         * @return string 
         */
        public function getLabelType()
        {
            return $this->labelType;
        }
     
        /**
         * Set exer_id
         *
         * @param Rendfort\ExerciceBundle\Entity\Exercices $exerId
         */
        public function setExerId(\Rendfort\ExerciceBundle\Entity\Exercices $exerId)
        {
            $this->exer_id = $exerId;
        }
     
        /**
         * Get exer_id
         *
         * @return Rendfort\ExerciceBundle\Entity\Exercices 
         */
        public function getExerId()
        {
            return $this->exer_id;
        }
     
        /**
         * Set labelColor
         *
         * @param string $labelColor
         */
        public function setLabelColor($labelColor)
        {
            $this->labelColor = $labelColor;
        }
     
        /**
         * Get labelColor
         *
         * @return string 
         */
        public function getLabelColor()
        {
            return $this->labelColor;
        }
    }
    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
     
    <?php
     
    namespace Rendfort\HomeBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Rendfort\HomeBundle\Entity\HibouConsigne
     *
     * @ORM\Table(name="hibou_consigne_rendfort")
     * @ORM\Entity(repositoryClass="Rendfort\HomeBundle\Entity\HibouConsigneRepository")
     */
    class HibouConsigne
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="hibo_cons_id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var text $consigne
         *
         * @ORM\Column(name="hibo_cons_consigne", type="text")
         */
        private $consigne;
     
    	/**
         * @var string $son
         *
         * @ORM\Column(name="hibo_cons_son", type="string", length=255)
         */
        private $son;
    	 /**
         * @var string $appartenance
         *
         * @ORM\Column(name="hibo_cons_appartenance", type="string", length=255)
         */
        private $appartenance;
    	/**
         * @var integer $categorie
    	*@ORM\ManyToOne(targetEntity="Rendfort\HomeBundle\Entity\HibouCategorie", inversedBy="consignes", cascade={"persist"})
         * @ORM\JoinColumn(name="hibo_cons_cate_id", referencedColumnName="hibo_cate_id")
         */
        private $categorie;
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set consigne
         *
         * @param text $consigne
         */
        public function setConsigne($consigne)
        {
            $this->consigne = $consigne;
        }
     
        /**
         * Get consigne
         *
         * @return text 
         */
        public function getConsigne()
        {
            return $this->consigne;
        }
     
        /**
         * Set son
         *
         * @param string $son
         */
        public function setSon($son)
        {
            $this->son = $son;
        }
     
        /**
         * Get son
         *
         * @return string 
         */
        public function getSon()
        {
            return $this->son;
        }
     
     
        /**
         * Set categorie
         *
         * @param Rendfort\HomeBundle\Entity\HibouCategorie $categorie
         */
        public function setCategorie(\Rendfort\HomeBundle\Entity\HibouCategorie $categorie)
        {
            $this->categorie = $categorie;
        }
     
        /**
         * Get categorie
         *
         * @return Rendfort\HomeBundle\Entity\HibouCategorie 
         */
        public function getCategorie()
        {
            return $this->categorie;
        }
     
        /**
         * Set appartenance
         *
         * @param string $appartenance
         */
        public function setAppartenance($appartenance)
        {
            $this->appartenance = $appartenance;
        }
     
        /**
         * Get appartenance
         *
         * @return string 
         */
        public function getAppartenance()
        {
            return $this->appartenance;
        }
    }
    Je veux récupérer la consigne selon le $cons_id dans l'entité étape. Il ne peut qu'avoir qu'une consigne pour une étape. Alors j'ai faite une oneToOne unidirectionnel.

    Par la suite, dans mon EtapeRepository, j'ai faite cette fonction :

    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
    public function constructEtape($inExerId, $ordre) {
     
    	$qb = $this->createQueryBuilder('e')
    				->join('e.cons_id', 'c', 'c.id = e.cons_id')
    				->addSelect('c')
    			   ->where('e.exer_id = :id')
    			   ->andWhere('e.ordre = :ordre');
     
    		$qb->setParameter('id', $inExerId);
    		$qb->setParameter('ordre', $ordre);
     
    		return $qb->getQuery()
    				   ->getResult();
     
    	}
    Pourtant quand j'essaye de récupérer ma consigne avec $etape->getConsId(), je ne reçois rien. Merci de m'éclairer pour mon problème

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    Y'a pas de JOIN à faire entre Entités c'est le but de l'ORM ...
    Ton mappage n'est pas bon

    A la fin t'as juste a faire:


Discussions similaires

  1. Mysql Inner join
    Par ..:: Atchoum ::.. dans le forum Requêtes
    Réponses: 3
    Dernier message: 25/10/2007, 12h21
  2. Pas de JOIN sous Oracle (vraiment dommage...)
    Par Isildur dans le forum Langage SQL
    Réponses: 7
    Dernier message: 15/03/2007, 11h28
  3. Non coincident MySQL (Left Join)
    Par Remiguel dans le forum Requêtes
    Réponses: 6
    Dernier message: 03/11/2003, 21h25
  4. Réponses: 5
    Dernier message: 04/08/2003, 21h50
  5. Export d'une vue avec LEFT JOIN
    Par schnourf dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 22/05/2003, 13h57

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