CakePHP 3:自定义数据类型无法正常工作(Cakephp 3: Custom Datatype not working properly)

编程入门 行业动态 更新时间:2024-10-28 11:21:48
CakePHP 3:自定义数据类型无法正常工作(Cakephp 3: Custom Datatype not working properly)

我正尝试在cakephp 3中为多选下拉菜单创建自定义数据类型。 我的表单中有一个multiple => true控件:

- PHP - HTML - CSS

当我提交这个表单时,我将该控件的值作为一个数组( 0 => PHP, 1 => HTML ),这很好,现在我想将这些值保存为字符串格式,就像这个PHP,HTML一样PHP,HTML并返回再次作为数组。

此输入的保存进行得很顺利,但数据仅以string的形式检索。 为此,我按照这个答案创建了一个自定义数据类型:

class MultiSelectType extends Type { public function toPHP($value, Driver $driver) { if (!$value) { return null; } return explode(',', $value); } public function marshal($value) { return explode(',', $value); } public function toDatabase($value, Driver $driver) { return implode(',', $value); } public function toStatement($value, Driver $driver) { if ($value === null) { return PDO::PARAM_NULL; } return PDO::PARAM_STR; } }

我如何修改此代码

返回array以便我的表单将自动从多个选择框中选择值。

只要我想将该值显示为一个字符串就可以返回字符串。

I am trying to create a custom datatype in cakephp 3 for multi select dropdown. I have a multiple => true control in my form:

- PHP - HTML - CSS

When I submit this form, I get the value for that control as an array (0 => PHP, 1 => HTML), that is fine, Now I want to save these value in string format like this PHP,HTML and retrieve back as an array again.

Saving of this input is going well but data is retrieved in the form of string only. For this I have followed this answer and created a custom datatype:

class MultiSelectType extends Type { public function toPHP($value, Driver $driver) { if (!$value) { return null; } return explode(',', $value); } public function marshal($value) { return explode(',', $value); } public function toDatabase($value, Driver $driver) { return implode(',', $value); } public function toStatement($value, Driver $driver) { if ($value === null) { return PDO::PARAM_NULL; } return PDO::PARAM_STR; } }

How do I modify this code

to get array back so that my form will automatically select values from mutiple select box.

to get string back whenever I want to show that value as a string.

最满意答案

正如评论中提到的,你最好正确地规范你的模式并使用belongsToMany关联。 数据是(目前)只是“装饰”的事实不是一个正常化的好时机。

这就是说,这种类型看起来OK-ish。 如果仅在检索数据时出现问题,那么我只能猜测该类型实际上未应用,这可能是检索数据的表对象的问题。

但是,您不能使用类型对象在视图层进行决策,到那时类型对象已经完成了他们的工作。 如果你需要视图中的数据有时是一个数组,有时是一个字符串,那么你最好总是以数组格式检索它,并使用一个帮助器将其转换为字符串列表,或者甚至可以使用相应的实体类(但请记住,实体不应该为演示负责)。

也可以看看

食谱>视图>助手>创建助手 食谱>数据库访问和ORM>实体>创建虚拟属性

As mentioned in the comments, you'd better normalize your schema properly and use a belongsToMany association. The fact that the data is (currently) only "decoration" is not a good reason for ditching normalization.

That being said, the type looks OK-ish. If there is a problem only with retrieving the data, then I can only guess that the type is actually not being applied, which probably is a problem with the table object that retrieves the data.

However you cannot use type objects to make decisions at the view layer, the type objects have already done their work by that time. If you need the data in your views to be sometimes an array, and sometimes a string, then you better always retrieve it in array format, and use a helper to convert it to a string list, or maybe even use a virtual property on the respective entity class (but remember that entities shouldn't really be responsible for presentation).

See also

Cookbook > Views > Helpers > Creating Helpers Cookbook > Database Access & ORM > Entities > Creating Virtual Properties

更多推荐

本文发布于:2023-07-22 06:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   无法正常   数据类型   工作   CakePHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!