【问题标题】:Doctrine "Like" DQL教义“喜欢” DQL
【发布时间】:2016-05-28 10:53:18
【问题描述】:

我是 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

它工作得很好,但是当我添加国家(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 中的表现如何。感谢您阅读长问题。 :)

【问题讨论】:

    标签: php sql orm doctrine-orm doctrine


    【解决方案1】:

    让我借此机会回答我自己的问题。 我需要的是内部连接。我相信它的工作方式与 SQL 查询中的工作方式相同。

    INNER JOIN X\application\model\entity\country country WITH venue.country = country.id
    

    通过在 from 之后将此部分添加到查询中,它可以正常工作。所以我的最终查询看起来像

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-06
      • 2015-03-10
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多