php Doctrine array vs simple_array vs json_array

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16587769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:23:48  来源:igfitidea点击:

Doctrine array vs simple_array vs json_array

phparrayssymfonydoctrine-orm

提问by 0x1gene

I use symfony and doctrine as ORM, in available type I have array, simple_array, or json_arrayI wonder what is the difference beetween each of them ? And when to use one or the other ? Do you have a show case for each of them to illustrate the differences ?

我使用 symfony 和学说作为 ORM,在可用类型中我有arraysimple_arrayjson_array我想知道它们之间有什么区别?什么时候使用一个或另一个?你有每个人的展示柜来说明差异吗?

I already used simple_array in some applications but I find it not really well rendered in formType... (Or maybe I'm not using it well!? )

我已经在一些应用程序中使用了 simple_array,但我发现它在 formType 中渲染得不是很好......(或者我可能没有很好地使用它!?)

To illustrate my question, here is a show case:

为了说明我的问题,这里有一个展示案例:

I have an Task that I have to run on specific week's days So I created TaskEntitywith daysattribute

我有一个任务,我必须在特定的星期几运行所以我创建带有days属性的TaskEntity

Days would be like that:

日子应该是这样的:

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

But I have no idea witch type to choose ...

但我不知道要选择女巫类型......

回答by Marino Di Clemente

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

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

However here's a little vademecum:

然而,这里有一点 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

simple_array

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

/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.

它只是一个implode()/explode()项目,只存储值,它很有用,因为您可以轻松查询数据库。

array

大批

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

/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.

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

json_array

json_array

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

/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).

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

June 2018 update

2018 年 6 月更新

  • 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
  • 现在有一个更加完整和更新的文档资料在这里
  • json_array 已被弃用,以支持 json 类型,它将利用 json 字段的新数据库功能

回答by Jason Hanley

Another consideration: The most efficient way to represent a small set of true/false values like the ones presented here would be a bitfield.

另一个考虑因素:表示一小组真/假值的最有效方法(如此处提供的值)是bitfield

That way you're only storing one integer instead of a full string. And you avoid the encode/decode overhead.

这样你只存储一个整数而不是一个完整的字符串。并且您避免了编码/解码开销。

See https://stackoverflow.com/a/5412214/827254for a good example.

有关一个很好的例子,请参见https://stackoverflow.com/a/5412214/827254

回答by Luis

The best solution for your problem, as stated, would be to use an array mapping of type arrayor json_arraybut notsimple_array. The reason is that the serialization method of simple_arrayis just a call to implode(',', $array)and that would retain only the values and not the keys of the array, thus invalid for your situation where you have an associative array.

如上所述,针对您的问题的最佳解决方案是使用类型为arrayorjson_array不是的数组映射simple_array。原因是 的序列化方法simple_array只是调用implode(',', $array)并且只会保留数组的值而不是键,因此对于您拥有关联数组的情况无效。

However, you could also model your $daysattribute as a 0-based array (i.e. monday would be zero, tuesday would be 1, etc.). In that case, it would work because the deserializing with explode(',', $serialized);generates a 0-based array with the serialised values.

但是,您也可以将$days属性建模为基于 0 的数组(即,星期一为零,星期二为 1,等等)。在这种情况下,它会起作用,因为反序列化会explode(',', $serialized);生成一个带有序列化值的基于 0 的数组。

回答by Pmpr

According to the Documentation:

根据文档:

Doctrine ORM> Basic Mapping> Doctrine Mapping Types

Doctrine ORM>基本映射> Doctrine 映射类型

You have 3 choices regaring array data:

关于数组数据,您有 3 种选择:

  1. arrayType that maps a SQL CLOB to a PHP array using serialize()and unserialize().

  2. simple_arrayType that maps a SQL CLOB to a PHP array using implode()and explode(), with a comma as delimiter.

    IMPORTANT:Only use this type if you are sure that your values cannot contain a ,.

  3. json_arrayType that maps a SQL CLOB to a PHP array using json_encode()and json_decode().

  1. array使用serialize()和将 SQL CLOB 映射到 PHP 数组的类型unserialize()

  2. simple_array使用implode()and将 SQL CLOB 映射到 PHP 数组的类型explode()以逗号作为分隔符

    重要提示:仅当您确定您的值不能包含,.

  3. json_array使用json_encode()和将 SQL CLOB 映射到 PHP 数组的类型json_decode()

So, if you are sure about not having ,(comma) in your array values, use simple_array. If you have a simple array structure (linear), use arrayand if you have more complex key-value arrays, use json_array.

因此,如果您确定,数组值中没有(逗号),请使用simple_array. 如果您有一个简单的数组结构(线性),请使用array,如果您有更复杂的键值数组,请使用json_array.