[英]Doctrine "Like" DQL
我是 Doctrine 的新手,並試圖弄清楚 DQL 查詢。
我有 2 個實體國家和地點。
國家
<?php
namespace X\application\model\entity;
/** @Entity */
class country
{
/**
* @Id
* @Column(type="string",length=2)
* @GeneratedValue(strategy="NONE")
*/
protected $id;
/** @Column(type="string",length=255,nullable=false) */
protected $name;
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
}
場地
<?php
namespace X\application\model\entity;
/** @Entity */
class venue
{
/**
* @var \Ramsey\Uuid\Uuid
*
* @Id
* @Column(type="uuid")
* @GeneratedValue(strategy="NONE")
*/
protected $id;
/** @Column(type="string",length=255,nullable=true) */
protected $building;
/** @Column(type="string",length=255,nullable=false) */
protected $street;
/** @Column(type="string",length=255,nullable=false) */
protected $city;
/** @Column(type="string",name="post_code",length=255,nullable=true) */
protected $postCode;
/** @Column(type="string",name="contact_number",length=255,nullable=true) */
protected $contactNumber;
/** @Column(type="text",name="point_of_contact",length=20000,nullable=true) */
protected $pointOfContact;
/** @Column(type="text",length=20000,nullable=true) */
protected $note;
/**
* @ManyToOne(targetEntity="country", inversedBy="id")
**/
protected $country;
/**
* venue constructor.
* @param $building
* @param $street
* @param $city
* @param $postCode
* @param $contactNumber
* @param $pointOfContact
* @param $note
* @param $country
*/
public function __construct($building, $street, $city, $postCode, $contactNumber, $pointOfContact, $note, $country)
{
$this->id = \Ramsey\Uuid\Uuid::uuid4();
$this->building = $building;
$this->street = $street;
$this->city = $city;
$this->postCode = $postCode;
$this->contactNumber = $contactNumber;
$this->pointOfContact = $pointOfContact;
$this->note = $note;
$this->country = $country;
}
/**
* @return \Ramsey\Uuid\Uuid
*/
public function getId()
{
return $this->id;
}
/**
* @param \Ramsey\Uuid\Uuid $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getBuilding()
{
return $this->building;
}
/**
* @param mixed $building
*/
public function setBuilding($building)
{
$this->building = $building;
}
/**
* @return mixed
*/
public function getStreet()
{
return $this->street;
}
/**
* @param mixed $street
*/
public function setStreet($street)
{
$this->street = $street;
}
/**
* @return mixed
*/
public function getCity()
{
return $this->city;
}
/**
* @param mixed $city
*/
public function setCity($city)
{
$this->city = $city;
}
/**
* @return mixed
*/
public function getPostCode()
{
return $this->postCode;
}
/**
* @param mixed $postCode
*/
public function setPostCode($postCode)
{
$this->postCode = $postCode;
}
/**
* @return mixed
*/
public function getContactNumber()
{
return $this->contactNumber;
}
/**
* @param mixed $contactNumber
*/
public function setContactNumber($contactNumber)
{
$this->contactNumber = $contactNumber;
}
/**
* @return mixed
*/
public function getPointOfContact()
{
return $this->pointOfContact;
}
/**
* @param mixed $pointOfContact
*/
public function setPointOfContact($pointOfContact)
{
$this->pointOfContact = $pointOfContact;
}
/**
* @return mixed
*/
public function getNote()
{
return $this->note;
}
/**
* @param mixed $note
*/
public function setNote($note)
{
$this->note = $note;
}
/**
* @return country | null
*/
public function getCountry()
{
return $this->country;
}
/**
* @param mixed $country
*/
public function setCountry($country)
{
$this->country = $country;
}
}
當我執行此查詢時
SELECT venue FROM X\application\model\entity\venue venue WHERE venue.building
LIKE '%fa%' OR venue.street LIKE '%fa%' OR venue.city LIKE '%fa%' OR
venue.contactNumber LIKE '%fa%' OR venue.pointOfContact LIKE '%fa%' ORDER BY
venue.building ASC
它工作得很好,但是當我添加 country(venue.country LIKE '%search%') 時,例如它不起作用
SELECT venue FROM X\application\model\entity\venue venue WHERE venue.building
LIKE '%fa%' OR venue.street LIKE '%fa%' OR venue.city LIKE '%fa%' OR
venue.contactNumber LIKE '%fa%' OR venue.pointOfContact LIKE '%fa%'
venue.country LIKE '%fa%' ORDER BY venue.building ASC
任何人都可以幫我解決這個問題,我確信在純 SQL 中您會進行內部聯接,但您在 Doctrine 中的表現如何。 感謝您閱讀這么長的問題。 :)
讓我借此機會回答我自己的問題。 我需要的是內連接。 我相信它的工作方式與 SQL 查詢中的工作方式相同。
INNER JOIN X\application\model\entity\country country WITH venue.country = country.id
通過將此部分添加到查詢之后,它可以正常工作。 所以我的最終查詢看起來像
SELECT venue FROM X\application\model\entity\venue venue INNER JOIN
X\application\model\entity\country country WITH venue.country =
country.id WHERE venue.building LIKE '%United Kingdom%' OR venue.street LIKE
'%United Kingdom%' OR venue.city LIKE '%United Kingdom%' OR venue.contactNumber
LIKE '%United Kingdom%' OR venue.postCode LIKE '%United Kingdom%' OR
venue.pointOfContact LIKE '%United Kingdom%' OR country.name LIKE '%United
Kingdom%' ORDER BY venue.building ASC
您可以在 QueryBuilder 文檔中進一步了解 Doctrine Inner join
http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/query-builder.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.