I am trying to add few Symfony components in a legacy project in order to leverage it (I would rather avoid using the full Symfony framework). That works very well with http-foundation and routing. Now I am trying to add Doctrine and I would like to create entities thanks to the Symfony console component.
I have installed the following packages:
"require": {
"symfony/error-handler": "^6.2",
"symfony/http-foundation": "^6.2",
"symfony/http-kernel": "^6.2",
"symfony/routing": "^6.2",
"symfony/console": "^6.2",
"symfony/security-bundle": "^6.2",
"symfony/orm-pack": "^2.2",
"doctrine/orm": "^2.13",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2"
},
"require-dev": {
"symfony/maker-bundle": "^1.48"
}
And wrote the console file
#!/usr/bin/env php
<?php
// bin/console
require __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->run();
But I get this error message when creating an Entity:
$php bin/console make:entity Product
There are no commands defined in the "make" namespace.
Here is the list of command I get with console list
php bin/console list
Console Tool
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
completion Dump the shell completion script
help Display help for a command
list List commands
How could I register the commands from Doctrine into Symfony console?
Thanks!
Related
I'm trying to generate and execute migrations using doctrine/doctrine-migrations-bundle.
Project specs:
Project deployed in docker. Mapping configured successfully.
Framework: symfony.
Kernel::getProjectDir() overrided successfully.
Project dependencies (not all, only important for this topic) from composer.json:
"doctrine/doctrine-bundle": "^2.1",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.7",
"symfony/framework-bundle": "5.1.*",
My project has migrations for two databases. There are 2 config files for this. An example of one of them for understanding (absolute path in container /opt/app/config/packages/migrations/some_config.yaml):
custom_template: '%kernel.project_dir%/config/migration_template.txt' # <- this not works
# custom_template: '/opt/app/config/migration_template.txt' # <- this works
em: some_em
transactional: false
migrations_paths:
'DoctrineMigrations': '%kernel.project_dir%/src/DoctrineMigrations' # <- this not works
#'DoctrineMigrations': '/opt/app/src/DoctrineMigrations' # <- this works
Problem:
I'm trying to generate migration, using following command:
bin/console doctrine:migrations:generate --configuration='/opt/app/config/packages/migrations/some_config.yaml'
An exception is thrown with an message:
The specified template "%kernel.project_dir%/config/migration_template.txt" cannot be found or is not readable
For absolute path everything works fine.
I have tried to debug doctrine-migrations package code and found, that path %kernel.project_dir%/config/migration_template.txt does not turn into /opt/app/config/migration_template.txt
I think the problem is that the command is implemented in the doctrine/migrations lib and is unaware of Symfony, and therefore doesn't know about container parameters. The Doctrine migrations docs have an example for such a path, so you might try this:
'./src/DoctrineMigrations'
(didn't try myself though).
The relevant part in the docs is here: https://www.doctrine-project.org/projects/doctrine-migrations/en/3.6/reference/configuration.html#configuration
I have a deployment script that executes the migrations this way:
php bin/console doctrine:migrations:diff --allow-empty-diff --env=prod
php bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration --env=prod
And I am getting this error:
[ERROR] No changes detected in your mapping information.
Previously, before upgrading to migrations 3, the error was not being thrown. Once I upgraded it the error started to appear.
"doctrine/doctrine-bundle": "2.1.*",
"doctrine/doctrine-migrations-bundle": "3.1.*", // previously "2.2.*"
"doctrine/migrations": "3.2.*", // previously "2.2.*"
At first I thought that the --allow-empty-diff was removed in the new version, but when I run:
php bin/console doctrine:migrations:diff --help
I do see
--allow-empty-diff Do not throw an exception when no changes are detected.
Any ideas?
It seems the functionality stills works the same as before. If no changes are detected, a message is printed to the console.
However, the difference between 2.2.* and 3.6.* is that it the latter uses StyleInterface to style the message as an error instead of using the regular OutputInterface.
In both cases, a exception is not thrown.
See the code for 2.2.*
See the code for 3.6.*
Just give a try updating your schema
php bin/console orm:schema-tool:update --force
Attempted to load class "Curl" from namespace "Buzz\Client". Did you
forget a "use" statement for another namespace? 500 Internal Server
Error - ClassNotFoundException
in app/cache/dev/appDevDebugProjectContainer.php at line 1809 -
protected function getHwiOauth_HttpClientService()
{
$this->services['hwi_oauth.http_client'] = $instance = new \Buzz\Client\Curl();
$instance->setVerifyPeer(false);
$instance->setTimeout(10);
When you asking a question please specify your operating system and versions of frameworks you are using (e.g. symfony 2). Here is an answer for windows and symfony2
1) Delete manually added folders
2) If you don't have composer installed, install it
For windows
For other read this
2) Install HWIOAuthBundle
HWIOAuthBundle readme.md
option i) Using composer
Open the symfony project directory where composer.json resides.
Open command prompt (In Windows: Shift key + Right click => open command window here)
Run this
composer require hwi/oauth-bundle
OR
option ii) Add dependencies to your composer.json (not recomended)
"require": {
// other dependencies will be here //
"hwi/oauth-bundle": "^0.4.0",
"friendsofsymfony/user-bundle": "^1.3"
}
then execute following command
composer install
i try to deploy an app in "integration" environment but after the "composer update" command, the parameters.yml file generated doesn't contains may integration parameters
Here is what i did :
I created a "config_integ.yml" file in app/config with :
# app/config/config_integ.yml
imports:
- { resource: parameters_integ.yml }
- { resource: config.yml }
framework:
profiler: { only_exceptions: false }
the parameters_integ.yml file contains parameters for integration environment. example of the beggining :
parameters:
database_host: localhost
database_port: null
database_name: test_integ
database_user: root
the parameters.yml.dist contains these parameters with default values :
parameters:
database_host: 127.0.0.1
database_port: ~
database_name: symfony
database_user: root
How can i tell to composer to take the parameters_integ.yml file to build the parameter.yml file ? Because i will have a prod environment after, i can't write my parameters in parameters.yml.dist file.
Tell me if you need other informations
Thanks for your help
1) You should include composer.json and composer.lock in your VCS
2) You should not include parameters.yml in your VCS
3) When deploying Sf application you should use composer install
http://symfony.com/doc/current/deployment.html#c-install-update-your-vendors
difference between composer install and composer update (+ composer.lock): https://blog.engineyard.com/2014/composer-its-all-about-the-lock-file
4) I can think of a few ways for creating parameters.yml in the integration environment:
you manually create parameters.yml file and place it beside parameters.dist.yml
or
if there is no parameters.yml or parameters.yml has some missing parameters, when you run composer install, you will be prompted to enter missing parameters and parameters.yml will be created/updated during the composer install process
or
you can have an app/config/parameters folder containing parameters_prod.yml and parameters_integ.yml files, and one of those files is automatically copied into parameter.yml file during deployment (most complicated solution)
If you want integration environment to be accessible via a browser, you should also create a front controller for it. Copy the web/app.php file to web/app_integ.php and edit the environment to be integration:
// web/app_integ.php
// ...
// change just this line
$kernel = new AppKernel('integ', false);
// ...
More info at https://symfony.com/doc/current/configuration/environments.html#creating-a-new-environment
And the answer to the question from the title, "How does composer know symfony environment" - I think composer has no idea about Sf environment :)
The composer has require-dev option for packages that you do not need in your production, packages required for running tests (phpunit for example), some debugging stuff during development, etc.
For an example this is how one of my composer.json files looks like:
...
"require-dev": {
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0",
"doctrine/doctrine-fixtures-bundle": "^2.3",
"nelmio/alice": "^2.1",
"deployer/deployer": "^4.0"
},
...
If I run composer install --no-dev, these packages won't be installed in the vendor directory.
But, how Sf knows when to include/omit some packages in the app.
That is defined inside AppKernel.php.
In the composer.json, under "require-dev" key I defined that I don't want doctrine fixtures in production.
So, inside AppKernel.php I put these lines of code:
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
...
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
And if I run my app in production mode, there will not be DoctrineFixturesBundle.
Best practice is to set your default parameters in your app/config/parameters.yml.dist file.
See this link:
http://symfony.com/doc/current/best_practices/configuration.html#canonical-parameters
When you run:
composer update
it will use the parameters.yml.dist and overwrite the parameters.yml with the values in the dist file.
When I run doctrine schema update, I am getting this weird queries that need to be executed but they are basically just redoing already done or not?
php app/console doctrine:schema:update --dump-sql
DROP INDEX idx_26d7e8feab772a3c ON notify;
CREATE INDEX IDX_217BEDC8AB772A3C ON notify (notifyUser_id);
DROP INDEX idx_26d7e8fea76ed395 ON notify;
CREATE INDEX IDX_217BEDC8A76ED395 ON notify (user_id);
DROP INDEX idx_26d7e8fe6bf700bd ON notify;
CREATE INDEX IDX_217BEDC86BF700BD ON notify (status_id);
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.5.3",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"gedmo/doctrine-extensions": "2.3.*#dev",
"stof/doctrine-extensions-bundle": "~1.1#dev",
},
This behaviour stops me from updating doctrine schema with --force because there are foreign keys and I get this error:
[PDOException]
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'IDX_A37CA197A76ED395': neede
d in a foreign key constraint
This started to happening probably after updating DB related vendors in past but I haven't notice it until now.
#theapprenticecoder #Trki this problem can be solve by the command
$ app/console doctrine:schema:update --force --complete
which is the best solution.
I can confirm you that this probably happened when you upgraded Doctrine Vendors.
This issue was fixed with the doctrine/dbal 2.5.1 version.
All you need to do is install the v2.5.1 doctrine/dbal dependency.
Then just do a "php app/console doctrine:schema:update --force" and your problem will be solved...