php Doctrine 和 LIKE 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8164682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:05:36  来源:igfitidea点击:

Doctrine and LIKE query

phpmysqldoctrine-orm

提问by Eugene

I have entity for Doctrine:

我有 Doctrine 的实体:

<?php
/**
 * @Entity
 * @Table(name="orders")
 */
class Orders {
    /** @Id @Column(name="OID",type="integer") @GeneratedValue */
    private $id;
    /** @Column(name="Product",type="string")*/
        private $product;
    /** @Column(name="RegCode",type="string")*/
        private $reg_code;
    /** @Column(name="OrderEmail",type="string")*/
    private $email;
}

I need make query like this:

我需要这样查询:

select * from `orders` where `OrderEmail`='[email protected]' and `Product` LIKE 'My Products%'

I try handle query without like:

我尝试处理查询而不像:

$em->getRepository("Orders")->findByEmailAndProduct($uname,$product);

But it make error. Why? Can I do this query without DQL? I want make this query use magic methods findBy**

但它会出错。为什么?我可以在没有 DQL 的情况下执行此查询吗?我想让这个查询使用魔术方法findBy**

回答by Kristian Zondervan

This is not possible with the magic find methods. Try using the query builder:

这对于魔术查找方法是不可能的。尝试使用查询构建器

$result = $em->getRepository("Orders")->createQueryBuilder('o')
   ->where('o.OrderEmail = :email')
   ->andWhere('o.Product LIKE :product')
   ->setParameter('email', '[email protected]')
   ->setParameter('product', 'My Products%')
   ->getQuery()
   ->getResult();

回答by Manse

You can use the createQuerymethod (direct in the controller) :

您可以使用该createQuery方法(直接在控制器中):

$query = $em->createQuery("SELECT o FROM AcmeCodeBundle:Orders o WHERE o.OrderMail =  :ordermail and o.Product like :searchterm")
->setParameter('searchterm', '%'.$searchterm.'%')
->setParameter('ordermail', '[email protected]');

You need to change AcmeCodeBundle to match your bundle name

您需要更改 AcmeCodeBundle 以匹配您的包名称

Or even better - create a repository class for the entity and create a method in there - this will make it reusable

或者甚至更好 - 为实体创建一个存储库类并在其中创建一个方法 - 这将使其可重用

回答by elcukro

This is not possible with the magic methods, however you can achieve this using DQL (Doctrine Query Language). In your example, assuming you have entity named Orders with Product property, just go ahead and do the following:

这对于魔术方法是不可能的,但是您可以使用 DQL(Doctrine Query Language)来实现这一点。在您的示例中,假设您有名为 Orders 且具有 Product 属性的实体,请继续执行以下操作:

$dql_query = $em->createQuery("
    SELECT o FROM AcmeCodeBundle:Orders o
    WHERE 
      o.OrderEmail = '[email protected]' AND
      o.Product LIKE 'My Products%'
");
$orders = $dql_query->getResult();

Should do exactly what you need.

应该做你需要的。

回答by Marco

Actually you just need to tell doctrine who's your repository class, if you don't, doctrine uses default repo instead of yours.

实际上,您只需要告诉doctect 谁是您的存储库类,如果您不知道,则doctrine 使用默认存储库而不是您的存储库。

@ORM\Entity(repositoryClass="Company\NameOfBundle\Repository\NameOfRepository")

@ORM\Entity(repositoryClass="Company\NameOfBundle\Repository\NameOfRepository")

回答by Patrik Ken

you can also do it like that :

你也可以这样做:

$ver = $em->getRepository('GedDocumentBundle:version')->search($val);

$tail = sizeof($ver);