如何使用getQuery() - > getOneOrNullresult()返回

时间:2014-08-18 10:59:48

标签: php symfony doctrine

在我的测试项目中我有2个实体: - endUser(FOSUserBundle的扩展) - Rezo(将包含两个成员之间的批准关系)

两个实体都被定义为:

EndUser实体:

<?php

namespace Core\CustomerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as 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");
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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);
    }

    /**
     * Get 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);
    }

    /**
     * Get 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);
    }

    /**
     * Get 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);
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get DateNaissance
     *
     * @return \DateTime 
     */
    public function getDateNaissance()
    {
        return $this->DateNaissance;
    }
}

<?php namespace Core\CustomerBundle\Entity; use Doctrine\ORM\Mapping as ORM; use FOS\UserBundle\Model\User as BaseUser; use Symfony\Component\Validator\Constraints as 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"); } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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); } /** * Get 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); } /** * Get 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); } /** * Get 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); } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get DateNaissance * * @return \DateTime */ public function getDateNaissance() { return $this->DateNaissance; } }

Rezo实体:

<?php

namespace Core\GeneralBundle\Entity;

use Doctrine\ORM\Mapping as 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;


    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get friendwith
     *
     * @return \Core\CustomerBundle\Entity\EndUser 
     */
    public function getFriendwith()
    {
        return $this->friendwith;
    }

我跑的时候:

  

app / console doctrine:schema:update --force

<?php

namespace Core\GeneralBundle\Entity;

use Doctrine\ORM\Mapping as 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;


    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get 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;
    }

    /**
     * Get friendwith
     *
     * @return \Core\CustomerBundle\Entity\EndUser 
     */
    public function getFriendwith()
    {
        return $this->friendwith;
    }

在RezoController.php控制器中,我想给endUser机会接受一个联系请求,我已经创建了一个名为:acceptnewrequestAction($ id)的函数

我想知道如何使用结果来了解一个对象(如果找到或返回)是否为Null,如果存在则更新它或创建一个新对象。

谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

getOneOrNullResult方法告诉您是否找到数据库中的任何记录。 如果它返回null,则表示您有一些结果,在您的情况下,您必须插入新的结果。 但是当它存在一些记录时,此方法将返回实体的对象实例。这意味着在您的情况下,您必须更新现有记录。

请记住,当结果集不唯一时,getOneOrNullResult方法会抛出异常。