I use symfony and doctrine as ORM, in available type I have array, simple_array, or json_array I 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和doctrine作为ORM,在可用类型中我有数组、simple_array或json_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 TaskEntity with days attribute
我有一个任务必须在特定的星期内运行,所以我用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 ...
但是我不知道怎么选择女巫类型……
4 个解决方案
#1
89
For your problem simple_array
is the right way, the right way may also create seven boolean fields.
对于问题simple_array的方法是正确的,正确的方法还可以创建七个布尔字段。
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.
了解类型在doctrine中的工作方式的最佳方法是读取类型的代码,这是因为在文档中有一些被认为是理所当然的或没有真正解释的细节。
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
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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来序列化()/unserialize(),比json_array要快。看看代码,我认为它也适用于对象。显然,如果你把这个领域看成是纯文本,那么它是不可理解的。
json_array
in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/JsonArrayType.php
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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无处不在)。
#2
8
Another consideration: The most efficient way to represent a small set of true/false values like the ones presented here would be a bitfield.
另一个需要考虑的问题是:表示一小组真值/假值的最有效方法是位域。
That way you're only storing one integer instead of a full string. And you avoid the encode/decode overhead.
这样你只存储一个整数而不是一个全字符串。并且避免了编码/解码开销。
See https://*.com/a/5412214/827254 for a good example.
请参阅https://*.com/a/5412214/827254,了解一个很好的示例。
#3
1
The best solution for your problem, as stated, would be to use an array mapping of type array
or json_array
but not simple_array
. The reason is that the serialization method of simple_array
is 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.
如前所述,解决问题的最佳方法是使用类型为array或json_array的数组映射,而不是simple_array。原因是simple_array的序列化方法只是对内爆('、'、$array)的调用,它只保留数组的值而不保留数组的键,因此对于有关联数组的情况无效。
However, you could also model your $days
attribute 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的数组(例如周一为0,周二为1,等等)。在这种情况下,它会起作用,因为反序列化是爆炸式的(',',$serialized);生成具有序列化值的基于0的数组。
#4
0
According to the Documentation:
根据文档:
Doctrine ORM > Basic Mapping > Doctrine Mapping Types
基本映射>学说映射类型
You have 3 choices regaring array data:
对于数组数据,您有三个选择:
-
array
Type that maps a SQL CLOB to a PHP array usingserialize()
andunserialize()
.使用serialize()和unserialize()将SQL CLOB映射到PHP数组的数组类型。
-
simple_array
Type that maps a SQL CLOB to a PHP array usingimplode()
andexplode()
, with a comma as delimiter.simple_array类型,将SQL CLOB映射到PHP数组,使用内爆()和防爆(),并使用逗号作为分隔符。
IMPORTANT: Only use this type if you are sure that your values cannot contain a
,
.重要:只有当您确定您的值不能包含a时才使用此类型。
-
json_array
Type that maps a SQL CLOB to a PHP array usingjson_encode()
andjson_decode()
.json_array类型,它使用json_encode()和json_decode()将SQL CLOB映射到一个PHP数组。
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 array
and if you have more complex key-value arrays, use json_array
.
因此,如果您确定数组值中没有(逗号),请使用simple_array。如果您有一个简单的数组结构(线性),使用数组,如果有更复杂的键值数组,请使用json_array。
#1
89
For your problem simple_array
is the right way, the right way may also create seven boolean fields.
对于问题simple_array的方法是正确的,正确的方法还可以创建七个布尔字段。
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.
了解类型在doctrine中的工作方式的最佳方法是读取类型的代码,这是因为在文档中有一些被认为是理所当然的或没有真正解释的细节。
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
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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来序列化()/unserialize(),比json_array要快。看看代码,我认为它也适用于对象。显然,如果你把这个领域看成是纯文本,那么它是不可理解的。
json_array
in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/JsonArrayType.php
在/供应商/理论/ dbal / lib /理论/ dbal /类型/ 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无处不在)。
#2
8
Another consideration: The most efficient way to represent a small set of true/false values like the ones presented here would be a bitfield.
另一个需要考虑的问题是:表示一小组真值/假值的最有效方法是位域。
That way you're only storing one integer instead of a full string. And you avoid the encode/decode overhead.
这样你只存储一个整数而不是一个全字符串。并且避免了编码/解码开销。
See https://*.com/a/5412214/827254 for a good example.
请参阅https://*.com/a/5412214/827254,了解一个很好的示例。
#3
1
The best solution for your problem, as stated, would be to use an array mapping of type array
or json_array
but not simple_array
. The reason is that the serialization method of simple_array
is 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.
如前所述,解决问题的最佳方法是使用类型为array或json_array的数组映射,而不是simple_array。原因是simple_array的序列化方法只是对内爆('、'、$array)的调用,它只保留数组的值而不保留数组的键,因此对于有关联数组的情况无效。
However, you could also model your $days
attribute 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的数组(例如周一为0,周二为1,等等)。在这种情况下,它会起作用,因为反序列化是爆炸式的(',',$serialized);生成具有序列化值的基于0的数组。
#4
0
According to the Documentation:
根据文档:
Doctrine ORM > Basic Mapping > Doctrine Mapping Types
基本映射>学说映射类型
You have 3 choices regaring array data:
对于数组数据,您有三个选择:
-
array
Type that maps a SQL CLOB to a PHP array usingserialize()
andunserialize()
.使用serialize()和unserialize()将SQL CLOB映射到PHP数组的数组类型。
-
simple_array
Type that maps a SQL CLOB to a PHP array usingimplode()
andexplode()
, with a comma as delimiter.simple_array类型,将SQL CLOB映射到PHP数组,使用内爆()和防爆(),并使用逗号作为分隔符。
IMPORTANT: Only use this type if you are sure that your values cannot contain a
,
.重要:只有当您确定您的值不能包含a时才使用此类型。
-
json_array
Type that maps a SQL CLOB to a PHP array usingjson_encode()
andjson_decode()
.json_array类型,它使用json_encode()和json_decode()将SQL CLOB映射到一个PHP数组。
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 array
and if you have more complex key-value arrays, use json_array
.
因此,如果您确定数组值中没有(逗号),请使用simple_array。如果您有一个简单的数组结构(线性),使用数组,如果有更复杂的键值数组,请使用json_array。