0

I am trying to work out a good way of storing an array of objects in my sym2 entity. The objects in the array would look like this:

{
    "id"        :   1,
    "top"       :   200,
    "left"      :   150,
    "width"     :   500,
    "height"    :   600
}

Should I just go for the array property like this?

/**
 * @var array $modules
 * 
 * @ORM\Column(name="modules", type="array", nullable=true)
 */
private $modules;
/*
{
    "id"        :   1,
    "left"      :   150,
    "top"       :   200,
    "width"     :   500,
    "height"    :   600
}
*/

Or is there a smoother way, could I create the objects contained in this array as a separate entity and store instead an array of those entities here in this entity?

I do not want to save these to database separately, I would like to keep them inside this main entity. I get that I could set up a many to many relationship but I don't want to, it is a bit overkill for what I am trying to accomplish.

----- UPDATE ------- Thanks to Guillaume Verbal, here's what I will do, I assume this will work fine as well then since JSON can take nested objects infinitely?

    $person[0] = new Acme\Person();
$person->setName('foo');
$person->setAge(99);

$person[1] = new Acme\Person();
$person->setName('foo');
$person->setAge(99);

$jsonContent = $serializer->serialize($person, 'json');

// $jsonContent contains {"name":"foo","age":99}
6
  • 1
    Why not to store the array as JSON string? Commented Nov 12, 2013 at 12:41
  • Oh I only used json above to describe the object, it will most likely come in the form of a php array when I want to persist the entity. Commented Nov 12, 2013 at 13:19
  • Yes, but you can use json_encode() on PHP array as well. Commented Nov 12, 2013 at 13:24
  • ok. Is there an advantage in using this instead of just persisting it with a normal PHP array for the contents? Commented Nov 12, 2013 at 13:35
  • JSON has better better compatibility between languages. It is considered as best-practice more than serialize() method used for PHP array. But do as you wish. Commented Nov 12, 2013 at 13:44

2 Answers 2

4

You can use the Symfony 2 Serializer Component: http://symfony.com/doc/current/components/serializer.html

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());

$serializer = new Serializer($normalizers, $encoders);

$person = new Acme\Person();
$person->setName('foo');
$person->setAge(99);

$jsonContent = $serializer->serialize($person, 'json');

// $jsonContent contains {"name":"foo","age":99}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I see.. would this work as well? $person[0] = new Acme\Person(); $person->setName('foo'); $person->setAge(99); $person[1] = new Acme\Person(); $person->setName('foo'); $person->setAge(99); $jsonContent = $serializer->serialize($person, 'json'); // $jsonContent contains {"name":"foo","age":99}
formatting is a bit messy in the comment field ... I added the same to original question above
1

You can use JSON type for this

http://symfony.com/doc/current/components/serializer.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.