php 教义——或在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6878448/
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 01:31:51  来源:igfitidea点击:

Doctrine - or where?

phpdoctrine

提问by Splendid

I have the following query:

我有以下查询:

$query = Doctrine_Query::create()
                ->from('Member m')
                    ->where("m.type='1'")
                        ->andWhere("m.name LIKE '%$term%'")
                        ->orWhere("m.surname LIKE '%$term%'")
                        ->orWhere("m.company LIKE '%$term%'")
                        ->orderBy('id DESC');

But it's not working like I want — it is ignoring typecolumn.

但它不像我想要的那样工作——它忽略了type列。

What I need is result set where m.type=1and some of other fields in this query is LIKE 'something'.

我需要的是结果集 wherem.type=1和此查询中的其他一些字段是LIKE 'something'.

回答by Tom

$query = Doctrine_Query::create()
  ->from('Member m')
  ->where('m.type = 1 AND m.name LIKE ?', '%'.$term.'%')
  ->orWhere('m.type = 1 AND m.surname LIKE ?', '%'.$term.'%')
  ->orWhere('m.type = 1 AND m.company LIKE ?', '%'.$term.'%')
  ->orderBy('m.id DESC');

Your OR conditions didn't include the first condition. It's also recommended to use the ?for your variables to ensure Doctrine escapes them.

您的 OR 条件不包括第一个条件。还建议?为您的变量使用 以确保 Doctrine 逃脱它们。

回答by adlawson

Tom's answeris correct, although I like to keep code repetition/duplication to a minimum.

汤姆的回答是正确的,尽管我喜欢将代码重复/重复保持在最低限度。

This way should also work, while being a shorter, cleaner way to do it

这种方式也应该有效,同时是一种更短、更干净的方式

$query = Doctrine_Query::create()
       ->from('Member m')
       ->where('m.type = ?', 1)
       ->andWhere('m.name LIKE :term OR m.surname LIKE :term OR m.company LIKE :term', array(':term' => '%' . $term . '%'))
       ->orderBy('m.id DESC');