本文介绍了从cakephp v1.3中的数据库中只提取“启用”记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何方法只提取模型的那些在cakephp v1.3中有'status = 1'的记录?

Is there any way of fetching only those records of a model which have 'status = 1' in cakephp v1.3 ?

我创建了一个名为'

我有一个名为Message的模型。我想要的是,只有那些消息被显示,包括在状态= 1的搜索结果中。 status = 0的邮件将不会显示,也不会显示在搜索结果中。

I have a model named 'Message'. What I want is that only those messages are displayed, included in search results which have 'status = 1'. Messages with a 'status = 0' won't be displayed and won't be included in search results too.

请帮助我,如果我能做到cakePHP方式,现在作为一个临时解决方案,我在每次查找消息的条件数组中使用'status = 1'。

Please help me if I can do it the cakePHP way, for now as a temporary solution I am using the 'status = 1' in the conditions array of every find of messages.

感谢

推荐答案

我会将它包含在 beforeFind()回调方法中。您可以将其包含在每个模型的回调中(但每个模型只有一次,而不是每个查询),也可以将其应用到 AppModel

I'd include this in the beforeFind() callback method. You can include it in the callback for every model (but only once for each model--not for each query) or you can apply it to your AppModel so that all of your other models inherit it.

beforeFind()回调中,您可以修改查询的结构。在这种情况下,您可以对传入的查询应用条件 array('status'=> 1)

In the beforeFind() callback, you can modify the structure of the query. In this case, you'd apply the condition array( 'status' => 1 ) to your incoming query. Dump the argument data structure to see what's coming in and I think it will make sense to you.

请注意,如果您选择在 AppModel ,并且必须在将来创建一个特定于 beforeFind 的模型,你需要确保你调用<$ c $ c>

Note that if you chose to do this in your AppModel and have to create a model-specific beforeFind callback in the future, you'll need to be sure that you call parent::beforeFind() in your subclass' beforeFind() method.

这篇关于从cakephp v1.3中的数据库中只提取“启用”记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:15