I work on a project that is currently usingSymfony 6.1.*
and
"doctrine/annotations": "^1.0",
"doctrine/common": "^3.1",
"doctrine/doctrine-bundle": "^2.1",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.10",
I recently migrated from annotations to attributes, so I am now usingDoctrine\DBAL\Types\Types
to define types of my columns and I saw thatTypes::ARRAY
has a deprecation-notice with the hint to useTypes::JSON
instead.
So for some new properties being added to an already existing entity I wanted to do exactly that.
The column-definition and get/set in my entity (Work
) look like this:
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $controlledKeywords = [];
public function getControlledKeywords(): ?array
{
return $this->controlledKeywords;
}
public function setControlledKeywords(?array $controlledKeywords): self
{
$this->controlledKeywords = $controlledKeywords;
return $this;
}
Each element of this array is of type:
#[ORM\Embeddable]
class ControlledKeywordEmbeddable
{
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $controlledKeyword;
#[ORM\Column(type: 'boolean', nullable: false)]
private ?bool $isWeighted;
public function getControlledKeyword(): ?string
{
return $this->controlledKeyword;
}
public function setControlledKeyword(?string $controlledKeyword): self
{
$this->controlledKeyword = $controlledKeyword;
return $this;
}
public function getIsWeighted(): ?bool
{
return $this->isWeighted;
}
public function setIsWeighted(?bool $isWeighted): void
{
$this->isWeighted = $isWeighted;
}
}
But when I add some instances of my embeddable to this array, it is only being persisted like that:
{"1":{},"2":{}}
What I did so far was checking on my model-/norm-/viewData in my Symfony-Controller that handles this request, because I thought I might have made a mistake with a DataMapper, but the output looked absolutely fine.
So next I looked into the hydratedWork
-instance after the submit and validity-check to verify my impression, which also looked good.
Meaning:$controlledKeywords
is in fact an array ofControlledKeywordEmbeddable
-instances with values as specified through my form.
Now I don't know what to look out for anymore because the next relevant thing happening in my code is already the call to
$entityManager->persist($work);
$entityManager->flush();
UsingTypes::ARRAY
, the otherwise identical code, works perfectly fine btw. but I would like to get rid of deprecations rather earlier than later.
Hence: what am I missing?
Found the answer to my question:
doctrine seems to use PHPs native json_encode under the hood to encode the data.
But this omitts private properties, so the solution would either be
to make the properties in the embeddable public (wouldn't prefer that)
or to implement\JsonSerializable
in the embeddable.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.