php - Explanation with doctrine -


i asking result of query. have not same result when use mysql , doctrine... have field change when it's executed (package ->id).

my query in mysql :

"select *  packages_dates_prices  inner join packages  on packages_dates_prices.id = packages.id  , packages_dates_prices.package ='.$id.'" 

my query in repository , in dql :

"select backbundle:packagesdatesprices inner join backbundle:packages b a.id = b.id , a.package = :id " 

the query in debug toolbar :

select    e0_.id id0,    e0_.date_start date_start1,    e0_.date_end date_end2,    e0_.price price3,    e0_.id id4     packages_dates_prices e0_    inner join packages e1_ on (     e0_.id = e1_.id      , e0_.id = ?   ) parameters: [1]  

entity packages :

<?php  namespace back\bundle\entity;  use doctrine\orm\mapping orm; use gedmo\mapping\annotation gedmo;  /**  * packages  *  * @orm\table(name="packages")  * @orm\entity(repositoryclass="back\bundle\entity\packagesrepository")  */ class packages {     /**      * @var integer      *      * @orm\column(name="id", type="integer", nullable=true)      * @orm\id      * @orm\generatedvalue(strategy="identity")      *      */     private $id;      /**      * @var string      *      * @orm\column(name="title", type="string", length=255, nullable=true)      */     private $title;      /**      * @var string      *      * @orm\column(name="description", type="string", length=255, nullable=true)      */     private $description;      /**      * @var string      *      * @orm\column(name="details", type="string", length=255, nullable=true)      */     private $details;      /**      * @var string      *      * @orm\column(name="essential", type="string", length=255, nullable=true)      */     private $essential;      /**      * @var string      *      * @orm\column(name="indices", type="string", length=255, nullable=true)      */     private $indices;      /**      * @var string      *      * @orm\column(name="mysterious", type="string", length=255, nullable=true)      */     private $mysterious;      /**      * @var string      *      * @orm\column(name="products", type="string", length=255, nullable=true)      */     private $products;      /**      * @var string      *      * @orm\column(name="attachment", type="string", nullable=true)      */     private $attachment;      /**      * @var string      *      * @orm\column(name="meta_keywords", type="string", nullable=true)      */     private $metakeywords;      /**      * @var string      *      * @orm\column(name="meta_description", type="string", nullable=true)      */     private $metadescription;      /**      * @var \datetime      *      * @gedmo\timestampable(on="create")      * @orm\column(name="created_at", type="datetime", nullable=true)      */     private $createdat;      /**      * @var string      *      * @orm\column(name="agent", type="string", length=255, nullable=true)      */     private $agent;      /**      * @var string      *      * @orm\column(name="place", type="string", length=255, nullable=true)      */     private $place;      /**      * @var string      *      * @orm\column(name="theme", type="string", length=255, nullable=true)      */     private $theme;      /**      * @var \datetime      *      * @gedmo\timestampable(on="update")      * @orm\column(name="updated_at", type="datetime", nullable=true)      */     private $updatedat;      /**      * @var \boolean      *      * @orm\column(name="is_active", type="boolean", nullable=true)      */     private $isactive;      /**      * @var \boolean      *      * @orm\column(name="is_home", type="boolean", nullable=true)      */     private $ishome;      /**      * @var string      *      * @orm\column(name="link", type="string", length=255, nullable=true)      */     private $link;      /**      * @var string      *      * @orm\column(name="order_home", type="integer", nullable=true)      */     private $orderhome;      /**      * @gedmo\slug(fields={"title"})      * @orm\column(length=128, unique=true)      */     private $slug;      /**      * @var \boolean      *      * @orm\column(name="is_outing", type="boolean", nullable=true)      */     private $isouting;      /**      * @var \boolean      *      * @orm\column(name="is_weekend", type="boolean", nullable=true)      */     private $isweekend;      /**      * @var \boolean      *      * @orm\column(name="is_wedding", type="boolean", nullable=true)      */     private $iswedding;      /**      * @var \boolean      *      * @orm\column(name="is_present", type="boolean", nullable=true)      */     private $ispresent;      /**      * @var \boolean      *      * @orm\column(name="is_company", type="boolean", nullable=true)      */     private $iscompany;      /**      * @var \boolean      *      * @orm\column(name="is_solo", type="boolean", nullable=true)      */     private $issolo;      /**      * @var \boolean      *      * @orm\column(name="is_couple", type="boolean", nullable=true)      */     private $iscouple;      /**      * @var \boolean      *      * @orm\column(name="is_friends", type="boolean", nullable=true)      */     private $isfriends;      /**      * @var \boolean      *      * @orm\column(name="is_family", type="boolean", nullable=true)      */     private $isfamily;      /**      * @var \doctrine\common\collections\collection      * @orm\onetomany(targetentity="back\bundle\entity\packagesdatesprices", mappedby="package")      **/     private $datesprices;       public function __construct()     {         $this->createdat = new \datetime();         $this->datesprices = new \doctrine\common\collections\arraycollection();     }      /**      * @return string      */     public function getpeople()     {         return sprintf("%s %s", $this->getissolo(), $this->getiscouple(), $this->getisfriends(), $this->getisfamily());     }      public function __tostring()     {         if ($this->getid())             return "packages n° ".$this->getid();         return "nouveau package";     }      /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set title      *      * @param string $title      * @return packages      */     public function settitle($title)     {         $this->title = $title;          return $this;     }      /**      * title      *      * @return string       */     public function gettitle()     {         return $this->title;     }      /**      * set description      *      * @param string $description      * @return packages      */     public function setdescription($description)     {         $this->description = $description;          return $this;     }      /**      * description      *      * @return string       */     public function getdescription()     {         return $this->description;     }      /**      * set details      *      * @param string $details      * @return packages      */     public function setdetails($details)     {         $this->details = $details;          return $this;     }      /**      * details      *      * @return string       */     public function getdetails()     {         return $this->details;     }      /**      * set essential      *      * @param string $essential      * @return packages      */     public function setessential($essential)     {         $this->essential = $essential;          return $this;     }      /**      * essential      *      * @return string       */     public function getessential()     {         return $this->essential;     }      /**      * set indices      *      * @param string $indices      * @return packages      */     public function setindices($indices)     {         $this->indices = $indices;          return $this;     }      /**      * indices      *      * @return string       */     public function getindices()     {         return $this->indices;     }      /**      * set mysterious      *      * @param string $mysterious      * @return packages      */     public function setmysterious($mysterious)     {         $this->mysterious = $mysterious;          return $this;     }      /**      * mysterious      *      * @return string       */     public function getmysterious()     {         return $this->mysterious;     }      /**      * set products      *      * @param string $products      * @return packages      */     public function setproducts($products)     {         $this->products = $products;          return $this;     }      /**      * products      *      * @return string       */     public function getproducts()     {         return $this->products;     }      /**      * set attachment      *      * @param string $attachment      * @return packages      */     public function setattachment($attachment)     {         $this->attachment = $attachment;          return $this;     }      /**      * attachment      *      * @return string       */     public function getattachment()     {         return $this->attachment;     }      /**      * set metakeywords      *      * @param string $metakeywords      * @return packages      */     public function setmetakeywords($metakeywords)     {         $this->metakeywords = $metakeywords;          return $this;     }      /**      * metakeywords      *      * @return string       */     public function getmetakeywords()     {         return $this->metakeywords;     }      /**      * set metadescription      *      * @param string $metadescription      * @return packages      */     public function setmetadescription($metadescription)     {         $this->metadescription = $metadescription;          return $this;     }      /**      * metadescription      *      * @return string       */     public function getmetadescription()     {         return $this->metadescription;     }      /**      * set createdat      *      * @param \datetime $createdat      * @return packages      */     public function setcreatedat($createdat)     {         $this->createdat = $createdat;          return $this;     }      /**      * createdat      *      * @return \datetime       */     public function getcreatedat()     {         return $this->createdat;     }      /**      * set agent      *      * @param string $agent      * @return packages      */     public function setagent($agent)     {         $this->agent = $agent;          return $this;     }      /**      * agent      *      * @return string       */     public function getagent()     {         return $this->agent;     }      /**      * set place      *      * @param string $place      * @return packages      */     public function setplace($place)     {         $this->place = $place;          return $this;     }      /**      * place      *      * @return string       */     public function getplace()     {         return $this->place;     }      /**      * set theme      *      * @param string $theme      * @return packages      */     public function settheme($theme)     {         $this->theme = $theme;          return $this;     }      /**      * theme      *      * @return string       */     public function gettheme()     {         return $this->theme;     }      /**      * set updatedat      *      * @param \datetime $updatedat      * @return packages      */     public function setupdatedat($updatedat)     {         $this->updatedat = $updatedat;          return $this;     }      /**      * updatedat      *      * @return \datetime       */     public function getupdatedat()     {         return $this->updatedat;     }      /**      * set isactive      *      * @param boolean $isactive      * @return packages      */     public function setisactive($isactive)     {         $this->isactive = $isactive;          return $this;     }      /**      * isactive      *      * @return boolean       */     public function getisactive()     {         return $this->isactive;     }      /**      * set ishome      *      * @param boolean $ishome      * @return packages      */     public function setishome($ishome)     {         $this->ishome = $ishome;          return $this;     }      /**      * ishome      *      * @return boolean       */     public function getishome()     {         return $this->ishome;     }      /**      * set link      *      * @param string $link      * @return packages      */     public function setlink($link)     {         $this->link = $link;          return $this;     }      /**      * link      *      * @return string       */     public function getlink()     {         return $this->link;     }      /**      * set orderhome      *      * @param integer $orderhome      * @return packages      */     public function setorderhome($orderhome)     {         $this->orderhome = $orderhome;          return $this;     }      /**      * orderhome      *      * @return integer       */     public function getorderhome()     {         return $this->orderhome;     }      /**      * set slug      *      * @param string $slug      * @return packages      */     public function setslug($slug)     {         $this->slug = $slug;          return $this;     }      /**      * slug      *      * @return string       */     public function getslug()     {         return $this->slug;     }      /**      * set isouting      *      * @param boolean $isouting      * @return packages      */     public function setisouting($isouting)     {         $this->isouting = $isouting;          return $this;     }      /**      * isouting      *      * @return boolean       */     public function getisouting()     {         return $this->isouting;     }      /**      * set isweekend      *      * @param boolean $isweekend      * @return packages      */     public function setisweekend($isweekend)     {         $this->isweekend = $isweekend;          return $this;     }      /**      * isweekend      *      * @return boolean       */     public function getisweekend()     {         return $this->isweekend;     }      /**      * set iswedding      *      * @param boolean $iswedding      * @return packages      */     public function setiswedding($iswedding)     {         $this->iswedding = $iswedding;          return $this;     }      /**      * iswedding      *      * @return boolean       */     public function getiswedding()     {         return $this->iswedding;     }      /**      * set ispresent      *      * @param boolean $ispresent      * @return packages      */     public function setispresent($ispresent)     {         $this->ispresent = $ispresent;          return $this;     }      /**      * ispresent      *      * @return boolean       */     public function getispresent()     {         return $this->ispresent;     }      /**      * set iscompany      *      * @param boolean $iscompany      * @return packages      */     public function setiscompany($iscompany)     {         $this->iscompany = $iscompany;          return $this;     }      /**      * iscompany      *      * @return boolean       */     public function getiscompany()     {         return $this->iscompany;     }      /**      * set issolo      *      * @param boolean $issolo      * @return packages      */     public function setissolo($issolo)     {         $this->issolo = $issolo;          return $this;     }      /**      * issolo      *      * @return boolean       */     public function getissolo()     {         return $this->issolo;     }      /**      * set iscouple      *      * @param boolean $iscouple      * @return packages      */     public function setiscouple($iscouple)     {         $this->iscouple = $iscouple;          return $this;     }      /**      * iscouple      *      * @return boolean       */     public function getiscouple()     {         return $this->iscouple;     }      /**      * set isfriends      *      * @param boolean $isfriends      * @return packages      */     public function setisfriends($isfriends)     {         $this->isfriends = $isfriends;          return $this;     }      /**      * isfriends      *      * @return boolean       */     public function getisfriends()     {         return $this->isfriends;     }      /**      * set isfamily      *      * @param boolean $isfamily      * @return packages      */     public function setisfamily($isfamily)     {         $this->isfamily = $isfamily;          return $this;     }      /**      * isfamily      *      * @return boolean       */     public function getisfamily()     {         return $this->isfamily;     }      /**      * add datesprices      *      * @param back\bundle\entity\packagesdatesprices $datesprices      * @return packages      */     public function adddatesprice(\back\bundle\entity\packagesdatesprices $datesprices)     {         $this->datesprices[] = $datesprices;          $datesprices->setpackage($this);          return $this;     }      /**      * remove datesprices      *      * @param \back\bundle\entity\packagesdatesprices $datesprices      */     public function removedatesprice(\back\bundle\entity\packagesdatesprices $datesprices)     {         $this->datesprices->removeelement($datesprices);     }      /**      * datesprices      *      * @return \doctrine\common\collections\collection       */     public function getdatesprices()     {         return $this->datesprices;     } } 

packagesdatesprices :

<?php  namespace back\bundle\entity;  use doctrine\orm\mapping orm; use gedmo\mapping\annotation gedmo;  /**  *  * @orm\table(name="packages_dates_prices")  * @orm\entity(repositoryclass="back\bundle\entity\packagesdatespricesrepository")  */ class packagesdatesprices {     /**      * @var integer      *      * @orm\column(name="id", type="integer", nullable=true)      * @orm\id      * @orm\generatedvalue(strategy="identity")      **/     private $id;      /**      * @var \back\bundle\entity\packages      * @orm\manytoone(targetentity="back\bundle\entity\packages", inversedby="datesprices")      * @orm\joincolumn(name="id", referencedcolumnname="id")      **/     private $package;      /**      * @var \back\bundle\entity\packages      * @var \datetime      *      * @orm\column(name="date_start", type="datetime", nullable=true)      */     private $datestart;      /**      * @var \datetime      *      * @orm\column(name="date_end", type="datetime", nullable=true)      */     private $dateend;      /**      * @var integer      *      * @orm\column(name="price", type="integer", nullable=true)      */     private $price;       public function __construct() {         $this->children = new \doctrine\common\collections\arraycollection();     }       /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set datestart      *      * @param \datetime $datestart      * @return packagesdatesprices      */     public function setdatestart($datestart)     {         $this->datestart = $datestart;          return $this;     }      /**      * datestart      *      * @return \datetime       */     public function getdatestart()     {         return $this->datestart;     }      /**      * set dateend      *      * @param \datetime $dateend      * @return packagesdatesprices      */     public function setdateend($dateend)     {         $this->dateend = $dateend;          return $this;     }      /**      * dateend      *      * @return \datetime       */     public function getdateend()     {         return $this->dateend;     }      /**      * set price      *      * @param integer $price      * @return packagesdatesprices      */     public function setprice($price)     {         $this->price = $price;          return $this;     }      /**      * price      *      * @return integer       */     public function getprice()     {         return $this->price;     }      /**      * set package      *      * @param \back\bundle\entity\packages $package      * @return packagesdatesprices      */     public function setpackage(\back\bundle\entity\packages $package = null)     {         $this->package = $package;          return $this;     }      /**      * package      *      * @return \back\bundle\entity\packages      */     public function getpackage()     {         return $this->package;     } } 

packagesdatespricesrepository.php :

<?php  namespace back\bundle\entity;  use doctrine\orm\entityrepository;  /**  *  * class generated doctrine orm. add own custom  * repository methods below.  */ class packagesdatespricesrepository extends entityrepository {     public function gettest($id)     {         $req=$this->getentitymanager()->createquery         (             "select             backbundle:packagesdatesprices             inner join backbundle:packages b             a.package = b.id             ,  b.id = :id             "         );          $req ->setparameter('id', $id);          return $req->getresult();     }  } 

my controller :

public function packagepageaction($id)     {         $test = $this->getdoctrine()             ->getmanager()             ->getrepository('backbundle:packagesdatesprices')             ->gettest($id)         ;          return $this->render('backbundle:global:packagepage.html.twig',array(             'test' => $test         ));     } 

i have relation onetomany between id of packages(name of field id) , packages_dates_prices(name of field package).

i have not found mistake yet. tried long time, not familiar doctrine.

thank read me , me.

you can read in symfony2 documention section fetching related objects.

the result expecting can obtained without writing sql statements.

$package = $this->getdoctrine()->getrepository("backbundle:packages")->find($id); $datesprices = $package->getdatesprices(); 

$datesprices contain rows joined via onetomany relationship.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -