Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store array in database #255

Closed
alborq opened this issue Mar 19, 2017 · 10 comments
Closed

Store array in database #255

alborq opened this issue Mar 19, 2017 · 10 comments
Labels

Comments

@alborq
Copy link

alborq commented Mar 19, 2017

Hi,

i try to define this ressource:

{
	"user": "/users/1",
	"name": "Test",
	"definition": {
		"enable": {
			"type": "Boolean",
			"nullable": true,
			"default": true
		},
		"locked": {
			"type": "Boolean",
			"default": false
		},
		"username": {
			"type": "Text",
			"length": 50,
			"unique": true
		}
	}
}

But, I want store Definiton has json/array in DB
If i try some of :

     * @ORM\Column(name="definition", type="array")
     * @ORM\Column(name="definition", type="json_array")

api platform espected array of sting :

{
  "id": 0,
  "user": "string",
  "name": "string",
  "definition": [
    "string"
  ]
}

Why, where it come from ?
How did it, change it ?

@cve
Copy link
Contributor

cve commented Mar 19, 2017

@alborq I suggest to use

* @ORM\Column(name="definition", type="json")

http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#json

@alborq
Copy link
Author

alborq commented Mar 19, 2017

@cve JSON type not available on MySql

@amenophis
Copy link
Contributor

hi @alborq ,
You could use https://github.com/dunglas/doctrine-json-odm made by @dunglas :)

@alborq
Copy link
Author

alborq commented Mar 22, 2017

hi @amenophis
like response to @cve, Json ODM is not available actually on Mysql

Currently only PostgreSQL is supported. MySQL (>= 5.7.8) will be supported when the Pull Request doctrine/dbal#2266 will be merged.

@alborq alborq closed this as completed Mar 22, 2017
@alborq alborq reopened this Mar 22, 2017
@teohhanhui
Copy link
Contributor

Doctrine json type is only available in master (v2.6; unreleased). But it's actually the same as json_array. There should be nothing stopping you from using the json_array type. If (I'm not sure about this) API Platform expects a flat array of strings, that's a wrong expectation on our end.

@alborq
Copy link
Author

alborq commented Mar 31, 2017

Ping @dunglas. We speak together on SymfonyLive about that. This may as a same bug on symfony components (duplicate code). Why array of string is excpect and not just an array. Allan

@dunglas
Copy link
Member

dunglas commented Apr 3, 2017

It's actually intended for simple_array (it's an array of strings: a CSV) but it should work for json_array according to https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php#L126-L127

Can you post the code of your entities (or publish a sample repository)?

@alborq
Copy link
Author

alborq commented Apr 3, 2017

I have try to reproduce my bug, it seen to be a mistake of myself. All work with json_array,

documentation just say 'string[]' change '@var' to "@var bool[]" let the same, but, this not concerne my initial problem.

this issue is solved.
Thanks.

@alborq alborq closed this as completed Apr 3, 2017
@codedmonkey
Copy link

@teohhanhui Yes this is unfortunately the case. When using the json type the model shows as [string] while it should be a key/value storage. Correct?

@teohhanhui
Copy link
Contributor

I think we should support a flat array of any homogeneous scalar type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants