Skip to content
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

Merge release 2.12.0 into 3.0.x #9667

Merged
merged 5 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ Use `toIterable()` instead.

# Upgrade to 2.12

## Deprecated the `doctrine` binary.

The documentation explains how the console tools can be bootstrapped for
standalone usage.

The method `ConsoleRunner::printCliConfigTemplate()` is deprecated because it
was only useful in the context of the `doctrine` binary.

## Deprecate omitting `$class` argument to `ORMInvalidArgumentException::invalidIdentifierBindingEntity()`

To make it easier to identify understand the cause for that exception, it is
Expand All @@ -325,6 +333,7 @@ multiple entity managers had been deprecated with 2.9 already. This leaves
The `EntityManagerHelper` class with no purpose which is why it is now
deprecated too. Applications that still rely on the `em` console helper, can
easily recreate that class in their own codebase.

## Deprecate custom repository classes that don't extend `EntityRepository`

Although undocumented, it is currently possible to configure a custom repository
Expand Down
10 changes: 10 additions & 0 deletions bin/doctrine-pear.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

fwrite(
STDERR,
'[Warning] The use of this script is discouraged. See'
. ' https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/tools.html#doctrine-console'
. ' for instructions on bootstrapping the console runner.'
. PHP_EOL
);

echo PHP_EOL . PHP_EOL;

require_once 'Doctrine/Common/ClassLoader.php';

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
Expand Down
10 changes: 10 additions & 0 deletions bin/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\ORM\Tools\Console\ConsoleRunner;

fwrite(
STDERR,
'[Warning] The use of this script is discouraged. See'
. ' https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/tools.html#doctrine-console'
. ' for instructions on bootstrapping the console runner.'
. PHP_EOL
);

echo PHP_EOL . PHP_EOL;

$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
Expand Down
16 changes: 2 additions & 14 deletions docs/en/reference/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,5 @@ Setting up the Console
----------------------

Doctrine uses the Symfony Console component for generating the command
line interface. You can take a look at the ``vendor/bin/doctrine.php``
script and the ``Doctrine\ORM\Tools\Console\ConsoleRunner`` command
for inspiration how to setup the cli.

In general the required code looks like this:

.. code-block:: php

<?php
$cli = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli);
$cli->run();
line interface. You can take a look at the
:doc:`tools chapter <../reference/tools>` for inspiration how to setup the cli.
26 changes: 3 additions & 23 deletions docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,6 @@ Setting up the Commandline Tool
-------------------------------

Doctrine ships with a number of command line tools that are very helpful
during development. You can call this command from the Composer binary
directory:

.. code-block:: sh

$ php vendor/bin/doctrine

You need to register your applications EntityManager to the console tool
to make use of the tasks by creating a ``cli-config.php`` file with the
following content:

.. code-block:: php

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
require_once 'bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);
during development. In order to make use of them, create an executable PHP
script in your project as described in the
:doc:`tools chapter <../reference/tools>`.
69 changes: 36 additions & 33 deletions docs/en/reference/tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ Doctrine Console
The Doctrine Console is a Command Line Interface tool for simplifying common
administration tasks during the development of a project that uses ORM.

Take a look at the :doc:`Installation and Configuration <configuration>`
chapter for more information how to setup the console command.
For the following examples, we will set up the CLI as ``bin/doctrine``.

Display Help Information
~~~~~~~~~~~~~~~~~~~~~~~~

Type ``php vendor/bin/doctrine`` on the command line and you should see an
overview of the available commands or use the --help flag to get
information on the available commands. If you want to know more
about the use of generate entities for example, you can call:

.. code-block:: php

$> php vendor/bin/doctrine orm:generate-entities --help


Configuration
~~~~~~~~~~~~~
Setting Up the Console
~~~~~~~~~~~~~~~~~~~~~~

Whenever the ``doctrine`` command line tool is invoked, it can
access all Commands that were registered by a developer. There is no
Expand All @@ -34,32 +20,52 @@ Doctrine DBAL and ORM. If you want to use additional commands you
have to register them yourself.

All the commands of the Doctrine Console require access to the
``EntityManager``. You have to inject it into the console application with
``ConsoleRunner::createHelperSet``. Whenever you invoke the Doctrine
binary, it searches the current directory for the file ``cli-config.php``.
This file contains the project-specific configuration.
``EntityManager``. You have to inject it into the console application.

Here is an example of a the project-specific ``cli-config.php``:
Here is an example of a the project-specific ``bin/doctrine`` binary.

.. code-block:: php

#!/usr/bin/env php
<?php

use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

// replace this with the path to your own project bootstrap file.
// replace with path to your own project bootstrap file
require_once 'bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);
$commands = [
// If you want to add your own custom console commands,
// you can do so here.
];

ConsoleRunner::run(
new SingleManagerProvider($entityManager),
$commands
);

.. note::

You have to adjust this snippet for your specific application or framework
and use their facilities to access the Doctrine EntityManager and
Connection Resources.

Display Help Information
~~~~~~~~~~~~~~~~~~~~~~~~

Type ``php bin/doctrine`` on the command line and you should see an
overview of the available commands or use the ``--help`` flag to get
information on the available commands. If you want to know more
about the use of generate entities for example, you can call:

::

$> php bin/doctrine orm:generate-entities --help

Command Overview
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -175,38 +181,35 @@ To create the schema use the ``create`` command:

.. code-block:: php

$ php doctrine orm:schema-tool:create
$ php bin/doctrine orm:schema-tool:create

To drop the schema use the ``drop`` command:

.. code-block:: php

$ php doctrine orm:schema-tool:drop
$ php bin/doctrine orm:schema-tool:drop

If you want to drop and then recreate the schema then use both
options:

.. code-block:: php

$ php doctrine orm:schema-tool:drop
$ php doctrine orm:schema-tool:create
$ php bin/doctrine orm:schema-tool:drop
$ php bin/doctrine orm:schema-tool:create

As you would think, if you want to update your schema use the
``update`` command:

.. code-block:: php

$ php doctrine orm:schema-tool:update
$ php bin/doctrine orm:schema-tool:update

All of the above commands also accept a ``--dump-sql`` option that
will output the SQL for the ran operation.

.. code-block:: php

$ php doctrine orm:schema-tool:create --dump-sql

Before using the orm:schema-tool commands, remember to configure
your cli-config.php properly.
$ php bin/doctrine orm:schema-tool:create --dump-sql

Runtime vs Development Mapping Validation
-----------------------------------------
Expand Down
37 changes: 25 additions & 12 deletions docs/en/tutorials/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,35 @@ Generating the Database Schema

Doctrine has a command-line interface that allows you to access the SchemaTool,
a component that can generate a relational database schema based entirely on the
defined entity classes and their metadata. For this tool to work, a
``cli-config.php`` file must exist in the project root directory:
defined entity classes and their metadata. For this tool to work, you need to
create an executable console script as described in the
:doc:`tools chapter <../reference/tools>`.

If you created the ``bootstrap.php`` file as described in the previous section,
that script could look like this:

.. code-block:: php

#!/usr/bin/env php
<?php
// cli-config.php
require_once "bootstrap.php";
// bin/doctrine

use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
// Adjust this path to your actual bootstrap.php
require __DIR__ . 'path/to/your/bootstrap.php';

ConsoleRunner::run(
new SingleManagerProvider($entityManager)
);

Now call the Doctrine command-line tool:
In the following examples, we will assume that this script has been created as
``bin/doctrine``.

::

$ vendor/bin/doctrine orm:schema-tool:create
$ php bin/doctrine orm:schema-tool:create

Since we haven't added any entity metadata in ``src`` yet, you'll see a message
stating "No Metadata Classes to process." In the next section, we'll create a
Expand All @@ -208,14 +221,14 @@ You can easily recreate the database using the following commands:

::

$ vendor/bin/doctrine orm:schema-tool:drop --force
$ vendor/bin/doctrine orm:schema-tool:create
$ php bin/doctrine orm:schema-tool:drop --force
$ php bin/doctrine orm:schema-tool:create

Or you can use the update functionality:

::

$ vendor/bin/doctrine orm:schema-tool:update --force
$ php bin/doctrine orm:schema-tool:update --force

The updating of databases uses a diff algorithm for a given
database schema. This is a cornerstone of the ``Doctrine\DBAL`` package,
Expand Down Expand Up @@ -542,7 +555,7 @@ let's update the database schema:

::

$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql
$ php bin/doctrine orm:schema-tool:update --force --dump-sql

Specifying both flags ``--force`` and ``--dump-sql`` will cause the DDL
statements to be executed and then printed to the screen.
Expand Down Expand Up @@ -1179,7 +1192,7 @@ class that holds the owning sides.
Update your database schema by running:
::

$ vendor/bin/doctrine orm:schema-tool:update --force
$ php bin/doctrine orm:schema-tool:update --force


Implementing more Requirements
Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM\Internal\Hydration;

use BackedEnum;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Types\Type;
Expand All @@ -22,6 +23,7 @@
use function count;
use function end;
use function in_array;
use function is_array;

/**
* Base class for all hydrators. A hydrator is a class that provides some form
Expand Down Expand Up @@ -285,7 +287,20 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
$type = $cacheKeyInfo['type'];
$value = $type->convertToPHPValue($value, $this->_platform);

// Reimplement ReflectionEnumProperty code
if ($value !== null && isset($cacheKeyInfo['enumType'])) {
$enumType = $cacheKeyInfo['enumType'];
if (is_array($value)) {
$value = array_map(static function ($value) use ($enumType): BackedEnum {
return $enumType::from($value);
}, $value);
} else {
$value = $enumType::from($value);
}
}

$rowData['scalars'][$fieldName] = $value;

break;

//case (isset($cacheKeyInfo['isMetaColumn'])):
Expand Down Expand Up @@ -428,13 +443,15 @@ protected function hydrateColumnInfo(string $key): ?array
'fieldName' => $this->_rsm->scalarMappings[$key],
'type' => Type::getType($this->_rsm->typeMappings[$key]),
'dqlAlias' => '',
'enumType' => $this->_rsm->enumMappings[$key] ?? null,
];

case isset($this->_rsm->scalarMappings[$key]):
return $this->_cache[$key] = [
'isScalar' => true,
'fieldName' => $this->_rsm->scalarMappings[$key],
'type' => Type::getType($this->_rsm->typeMappings[$key]),
'enumType' => $this->_rsm->enumMappings[$key] ?? null,
];

case isset($this->_rsm->metaMappings[$key]):
Expand Down
9 changes: 3 additions & 6 deletions lib/Doctrine/ORM/Internal/Hydration/ScalarColumnHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Driver\Exception;
use Doctrine\ORM\Exception\MultipleSelectorsFoundException;

use function array_column;
use function count;

/**
Expand All @@ -26,12 +27,8 @@ protected function hydrateAllData(): array
throw MultipleSelectorsFoundException::create($this->resultSetMapping()->fieldMappings);
}

$result = [];
$result = $this->statement()->fetchAllNumeric();

while ($row = $this->statement()->fetchOne()) {
$result[] = $row;
}

return $result;
return array_column($result, 0);
}
}
Loading