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 ?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With