php – 用doctrine解释
发布时间:2020-12-13 22:23:06 所属栏目:PHP教程 来源:网络整理
导读:我问的是一个查询结果.当我使用 MySql和Doctrine时,我得到的结果不一样……我有一个字段在执行时会发生变化(package – id). 我在Mysql中的查询: "SELECT * FROM packages_dates_prices INNER JOIN packages ON packages_dates_prices.id = packages.id AND
我问的是一个查询结果.当我使用
MySql和Doctrine时,我得到的结果不一样……我有一个字段在执行时会发生变化(package – > id).
我在Mysql中的查询: "SELECT * FROM packages_dates_prices INNER JOIN packages ON packages_dates_prices.id = packages.id AND packages_dates_prices.package ='.$id.'" 我在存储库和DQL中的查询: "SELECT a FROM BackBundle:PackagesDatesPrices a INNER JOIN BackBundle:Packages b WHERE a.id = b.id AND a.package = :id " 调试工具栏中的查询: SELECT e0_.id AS id0,e0_.date_start AS date_start1,e0_.date_end AS date_end2,e0_.price AS price3,e0_.id AS id4 FROM packages_dates_prices e0_ INNER JOIN packages e1_ ON ( e0_.id = e1_.id AND e0_.id = ? ) Parameters: [1] 实体包: <?php namespace BackBundleEntity; use DoctrineORMMapping as ORM; use GedmoMappingAnnotation as Gedmo; /** * Packages * * @ORMTable(name="packages") * @ORMEntity(repositoryClass="BackBundleEntityPackagesRepository") */ class Packages { /** * @var integer * * @ORMColumn(name="id",type="integer",nullable=true) * @ORMId * @ORMGeneratedValue(strategy="IDENTITY") * */ private $id; /** * @var string * * @ORMColumn(name="title",type="string",length=255,nullable=true) */ private $title; /** * @var string * * @ORMColumn(name="description",nullable=true) */ private $description; /** * @var string * * @ORMColumn(name="details",nullable=true) */ private $details; /** * @var string * * @ORMColumn(name="essential",nullable=true) */ private $essential; /** * @var string * * @ORMColumn(name="indices",nullable=true) */ private $indices; /** * @var string * * @ORMColumn(name="mysterious",nullable=true) */ private $mysterious; /** * @var string * * @ORMColumn(name="products",nullable=true) */ private $products; /** * @var string * * @ORMColumn(name="attachment",nullable=true) */ private $attachment; /** * @var string * * @ORMColumn(name="meta_keywords",nullable=true) */ private $metaKeywords; /** * @var string * * @ORMColumn(name="meta_description",nullable=true) */ private $metaDescription; /** * @var DateTime * * @GedmoTimestampable(on="create") * @ORMColumn(name="created_at",type="datetime",nullable=true) */ private $createdAt; /** * @var string * * @ORMColumn(name="agent",nullable=true) */ private $agent; /** * @var string * * @ORMColumn(name="place",nullable=true) */ private $place; /** * @var string * * @ORMColumn(name="theme",nullable=true) */ private $theme; /** * @var DateTime * * @GedmoTimestampable(on="update") * @ORMColumn(name="updated_at",nullable=true) */ private $updatedAt; /** * @var Boolean * * @ORMColumn(name="is_active",type="boolean",nullable=true) */ private $isActive; /** * @var Boolean * * @ORMColumn(name="is_home",nullable=true) */ private $isHome; /** * @var string * * @ORMColumn(name="link",nullable=true) */ private $link; /** * @var string * * @ORMColumn(name="order_home",nullable=true) */ private $orderHome; /** * @GedmoSlug(fields={"title"}) * @ORMColumn(length=128,unique=true) */ private $slug; /** * @var Boolean * * @ORMColumn(name="is_outing",nullable=true) */ private $isOuting; /** * @var Boolean * * @ORMColumn(name="is_weekend",nullable=true) */ private $isWeekend; /** * @var Boolean * * @ORMColumn(name="is_wedding",nullable=true) */ private $isWedding; /** * @var Boolean * * @ORMColumn(name="is_present",nullable=true) */ private $isPresent; /** * @var Boolean * * @ORMColumn(name="is_company",nullable=true) */ private $isCompany; /** * @var Boolean * * @ORMColumn(name="is_solo",nullable=true) */ private $isSolo; /** * @var Boolean * * @ORMColumn(name="is_couple",nullable=true) */ private $isCouple; /** * @var Boolean * * @ORMColumn(name="is_friends",nullable=true) */ private $isFriends; /** * @var Boolean * * @ORMColumn(name="is_family",nullable=true) */ private $isFamily; /** * @var DoctrineCommonCollectionsCollection * @ORMOneToMany(targetEntity="BackBundleEntityPackagesDatesPrices",mappedBy="Package") **/ private $datesPrices; public function __construct() { $this->createdAt = new Datetime(); $this->datesPrices = new DoctrineCommonCollectionsArrayCollection(); } /** * @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"; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get isFamily * * @return boolean */ public function getIsFamily() { return $this->isFamily; } /** * Add datesPrices * * @param BackBundleEntityPackagesDatesPrices $datesPrices * @return Packages */ public function addDatesPrice(BackBundleEntityPackagesDatesPrices $datesPrices) { $this->datesPrices[] = $datesPrices; $datesPrices->setPackage($this); return $this; } /** * Remove datesPrices * * @param BackBundleEntityPackagesDatesPrices $datesPrices */ public function removeDatesPrice(BackBundleEntityPackagesDatesPrices $datesPrices) { $this->datesPrices->removeElement($datesPrices); } /** * Get datesPrices * * @return DoctrineCommonCollectionsCollection */ public function getDatesPrices() { return $this->datesPrices; } } PackagesDatesPrices: <?php namespace BackBundleEntity; use DoctrineORMMapping as ORM; use GedmoMappingAnnotation as Gedmo; /** * * @ORMTable(name="packages_dates_prices") * @ORMEntity(repositoryClass="BackBundleEntityPackagesDatesPricesRepository") */ class PackagesDatesPrices { /** * @var integer * * @ORMColumn(name="id",nullable=true) * @ORMId * @ORMGeneratedValue(strategy="IDENTITY") **/ private $id; /** * @var BackBundleEntityPackages * @ORMManyToOne(targetEntity="BackBundleEntityPackages",inversedBy="datesPrices") * @ORMJoinColumn(name="id",referencedColumnName="id") **/ private $package; /** * @var BackBundleEntityPackages * @var DateTime * * @ORMColumn(name="date_start",nullable=true) */ private $dateStart; /** * @var DateTime * * @ORMColumn(name="date_end",nullable=true) */ private $dateEnd; /** * @var integer * * @ORMColumn(name="price",nullable=true) */ private $price; public function __construct() { $this->children = new DoctrineCommonCollectionsArrayCollection(); } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get price * * @return integer */ public function getPrice() { return $this->price; } /** * Set package * * @param BackBundleEntityPackages $package * @return PackagesDatesPrices */ public function setPackage(BackBundleEntityPackages $package = null) { $this->package = $package; return $this; } /** * Get package * * @return BackBundleEntityPackages */ public function getPackage() { return $this->package; } } PackagesDatesPricesRepository.php: <?php namespace BackBundleEntity; use DoctrineORMEntityRepository; /** * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class PackagesDatesPricesRepository extends EntityRepository { public function getTest($id) { $req=$this->getEntityManager()->createQuery ( "SELECT a FROM BackBundle:PackagesDatesPrices a INNER JOIN BackBundle:Packages b WHERE a.package = b.id AND b.id = :id " ); $req ->setParameter('id',$id); return $req->getResult(); } } 我的控制器: public function packagePageAction($id) { $test = $this->getDoctrine() ->getManager() ->getRepository('BackBundle:PackagesDatesPrices') ->getTest($id) ; return $this->render('BackBundle:Global:packagePage.html.twig',array( 'test' => $test )); } 我在包的id(字段的名称是id)和packages_dates_prices(字段的名称是包)之间有一个关系OneToMany. 我还没有发现错误. 谢谢你读我并帮助我. 解决方法
您可以在Symfony2文档部分
Fetching Related Objects中阅读此内容.
无需编写任何SQL语句即可获得您期望的结果. $package = $this->getDoctrine()->getRepository("BackBundle:Packages")->find($id); $datesPrices = $package->getDatesPrices(); $datesPrices将包含通过OneToMany关系连接的所有行. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |