-
Notifications
You must be signed in to change notification settings - Fork 2.5k
document how to work with generated columns #11834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Generated Columns | ||
================= | ||
|
||
Generated columns, sometimes also called virtual columns, are populated by | ||
the database engine itself. They are a tool for performance optimization, to | ||
avoid calculating a value on each query. | ||
|
||
You can define generated columns on entities and have Doctrine map the values | ||
to your entity. | ||
|
||
Declaring a generated column | ||
---------------------------- | ||
|
||
There is no explicit mapping instruction for generated columns. Instead, you | ||
specify that the column should not be written to, and define a custom column | ||
definition. | ||
|
||
.. code-block:: php | ||
.. literalinclude:: generated-columns/Person.php | ||
|
||
* ``insertable``, ``updatable``: Setting these to false tells Doctrine to never | ||
write this column - writing to a generated column would result in an error | ||
from the database. | ||
* ``columnDefinition``: We specify the full DDL to create the column. To allow | ||
to use database specific features, this attribute does not use Doctrine Query | ||
Language but native SQL. Note that you need to reference columns by their | ||
database name (either explicitly set in the mapping or per the current | ||
:doc:`naming strategy <../reference/namingstrategy>`). | ||
Be aware that specifying a column definition makes the ``SchemaTool`` | ||
completely ignore all other configuration for this column. See also | ||
:ref:`#[Column] <attrref_column>` | ||
* ``generated``: Specifying that this column is always generated tells Doctrine | ||
to update the field on the entity with the value from the database after | ||
every write operation. | ||
|
||
Advanced example: Extracting a value from a JSON structure | ||
---------------------------------------------------------- | ||
|
||
Lets assume we have an entity that stores a blogpost as structured JSON. | ||
To avoid extracting all titles on the fly when listing the posts, we create a | ||
generated column with the field. | ||
|
||
.. code-block:: php | ||
.. literalinclude:: generated-columns/Article.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Article | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
private int $id; | ||
|
||
/** | ||
* When working with Postgres, it is recommended to use the jsonb | ||
* format for better performance. | ||
*/ | ||
#[ORM\Column(options: ['jsonb' => true])] | ||
private array $content; | ||
|
||
/** | ||
* Because we specify NOT NULL, inserting will fail if the content does | ||
* not have a string in the title field. | ||
*/ | ||
#[ORM\Column( | ||
insertable: false, | ||
updatable: false, | ||
columnDefinition: "VARCHAR(255) generated always as (content->>'title') stored NOT NULL", | ||
generated: 'ALWAYS', | ||
)] | ||
private string $title; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Person | ||
{ | ||
#[ORM\Column(type: 'string')] | ||
private string $firstName; | ||
|
||
#[ORM\Column(type: 'string', name: 'name')] | ||
private string $lastName; | ||
|
||
#[ORM\Column( | ||
type: 'string', | ||
insertable: false, | ||
updatable: false, | ||
columnDefinition: "VARCHAR(255) GENERATED ALWAYS AS (concat(firstName, ' ', name) stored NOT NULL", | ||
generated: 'ALWAYS', | ||
)] | ||
private string $fullName; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.