php - How to use getQuery()->getOneOrNullresult() return -


in test project have 2 entities : - enduser (extend of fosuserbundle) - rezo (will containt approved relation between 2 members)

the both entities have been defined :

enduser entity :

<?php  namespace core\customerbundle\entity;  use doctrine\orm\mapping orm; use fos\userbundle\model\user baseuser; use symfony\component\validator\constraints assert;   /**  * enduser  *  * @orm\table()  * @orm\entity(repositoryclass="core\customerbundle\entity\enduserrepository")  */ class enduser extends baseuser {     /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     protected $id;      /**      * @var string      * @orm\column(name="firstname", type="string", length=255)      */     private $firstname;      /**      * @var string      * @orm\column(name="lastname", type="string", length=255)      */     private $lastname;      /**      * @var \datetime      *      * @orm\column(name="datenaissance", type="datetime", nullable=true)      */     private $datenaissance;      /**      * @orm\onetoone(targetentity="core\customerbundle\entity\enduser", cascade={"persist", "merge", "remove"})      * @orm\joincolumn(name="adressbook_id", referencedcolumnname="id", nullable=true)      */     private $adressbook;      /**      * @orm\manytomany(targetentity="core\myequibookbundle\entity\discipline", mappedby="endusers")      */     private $disciplines;      /**      * @orm\manytomany(targetentity="core\myequibookbundle\entity\experiences", mappedby="endusers")      */     private $experiences;      /**      * @var string      * @orm\column(name="avatar", type="string", length=255, nullable=true)      */     private $avatar;      /**      * @var string      * @orm\column(name="repository", type="string", length=255, nullable=true)      */     private $repository;      /**      * @orm\onetomany(targetentity="core\myequibookbundle\entity\niveauenduser", mappedby="enduser", cascade={"remove", "persist"})      */     protected $niveaux;      /**      * @orm\onetomany(targetentity="core\generalbundle\entity\rezo", mappedby="enduser", cascade={"remove", "persist"})      */     protected $friends;      /**      * constructor      */     public function __construct()     {         parent::__construct();         $this->disciplines = new \doctrine\common\collections\arraycollection();         $this->niveaux = new \doctrine\common\collections\arraycollection();         $this->experiences = new \doctrine\common\collections\arraycollection();         $this->friends = new \doctrine\common\collections\arraycollection();         $this->expiresat = new \datetime("+1 year");         $this->credentialsexpireat = new \datetime("+1 year");     }      /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set avatar      *      * @param string $avatar      * @return enduser      */     public function setavatar($avatar)     {         $this->avatar = $avatar;          return $this;     }      /**      * avatar      *      * @return string       */     public function getavatar()     {         return $this->avatar;     }      /**      * set repository      *      * @param string $repository      * @return enduser      */     public function setrepository($repository)     {         $this->repository = $repository;          return $this;     }      /**      * repository      *      * @return string       */     public function getrepository()     {         return $this->repository;     }      /**      * set adressbook      *      * @param \core\customerbundle\entity\enduser $adressbook      * @return enduser      */     public function setadressbook(\core\customerbundle\entity\enduser $adressbook = null)     {         $this->adressbook = $adressbook;          return $this;     }      /**      * adressbook      *      * @return \core\customerbundle\entity\enduser       */     public function getadressbook()     {         return $this->adressbook;     }      /**      * add disciplines      *      * @param \core\myequibookbundle\entity\discipline $disciplines      * @return enduser      */     public function adddiscipline(\core\myequibookbundle\entity\discipline $disciplines)     {         $this->disciplines[] = $disciplines;          return $this;     }      /**      * remove disciplines      *      * @param \core\myequibookbundle\entity\discipline $disciplines      */     public function removediscipline(\core\myequibookbundle\entity\discipline $disciplines)     {         $this->disciplines->removeelement($disciplines);     }      /**      * disciplines      *      * @return \doctrine\common\collections\collection       */     public function getdisciplines()     {         return $this->disciplines;     }      /**      * add niveaux      *      * @param \core\myequibookbundle\entity\niveauenduser $niveaux      * @return enduser      */     public function addniveaux(\core\myequibookbundle\entity\niveauenduser $niveaux)     {         $this->niveaux[] = $niveaux;          return $this;     }      /**      * remove niveaux      *      * @param \core\myequibookbundle\entity\niveauenduser $niveaux      */     public function removeniveaux(\core\myequibookbundle\entity\niveauenduser $niveaux)     {         $this->niveaux->removeelement($niveaux);     }      /**      * niveaux      *      * @return \doctrine\common\collections\collection       */     public function getniveaux()     {         return $this->niveaux;     }      /**      * add experiences      *      * @param \core\myequibookbundle\entity\experiences $experiences      * @return enduser      */     public function addexperience(\core\myequibookbundle\entity\experiences $experiences)     {         $this->experiences[] = $experiences;          return $this;     }      /**      * remove experiences      *      * @param \core\myequibookbundle\entity\experiences $experiences      */     public function removeexperience(\core\myequibookbundle\entity\experiences $experiences)     {         $this->experiences->removeelement($experiences);     }      /**      * experiences      *      * @return \doctrine\common\collections\collection       */     public function getexperiences()     {         return $this->experiences;     }      /**      * add friends      *      * @param \core\generalbundle\entity\rezo $friends      * @return enduser      */     public function addfriend(\core\generalbundle\entity\rezo $friends )     {         $this->friends[] = $friends;          return $this;     }      /**      * remove friends      *      * @param \core\generalbundle\entity\rezo $friends      */     public function removefriend(\core\generalbundle\entity\rezo $friends)     {         $this->friends->removeelement($friends);     }      /**      * friends      *      * @return \doctrine\common\collections\collection       */     public function getfriends()     {         return $this->friends;     }      /**      * set firstname      *      * @param string $firstname      * @return enduser      */     public function setfirstname($firstname)     {         $this->firstname = $firstname;          return $this;     }      /**      * firstname      *      * @return string       */     public function getfirstname()     {         return $this->firstname;     }      /**      * set lastname      *      * @param string $lastname      * @return enduser      */     public function setlastname($lastname)     {         $this->lastname = $lastname;          return $this;     }      /**      * lastname      *      * @return string       */     public function getlastname()     {         return $this->lastname;     }      /**      * set datenaissance      *      * @param \datetime $datenaissance      * @return enduser      */     public function setdatenaissance($datenaissance)     {         $this->datenaissance = $datenaissance;          return $this;     }      /**      * datenaissance      *      * @return \datetime       */     public function getdatenaissance()     {         return $this->datenaissance;     } } 

rezo entity :

<?php  namespace core\generalbundle\entity;  use doctrine\orm\mapping orm;  /**  * rezo  *  * @orm\table()  * @orm\entity(repositoryclass="core\generalbundle\entity\rezorepository")  */ class rezo {     /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var \datetime      *      * @orm\column(name="requesteddate", type="datetime")      */     private $requesteddate;      /**      * @var \datetime      *      * @orm\column(name="accepteddate", type="datetime", nullable=true)      */     private $accepteddate;       /**      * @orm\manytoone(targetentity="core\customerbundle\entity\enduser", inversedby="friends", cascade={"refresh"})      * @orm\joincolumn(name="user_id", referencedcolumnname="id")      */     protected $enduser;      /**      * @orm\manytoone(targetentity="core\customerbundle\entity\enduser", cascade={"refresh"})      * @orm\joincolumn(name="friendwith", referencedcolumnname="id")      */     protected $friendwith;       /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set requesteddate      *      * @param \datetime $requesteddate      * @return rezo      */     public function setrequesteddate($requesteddate)     {         $this->requesteddate = $requesteddate;          return $this;     }      /**      * requesteddate      *      * @return \datetime       */     public function getrequesteddate()     {         return $this->requesteddate;     }      /**      * set accepteddate      *      * @param \datetime $accepteddate      * @return rezo      */     public function setaccepteddate($accepteddate)     {         $this->accepteddate = $accepteddate;          return $this;     }      /**      * accepteddate      *      * @return \datetime       */     public function getaccepteddate()     {         return $this->accepteddate;     }      /**      * set enduser      *      * @param \core\customerbundle\entity\enduser $enduser      * @return rezo      */     public function setenduser(\core\customerbundle\entity\enduser $enduser = null)     {         $this->enduser = $enduser;          return $this;     }      /**      * enduser      *      * @return \core\customerbundle\entity\enduser       */     public function getenduser()     {         return $this->enduser;     }      /**      * set friendwith      *      * @param \core\customerbundle\entity\enduser $friendwith      * @return rezo      */     public function setfriendwith(\core\customerbundle\entity\enduser $friendwith = null)     {         $this->friendwith = $friendwith;          return $this;     }      /**      * friendwith      *      * @return \core\customerbundle\entity\enduser       */     public function getfriendwith()     {         return $this->friendwith;     } 

when run :

app/console doctrine:schema:update --force

the following mysql table has been created :   create table if not exists `rezo` (   `id` int(11) not null auto_increment,   `user_id` int(11) default null,   `friendwith` int(11) default null,   `requesteddate` datetime not null,   `accepteddate` datetime default null,   primary key (`id`),   key `idx_681fc4ba76ed395` (`user_id`),   key `idx_681fc4b1094ad75` (`friendwith`) ) engine=innodb  default charset=utf8 collate=utf8_unicode_ci auto_increment=1 ; 

in rezocontroller.php controller give enduser opportunity accept contact request fot have created function named : acceptnewrequestaction($id)

public function acceptnewrequestaction($id){          $em = $this->getdoctrine()->getmanager();         $rezo = $em->getrepository('coregeneralbundle:rezo')->find($id);         $user1 = $rezo->getenduser();         $user2 = $rezo->getfriendwith();         $daterequest = $rezo->getrequesteddate();         $rezo->setaccepteddate(new \datetime('now'));         $em->persist($rezo);         $em->flush();          /* check if inverse relation exist */          $query = $em->createquerybuilder();         $query->select('t0.id');         $query->from('coregeneralbundle:rezo','t0');         $query->where('t0.accepteddate null');         $query->andwhere('t0.enduser = :userid');         $query->andwhere('t0.friendwith =:userid2');         $query->setparameters(array('userid'=> $user2, 'userid2'=>$user1));         $result = $query->getquery()->getoneornullresult();          if ( is_object($result))         {             $rezo = $em->getrepository('coregeneralbundle:rezo')->findbyid($result->getid());             $rezo->setaccepteddate(new \datetime('now'));          } else {             $rezo = new rezo();             $rezo->setrequesteddate(new \datetime('now'));             $rezo->setaccepteddate(new \datetime('now'));             $rezo->setenduser($user2);             $rezo->setfriendwith($user1);         }         $em->persist($rezo);         $em->flush();           return $this->render(controller('coregeneralbundle:rezo:rezolist'));     } 

i know how can use results know if 1 object if found or return null , in case exists update or create new one.

thank help

getoneornullresult method tells if record in database found, or not. if return null, means have results, , in case have insert new one. when exists records, method return object instance of entity. means in case have update existing record.

please remember, getoneornullresult method throws exception, when result set not unique.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -