如何在 cakePHP 和 Simpletest 中执行复杂的搜索并测试它们

发布于 2024-10-16 20:04:33 字数 802 浏览 7 评论 0原文

我有一个非常复杂的数据结构,比如 10 个没有连接表的表。我的应用程序需要能够在大多数表中执行搜索。

为此,我将搜索字段的内容转换为条件数组。键是模型名称,值是搜索条件,即

$conditions = array(
        'Artist' => array(
                'OR' => array(
                    'Artist.name LIKE' => '%barl%', 
                    'Pseudonym.name LIKE' => '%barl%'
                    )
                ),
        'Content' => array('Content.subject' => 'architecture'),
        'Editor'  => array('Editor.name LIKE' => '%Gal%'),
        etc....
    )

该数组传递给可搜索的模型,每个模型都采用相关的条件。

$this->find('all', array('conditions' => $conditions['Artist']))

到目前为止一切都很好,至少我认为。现在我开始测试模型,我发现自己在不同的模型测试用例中一遍又一遍地复制同一个数组,这让我很烦恼。

有没有办法让每个测试用例都可以访问这个数组?也许数组不是最好的解决方案,我应该建立一个搜索模型?

有什么建议吗?

I have a very complex data structure, something like 10 tables without the join tables. My application needs to be able to perform search in most of the tables.

To do this, I though to turn the content of the search fields into an array of conditions. The key is the model name, the value is the search conditions, i.e.

$conditions = array(
        'Artist' => array(
                'OR' => array(
                    'Artist.name LIKE' => '%barl%', 
                    'Pseudonym.name LIKE' => '%barl%'
                    )
                ),
        'Content' => array('Content.subject' => 'architecture'),
        'Editor'  => array('Editor.name LIKE' => '%Gal%'),
        etc....
    )

This array gets passed to the models that are searchable and each model takes the condition that is relevant.

$this->find('all', array('conditions' => $conditions['Artist']))

So far so good, at least I think. Now I started to test the models and I found myself copying over and over that same array in the different model test cases, and that bothers me.

Is there a way to have this array accessible to every test cases? Maybe the array is not the best solution and I should make a search model?

Any suggestions?

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

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

发布评论

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

评论(1

不可一世的女人 2024-10-23 20:04:33

将数组作为属性放在 app_model.php 中,如 $commonSearchConditions 之类的内容,并从应该继承 AppModel 的模型内部访问它们。

根据您具体执行的操作,如果每个模型中的搜索不同,我将在每个模型测试中都有一个测试用例。如果没有,您可能想使用您在测试内部创建的测试模型创建一个单独的测试,以仅测试您想要执行的搜索内容。如果不了解更多,很难说清楚。

Put the array as property in the app_model.php as something like $commonSearchConditions and access them from inside your models which should inherit the AppModel.

Depending on what exactly you do, if the searches differ in every model, I would have a test-case in every models test. If not you might want to create a separate test with a test model you create inside of the test for testing just the search stuff you want to do. Hard to tell without knowing more.

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