CakePHP 3.x搜索表单,包含11种不同的模型 [英] CakePHP 3.x search form with 11 different models

查看:78
本文介绍了CakePHP 3.x搜索表单,包含11种不同的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP 3.5.13中有一个应用程序。该应用程序已连接到旧数据库-也就是说,它尚未按照Cake的命名约定编写。

I have an application in CakePHP 3.5.13. The application is connected to a legacy database - that is to say it has not been written according to Cake's naming conventions.

我烘焙了该应用程序,该应用程序生成了 Table Entity 类。

I have baked the application which has generated some Table and Entity classes.

应用程序的一部分允许用户执行搜索。我想使用表单助手,但是不确定如何命名/配置事物:

Part of the application allows the user to perform a search. I'm wanting to use the Form Helper but am unsure how to name/configure things:

搜索表单所在的模板文件具有11个文本输入。这些都与存储在数据库中的数据有关。为简便起见,我将考虑前三个字段。这些与 Table 类相关:

The template file where the search form lives has 11 text inputs. Each of these relates to data stored in the database. For brevity I'll consider the first 3 fields. These are associated with the Table classes:


  • CAS号-与<$相关c $ c> CasTable

  • EC号-与 EcsTable

  • 相关
  • 物质名称-与 SubstancesTable

  • CAS Number - related to CasTable
  • EC Number - related to EcsTable
  • Substance Name - related to SubstancesTable

打开表单标签时相关我已执行以下操作:

When opening the form tag I have done the following:

<?php echo $this->Form->create(null, [
'url' => ['controller' => 'Front', 'action' => 'search'] ]); ?>

我通过 null 作为原因的原因第一个参数是因为我不知道是否应该使用 Entity 的实例(如文档),但不确定是哪一个,因为总共有11个输入其中与不同的 Table 类相关。所以我相信 null 在这里是正确的-请有人澄清一下吗?

The reason I have passed null as the first parameter is because I don't know whether I'm supposed to use the instance of an Entity (as mentioned in the docs), but wasn't sure which one, since there are 11 inputs all of which are associated with different Table classes. So I believe null to be correct here - Please can someone clarify this?

我也不确定如何命名我的输入。例如,搜索 CAS号的输入需要在数据库字段 cas.value 上进行搜索。所以我会这样命名输入吗?

I'm also unsure how to name my inputs. For example the input searching for "CAS Number" needs to do a search on the database field cas.value. So would I name the input like this?

echo $this->Form->control('cas.value' ... );

我希望能够根据适当模型中的规则验证每个字段。因为有11个不同的字段,并且表单不充当添加(SQL INSERT )或编辑(SQL UPDATE )函数,我无法按照Cake的文档来理解我应该如何编写此代码。

I want to be able to validate each field according to rules that would be in the appropriate models. Because there are 11 different fields, and the form is not acting as either an "add" (SQL INSERT) or "edit" (SQL UPDATE) function, I cannot follow the Cake documentation to understand how I'm supposed to code this.

总而言之,我正在尝试做什么使用Cake的Form Helper语法构造一个HTML表单,其中包含11个字段,所有这些字段都与适当的模型相关联。我希望能够按照这些模型的规则来验证字段。我怎样才能做到这一点?我已经对其进行了研究,但是看不到任何可以做到这一点的示例-大多数都专注于插入/更新数据。我还查看了无模式表格,但没有认为是正确的,因为在这种情况下,我确实具有用于搜索数据的模型。

In summary what I'm trying to do is construct a HTML form - using Cake's Form Helper syntax - with 11 fields all of which are associated with appropriate models. I want to be able to validate the fields as per rules of those models. How can I do this? I have done research into it but cannot see any examples that do this - most focus on inserting/updating data. I also had a look at Modeless Forms but don't think that's right because in this case I do have models for the search data.

目前,我只是简单地将表单编写为原始HTML并可以访问发布到 FrontController :: search() $ this-> request-> data()。尽管此方法有效,但我认为这不是一个好主意,因为我必须以某种方式运行验证方法,这可能会导致重新填充表单或稍后使用ORM时出现问题。我在这里可能是错的,但是我正在寻找一种使用Cake约定的解决方案,特别是在这种情况下,着重于正确使用Form Helper。

At the moment I have simply written the form as vanilla HTML and can access the data posted to FrontController::search() through $this->request->data(). Whilst this works I believe this is a bad idea since I'd have to somehow run validation methods, and it may cause problems re-populating the form, or using the ORM later. I may be wrong here but am looking for a solution which uses Cake conventions, particularly focusing on correct use of the Form Helper in this type of situation.

推荐答案

我实际上不明白为什么必须验证搜索(我认为如果出于某种原因搜索输入之一不正确,则查询将仅返回零记录),但我想您已经原因是这里是怎么做的。无论如何,这甚至对于保存实体也是如此,所以:

I don't actually see why you have to validate the search (I think that if one of the search inputs for some reason is not ok then the query will simply return zero records) but I guess you have your reasons so here is how to do it. Anyway this works even for saving entities so:

使用命名约定

第一所有使用g蛋糕命名约定的控件,如 此处 在此处此处

First of all use g cake naming convention for your controls as explained here and here

即使您的数据库命名不符合蛋糕标准,它也可以工作。

It works even if your DB naming does not follows cake standard.

创建验证规则

然后在每个表的内容将验证集设置为crete。您可以针对不同的用途(保存,更新,搜索)创建不同的验证(请参见 此处 )。

Then in each of your table crete the validations set. You can create different validation for differents uses (save, update, search) (see here). In your case you want to create a set for the search.

因此,在您的表(每个表中!)中,您可以创建一个名为的方法。 validationSearch

So in your tables (in every table!) you can create a method named validationSearch

public function validationSearch($validator)
{
    $validator
        ->add('title', 'notEmpty', [
            'rule' => 'notEmpty',
            'message' => __('You need to provide a title'),
        ]);
    return $validator;
}

修补实体

然后在您的 FrontController 中可以

$this->loadModel('Substances');
$substance = $this->Substances->newEntity(
    $this->request->getData(),
    [
        'validate' => 'search', // tells cake to use validateSearch
                                // for Substances Table
        'associated' => [
            'Cas' => [
                'validate' => 'search', // you want to use validateSearch
                                        // even for this table
            ],
            'Ecs'=> [
                'validate' => 'search', // and for this table, too
            ],
            // list of other related tables here
            // you can nest the array if you have
            // nested relationships
        ]
    ]
);

这将创建一个包含您的数据的实体,但最重要的是它将检查验证问题并存储错误在 $物质实体中。

this will create an entity containing your data but above all it will check for validation issues and store the errors in the $substance entity.

$this->set('substance', $substance );

以上内容会将您新创建的实体传递给表单

the above will pass your newly created entity to the form

创建表单

最后在视图中

<?php echo $this->Form->create($substance , [ /*... */ ]); ?>

因此,表单知道该实体,要应用的验证规则以及要显示的验证错误

So the form knows about the entity, the validation rules to apply and the validation errors to show

创建控件

假设您有一个主表,这意味着所有其他表格与此相关。假设是物质。因此,请遵循约定。

Suppose you have a "main" table, meaning that all the other tables are somehow related to this. Let's say it is Substances. So follow the conventions.

<?= $this->Form->control('name', ['label' => 'Substance name') ?> 
//                        ^ No need to prepend the table here
<?= $this->Form->control('cas.number', ['label' => 'Cas number') ?>
<?= $this->Form->control('ecs.number', ['label' => 'Ecs number') ?>

使用插件

,并考虑使用FriendsOfCake / search插件。拥有如此众多的字段,可以简化您的生活

And consider using using FriendsOfCake/search plugin. With so many fields to check it will simplify your life

这篇关于CakePHP 3.x搜索表单,包含11种不同的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆