gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:07:29 26 4
gpt4 key购买 nike

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

这两个实体都被定义为:

最终用户实体:

<?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


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 ;

在 RezoController.php Controller 中,我想给最终用户机会接受联系请求为此我创建了一个名为: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 IS 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'));
}

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

感谢您的帮助

最佳答案

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

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

关于php - 如何使用 getQuery()->getOneOrNullresult() 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com