My object array is not deserializing when loading the entity from the database.
I have a non-entity object named NetworkAddress. Then in my Foo entity, I have an array of these network addresses.
The network addresses are being saved as serialized PHP in the database, but when I load the object, the data for the network addresses aren't being added.
/**
* @ORM\Entity(repositoryClass="App\Repository\FooRepository")
*/
class Foo implements \Serializable
{
...
/**
* @var NetworkAddress[]
* @Groups({"main", "initialized"})
* @ORM\Column(type="array")
*/
private array $addresses;
...
So when I load my $foo object, $foo->getAddresses() returns an array of empty NetworkAddresses, even though the raw database values is this:
a:1:{i:0;C:22:"App\DTO\NetworkAddress":41:{a:2:{i:0;s:9:"127.0.0.1";i:1;s:4:"5000";}}}
I'm using a basic ->findOneBy() command on my repo to retrieve the object.
Serializing seems to work, but is there something special for deserializing an object stored as an array?
Here's my NetworkAddress class, w/o the getters/setters
class NetworkAddress implements \Serializable
{
/**
* @Groups({"main", "initialized"})
* @Assert\Regex(
* pattern="/^[0-9A-z\.-]+$/",
* match=true,
* message="Invalid host format."
* )
*/
private string $host = 'localhost';
/**
* Intentionally left untyped so we can accept int or string for port number.
*
* @Groups({"main", "initialized"})
* @Assert\NotBlank(message = "Port should not be empty.")
* @Assert\Range(min="1", max="65535", invalidMessage="Invalid port.")
*/
private $port;
arraycolumn types by Doctrine. For PHP, it's more powerful because it can reconstruct theNetworkAddressobject from that without any additional logic.