php 教义 findby 魔法空值

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

doctrine findby magic null value

phpdoctrinedoctrine-1.2findby

提问by Sean

I'm wondering if there is anyway to use doctrine's magic method to look for null values. For instance:

我想知道是否有任何使用学说的魔术方法来查找空值的方法。例如:

Doctrine::getTable('myClass')->findByDeletedAt(null);

Essentially, I want to return all records that are not deleted. I've tried the above, but it doesn't seem to work.

本质上,我想返回所有未删除的记录。我已经尝试了上面的方法,但它似乎不起作用。

Any ideas?

有任何想法吗?

回答by Daniel W.

Trying this gives me the error:

尝试这给了我错误:

Catchable fatal error: Argument 1 passed to Doctrine\ORM\EntityRepository::findBy() must be an array, string given

可捕获的致命错误:传递给 Doctrine\ORM\EntityRepository::findBy() 的参数 1 必须是一个数组,给出字符串

So this works for me:

所以这对我有用:

$repository->findBy(array('date_field' => null));

回答by Sean

Actually, I think I was trying to be to fancy. It can be done like this:

事实上,我想我是想变得花哨。可以这样做:

Doctrine::getTable('myClass')->findBy('deleted_at',null);

回答by regomodo

I know this is old but your 1st attempt was incorrect. For a field 'deleted_at' you should have been doing this:

我知道这很旧,但您的第一次尝试不正确。对于字段“deleted_at”,您应该这样做:

Doctrine::getTable('YourClass')->findByDeleted_at($val);

Notice I only capitalised the 1st letter of the field 'deleted_at'.

请注意,我只将“deleted_at”字段的第一个字母大写。

回答by Tomasz Rozmus

I know this is old, but provided solutions are not correct for Doctrine 1.2.x

我知道这是旧的,但提供的解决方案对 Doctrine 1.2.x 不正确

When you use functions like:

当您使用以下函数时:

Doctrine::getTable('YourClass')->findByDeleted_at(null); //he want to get NOT DELETED rows!

Doctrine throws an error that: "You must specify the value to findBy", because null is threaten as no passed value;

Doctrine 抛出一个错误:“您必须指定 findBy 的值”,因为null 是威胁,因为没有传递值

->findBy('deleted_at', null)throws an exception too.

->findBy('deleted_at', null)也抛出异常。

->findBy('deleted_at','NULL')is either not correct becouse it is translated to SQL: ... WHERE deleted_at = 'NULL';

->findBy('deleted_at','NULL')要么不正确,因为它被翻译成 SQL: ... WHERE deleted_at = 'NULL';

SQL syntax for such query should look like:

此类查询的 SQL 语法应如下所示:

... WHERE deleted_at IS NULL;

So the solution is to use ->findBySql()method:

所以解决方法是使用->findBySql()方法:

Doctrine::getTable('YourClass')->findBySql('deleted_at IS NULL', array());

* third argument could be a Hydration_Mode.

* 第三个参数可以是 Hydration_Mode。

Hope that will help someone...

希望这会帮助某人...