教义数组vs simple_array vs json_array [英] Doctrine array vs simple_array vs json_array

查看:81
本文介绍了教义数组vs simple_array vs json_array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony和学说作为我的ORM.

对于可用类型,我有:

  • 数组
  • simple_array
  • json_array

我想知道它们之间的区别是什么:何时使用一个或另一个?

我可以为他们每个人演示一下吗?

我已经在某些应用程序中使用过simple_array,但是我发现我不理解formType ...(或者也许我没有很好地使用它!?)

为说明我的问题,下面是一个示例:

我有一个必须在特定日期运行的任务 因此,我创建了具有 days 属性

TaskEntity

天将是:

$days = array(
    1=>true,
    2=>true,
    3=>true,
    4=>true,
    5=>true,
    6=>false,
    7=>false
);

但是我不知道要选择以上哪种类型...

解决方案

对于您的问题,simple_array是正确的方法,正确的方法可能还会创建七个布尔字段.

但是这里有一些小东西:

了解类型在理论上的工作方式的最佳方法是读取类型的代码,这是因为有一些细节是理所当然的,或者在文档中并未真正解释.

因此您可以进入

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Type.php

找到您的类型,并检查其方法是否可以根据需要工作.

以下是一些详细信息:

simple_array

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/SimpleArrayType.php

return implode(',', $value);

它只是项目的implode()/explode(),仅存储值,它非常有用,因为您可以轻松查询数据库.

数组

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ArrayType.php

return serialize($value);

将PHP调用到serialize()/unserialize(),它比json_array快.查看代码,我认为它也适用于对象.显然,如果您将该字段视为纯文本,则无法理解.

json_array

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/JsonArrayType.php

return json_encode($value);

它调用json_encode()/json_decode(),如果您在该字段中查看,您会看到一个未格式化的JSON数组,但它比PHP的序列化对象更具可读性,并且更易于移植(JSON随处可见).

2018年6月更新

  • 现在有完整且更新的文档这里
  • 不建议使用json_array,而建议使用json类型,它将利用json字段的新数据库功能

I am using symfony and doctrine as my ORM.

For available types I have:

  • array
  • simple_array
  • json_array

I am wondering what the difference is between each of them: when do I use one or the other?

Can I have a demonstration for each of them to illustrate the differences?

I already use simple_array in some applications but I find I don't understand formType... (Or maybe I'm not using it well!? )

To illustrate my question, here is an example:

I have an Task that I have to run on specific days So I created TaskEntity with days attribute

Days would be:

$days = array(
    1=>true,
    2=>true,
    3=>true,
    4=>true,
    5=>true,
    6=>false,
    7=>false
);

But I have no idea which of the above types to choose ...

解决方案

For your problem simple_array is the right way, the right way may also create seven boolean fields.

However here's a little vademecum:

The best way to see how a type works in doctrine is to read the code of the type, this is because there are several details that are taken for granted or are not really explained in the documentation.

So you can go into

/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Type.php

find your type and check if its methods work as you want.

Here some details:

simple_array

in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/SimpleArrayType.php

return implode(',', $value);

it's just a implode()/explode() of items, stores only the values and it's useful because you can easily query the database.

array

in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ArrayType.php

return serialize($value);

calls PHP to serialize()/unserialize(), it's faster than json_array. Looking at the code I think it also works with objects. Obviously if you see the field as plain text it's uncomprensible.

json_array

in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/JsonArrayType.php

return json_encode($value);

it calls json_encode()/json_decode(), if you look in the field you can see a unformatted JSON array but it's more readable than PHP's serialized object and is really more portable (JSON exists everywhere).

June 2018 update

  • now there is a complete and more updated documentation here
  • json_array is deprecated in favor of json type, it will leverage on new database features for json fields

这篇关于教义数组vs simple_array vs json_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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