PHP Doctrine SoftDelete - 包括已删除的记录?

发布于 2024-08-03 03:15:41 字数 373 浏览 11 评论 0原文

如果我有一个 PHP Doctrine 对象充当 SoftDelete,是否可以在某些查询的结果中包含已删除的项目?我正在寻找的是这样的东西......

$q = Doctrine_Query::create()
    ->select('*')
    ->from('Test t')
    ->where('id < ?', 25)
    *->includeDeleted()*;

这样的东西对于大多数查询很有用,我希望排除已删除的记录,但有时(例如,对于管理员)我希望能够包含软记录已删除。有没有一些好的方法可以使用 SoftDelete 来做到这一点,或者我应该简单地在大多数查询中添加一个额外的 where 子句?

If I have one of my PHP Doctrine objects act as a SoftDelete, is it possible to include deleted items in the results of certain queries? What I'm looking for is something like this...

$q = Doctrine_Query::create()
    ->select('*')
    ->from('Test t')
    ->where('id < ?', 25)
    *->includeDeleted()*;

Something like this would be useful as for most queries I want deleted records excluded, but sometimes (for example, to administrators) I want to be able to include records that have been soft deleted. Is there some good way to do this with SoftDelete or should I simply add an additional where clause onto most queries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恏ㄋ傷疤忘ㄋ疼 2024-08-10 03:15:41

它返回表中的所有记录(包括 softDeleted)

public function findAllWithDeleted()
{
    $query = $this->createQuery('a');
    $query->addWhere('(a.deleted_at IS NULL OR a.deleted_at IS NOT NULL)');
    return $query->execute();
}

It returns all records from table (include softDeleted)

public function findAllWithDeleted()
{
    $query = $this->createQuery('a');
    $query->addWhere('(a.deleted_at IS NULL OR a.deleted_at IS NOT NULL)');
    return $query->execute();
}
过气美图社 2024-08-10 03:15:41

看起来您可以禁用软删除过滤器
http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html

// 这将禁用 SoftDeleteable 过滤器,因此“软删除”的实体将出现在结果中
$em->getFilters()->disable('软删除');

Looks like you may disable soft-deleteable filter
http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html

// This will disable the SoftDeleteable filter, so entities which were "soft-deleted" will appear in results
$em->getFilters()->disable('soft-deleteable');

此刻的回忆 2024-08-10 03:15:41

粗略浏览一下此处 和此处 ,此功能似乎不是由 SoftDelete 行为提供的。您必须按照您的建议手动添加 where 子句。

(我可能是错的,但我很确定 Doctrine_Query 不能通过像 Doctrine_Record 那样的行为来动态扩展。您可以 总是编写一个特殊的 Doctrine_Query 子类来添加 includeDeleted(),但这似乎比它的价值更值得:))

From a cursory glance at the source here and here, this functionality does not seem to provided by the SoftDelete behaviour. You have to add a where clause manually like you suggest.

(I may be wrong, but I'm pretty sure Doctrine_Query can not be extended dynamically by way of a behaviour like you can with Doctrine_Record. You could always write a special Doctrine_Query subclass which adds includeDeleted(), but that seems like more thouble than it's worth :) )

剩余の解释 2024-08-10 03:15:41
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);

此后您可以选择、更新、删除数据,如软删除关闭。

$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);

After this you can select, update, delete data like softDelete is off.

你又不是我 2024-08-10 03:15:41

我们在 Doctrine 1.2.2 中遇到了一个错误,其中 ATTR_USE_DQL_CALLBACKS 被忽略,并且 softdelete 列的名称错误。解决方案是使用

$m = new MyModel;
$m->setListener(new MockListener());
$m->getTable()->getQueryObject(); //... and so forth

虚拟类覆盖侦听器:

class MockListener implements Doctrine_Overloadable
{
    public function __call($m, $a){}
}

We had a bug in Doctrine 1.2.2 where ATTR_USE_DQL_CALLBACKS was ignored AND the softdelete column had the wrong name. The solution is to overwrite the listener:

$m = new MyModel;
$m->setListener(new MockListener());
$m->getTable()->getQueryObject(); //... and so forth

with a dummy class:

class MockListener implements Doctrine_Overloadable
{
    public function __call($m, $a){}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文