2015-06-28 56 views
0

我試圖讓我的項目在我的數據庫中使用教義的名稱。下面是我使用的代碼:如何獲得我的項目與教條的hte名稱

$repository = $this->getDoctrine()->getRepository('PublicBundle:Projet'); 

$query = $repository->createQueryBuilder('p') 
        ->innerJoin('p.descriptions', 'd') 
        ->getQuery(); 

$products = $query->getResult(); 

foreach ($products as &$produit) { 
    print($produit->getDescriptions()->getNom()); 
} 

我的項目是一個實體,所有的文字是在其他:

項目:

<?php 

namespace PublicBundle\Entity; 

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

/** 
* Projet 
* 
* @ORM\Table(name="pt_projet"); 
* @ORM\Entity 
* @ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot") 
*/ 
class Projet 
{ 

    /** 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    //ID du projet 
    protected $id; 

    /** 
    * @ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projet", orphanRemoval=true) 
    */ 
    protected $descriptions; 

    /** 
    * @ORM\Column(name="pro_img", type="string", length=64, unique=true) 
    */ 
    //Nom du fichier de l'image du projet 
    protected $image; 

    /** 
    * @ORM\Column(name="pro_technologie_utilisee", type="text", length=200) 
    */ 
    //Text qui liste tout les technologies utilisées pour le projet 
    protected $technologie; 

    /** 
    * @ORM\Column(name="pro_annee", type="integer", length=4) 
    */ 
    //Année de réalisation du projet 
    protected $annee; 

    /** 
    * @ORM\ManyToOne(targetEntity="Type", inversedBy="projets") 
    * @ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false) 
    */ 
    //Clef étrangère du type de projet 
    //Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio. 
    protected $type; 

    /** 
    * @ORM\Column(name="pro_fichier", type="string", length=64, unique=true) 
    */ 
    //Nom du fichier du projet 
    private $fichier; 

    /** 
    * @ORM\Column(name="pro_largeur", type="integer") 
    */ 
    //Largeur du projet 
    protected $largeur; 

    /** 
    * @ORM\Column(name="pro_hauteur", type="integer") 
    */ 
    //Hauteur du projet 
    protected $hauteur; 

    /** 
    * @ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"}) 
    */ 
    //La ou les catégories du projet 
    private $categories; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->descriptions=new ArrayCollection(); 
     $this->categories=new ArrayCollection(); 
    } 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set image 
    * 
    * @param string $image 
    * @return Projet 
    */ 
    public function setImage($image) 
    { 
     $this->image = $image; 

     return $this; 
    } 

    /** 
    * Get image 
    * 
    * @return string 
    */ 
    public function getImage() 
    { 
     return $this->image; 
    } 

    /** 
    * Set technologie 
    * 
    * @param string $technologie 
    * @return Projet 
    */ 
    public function setTechnologie($technologie) 
    { 
     $this->technologie = $technologie; 

     return $this; 
    } 

    /** 
    * Get technologie 
    * 
    * @return string 
    */ 
    public function getTechnologie() 
    { 
     return $this->technologie; 
    } 

    /** 
    * Set annee 
    * 
    * @param integer $annee 
    * @return Projet 
    */ 
    public function setAnnee($annee) 
    { 
     $this->annee = $annee; 

     return $this; 
    } 

    /** 
    * Get annee 
    * 
    * @return integer 
    */ 
    public function getAnnee() 
    { 
     return $this->annee; 
    } 

    /** 
    * Set fichier 
    * 
    * @param string $fichier 
    * @return Projet 
    */ 
    public function setFichier($fichier) 
    { 
     $this->fichier = $fichier; 

     return $this; 
    } 

    /** 
    * Get fichier 
    * 
    * @return string 
    */ 
    public function getFichier() 
    { 
     return $this->fichier; 
    } 

    /** 
    * Set largeur 
    * 
    * @param integer $largeur 
    * @return Projet 
    */ 
    public function setLargeur($largeur) 
    { 
     $this->largeur = $largeur; 

     return $this; 
    } 

    /** 
    * Get largeur 
    * 
    * @return integer 
    */ 
    public function getLargeur() 
    { 
     return $this->largeur; 
    } 

    /** 
    * Set hauteur 
    * 
    * @param integer $hauteur 
    * @return Projet 
    */ 
    public function setHauteur($hauteur) 
    { 
     $this->hauteur = $hauteur; 

     return $this; 
    } 

    /** 
    * Get hauteur 
    * 
    * @return integer 
    */ 
    public function getHauteur() 
    { 
     return $this->hauteur; 
    } 

    /** 
    * Add descriptions 
    * 
    * @param \PublicBundle\Entity\ProjetInt $descriptions 
    * @return Projet 
    */ 
    public function addDescription(\PublicBundle\Entity\ProjetInt $descriptions) 
    { 
     $this->descriptions[] = $descriptions; 

     return $this; 
    } 

    /** 
    * Remove descriptions 
    * 
    * @param \PublicBundle\Entity\ProjetInt $descriptions 
    */ 
    public function removeDescription(\PublicBundle\Entity\ProjetInt $descriptions) 
    { 
     $this->descriptions->removeElement($descriptions); 
    } 

    /** 
    * Get descriptions 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getDescriptions() 
    { 
     return $this->descriptions; 
    } 

    /** 
    * Set type 
    * 
    * @param \PublicBundle\Entity\Type $type 
    * @return Projet 
    */ 
    public function setType(\PublicBundle\Entity\Type $type) 
    { 
     $this->type = $type; 

     return $this; 
    } 

    /** 
    * Get type 
    * 
    * @return \PublicBundle\Entity\Type 
    */ 
    public function getType() 
    { 
     return $this->type; 
    } 

    /** 
    * Add categories 
    * 
    * @param \PublicBundle\Entity\Categorie $categories 
    * @return Projet 
    */ 
    public function addCategory(\PublicBundle\Entity\Categorie $categories) 
    { 
     $this->categories[] = $categories; 

     return $this; 
    } 

    /** 
    * Remove categories 
    * 
    * @param \PublicBundle\Entity\Categorie $categories 
    */ 
    public function removeCategory(\PublicBundle\Entity\Categorie $categories) 
    { 
     $this->categories->removeElement($categories); 
    } 

    /** 
    * Get categories 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getCategories() 
    { 
     return $this->categories; 
    } 
} 

這裏是實體我的項目的所有文本:

<?php 

namespace PublicBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Projet Inter 
* 
* @ORM\Table(name="pt_projet_int"); 
* @ORM\Entity 
* @ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetIntDepot") 
*/ 
class ProjetInt 
{ 

    /** 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    //ID du projet 
    protected $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Projet", inversedBy="descriptions") 
    * @ORM\JoinColumn(name="pro_id_CE", referencedColumnName="id", nullable=false) 
    */ 
    //Clef étrangère du projet associé 
    protected $projet; 

    /** 
    * @ORM\Column(name="pri_lang", type="string", length=2) 
    */ 
    //Langue de la carégorie 
    protected $langue; 

    /** 
    * @ORM\Column(name="pri_nom", type="string", length=30) 
    */ 
    //Nom du produit 
    protected $nom; 

    /** 
    * @ORM\Column(name="pri_description_cours", type="text",length=100) 
    */ 
    //Description cours du projet 
    protected $descriptionCours; 

    /** 
    * @ORM\Column(name="pri_description_complete", type="text",length=250) 
    */ 
    //Description complete du projet 
    protected $descriptionComplete; 

    /** 
    * @ORM\Column(name="pri_roles", type="string",length=60) 
    */ 
    //Roles joués dans la création du projet 
    protected $roles; 

    /** 
    * @ORM\Column(name="pri_aptitudes_developpees", type="string",length=200) 
    */ 
    //Aptitudes développées lors du projet 
    protected $aptitudesDeveloppees; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set langue 
    * 
    * @param string $langue 
    * @return ProjetInt 
    */ 
    public function setLangue($langue) 
    { 
     $this->langue = $langue; 

     return $this; 
    } 

    /** 
    * Get langue 
    * 
    * @return string 
    */ 
    public function getLangue() 
    { 
     return $this->langue; 
    } 

    /** 
    * Set nom 
    * 
    * @param string $nom 
    * @return ProjetInt 
    */ 
    public function setNom($nom) 
    { 
     $this->nom = $nom; 

     return $this; 
    } 

    /** 
    * Get nom 
    * 
    * @return string 
    */ 
    public function getNom() 
    { 
     return $this->nom; 
    } 

    /** 
    * Set descriptionCours 
    * 
    * @param string $descriptionCours 
    * @return ProjetInt 
    */ 
    public function setDescriptionCours($descriptionCours) 
    { 
     $this->descriptionCours = $descriptionCours; 

     return $this; 
    } 

    /** 
    * Get descriptionCours 
    * 
    * @return string 
    */ 
    public function getDescriptionCours() 
    { 
     return $this->descriptionCours; 
    } 

    /** 
    * Set descriptionComplete 
    * 
    * @param string $descriptionComplete 
    * @return ProjetInt 
    */ 
    public function setDescriptionComplete($descriptionComplete) 
    { 
     $this->descriptionComplete = $descriptionComplete; 

     return $this; 
    } 

    /** 
    * Get descriptionComplete 
    * 
    * @return string 
    */ 
    public function getDescriptionComplete() 
    { 
     return $this->descriptionComplete; 
    } 

    /** 
    * Set roles 
    * 
    * @param string $roles 
    * @return ProjetInt 
    */ 
    public function setRoles($roles) 
    { 
     $this->roles = $roles; 

     return $this; 
    } 

    /** 
    * Get roles 
    * 
    * @return string 
    */ 
    public function getRoles() 
    { 
     return $this->roles; 
    } 

    /** 
    * Set aptitudesDeveloppees 
    * 
    * @param string $aptitudesDeveloppees 
    * @return ProjetInt 
    */ 
    public function setAptitudesDeveloppees($aptitudesDeveloppees) 
    { 
     $this->aptitudesDeveloppees = $aptitudesDeveloppees; 

     return $this; 
    } 

    /** 
    * Get aptitudesDeveloppees 
    * 
    * @return string 
    */ 
    public function getAptitudesDeveloppees() 
    { 
     return $this->aptitudesDeveloppees; 
    } 

    /** 
    * Set projetId 
    * 
    * @param \PublicBundle\Entity\Projet $projetId 
    * @return ProjetInt 
    */ 
    public function setProjetId(\PublicBundle\Entity\Projet $projetId) 
    { 
     $this->projetId = $projetId; 

     return $this; 
    } 

    /** 
    * Get projetId 
    * 
    * @return \PublicBundle\Entity\Projet 
    */ 
    public function getProjetId() 
    { 
     return $this->projetId; 
    } 

    /** 
    * Set projet 
    * 
    * @param \PublicBundle\Entity\Projet $projet 
    * @return ProjetInt 
    */ 
    public function setProjet(\PublicBundle\Entity\Projet $projet) 
    { 
     $this->projet = $projet; 

     return $this; 
    } 

    /** 
    * Get projet 
    * 
    * @return \PublicBundle\Entity\Projet 
    */ 
    public function getProjet() 
    { 
     return $this->projet; 
    } 
} 

當我試圖讓名(「NOM」),它告訴我:

試圖在類「Doctrine \ ORM \ PersistentCollection」上調用方法「getNom」。

回答

0

我找到了答案,我忘記了把描述的索引。這裏是:

foreach ($products as &$produit) { 
    print($produit->getDescriptions()[0]->getNom()); 
} 
+1

或者,您可以使用'$ produit-> getDescriptions() - > first();'。我不確定如果結果中沒有'$ description'對象,那麼調用它會執行什麼操作,但是您在上面執行的數組取消引用將觸發未定義的偏移量通知,並立即通過「非對象」致命錯誤,所以你應該處理這個。 'Doctrine \ ORM \ PersistentCollection'有一個'count()'方法,可以幫助你緩解這個問題。 –