Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save array in SQL database using Symfony 2, Doctrine?

i am doing app, where i use SQL and i want to save checkbox values in one column. i am doing it like this:

 /**
* @Assert\NotBlank(
*           message="please select!")
* @Assert\NotNull(
*           message="please select!")
* @Assert\Range(min=0, max=9)
* @ORM\Column(type="integer")
*/  
protected $ingredients;


public static function getIngredientsOptions(){
return array('cheese','tomatoes','salami','onions','mushroom','bacon','ham','vegetables','peppers','olives');

    }

but i get error which says that i have SELECT error, i think problem is with checkbox. is this correct? can you help me how to this ?

like image 872
Giga Avatar asked Dec 06 '15 23:12

Giga


People also ask

Can I save an array in DB?

Use the PHP function serialize() to convert arrays to strings. These strings can easily be stored in MySQL database. Using unserialize() they can be converted to arrays again if needed.

Does Symfony use Doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

What is schema Symfony?

In order to create the data object model that symfony will use, you need to translate whatever relational model your database has to an object data model. The ORM needs a description of the relational model to do the mapping, and this is called a schema.


1 Answers

You can change the column type to "array" like this:

@ORM\Column(name="ingredients", type="array", nullable=true)

This will result in a longtext field with comment "(DC2Type:array)" so Doctrine knows how to handle it. It will store a serialized array.

This might be what you want. If not, please post some more code of your setter and controller where the form is used, as well as the error message.

like image 122
sekl Avatar answered Sep 22 '22 00:09

sekl