I've successfully installed and used doctrine according to this article: Getting started with Doctrine.
Now I want to reverse engineer an existing database. I tried running the following command:
php vendor/doctrine/orm/bin/doctrine orm:convert-mapping --from-database annotation entity/generated
and that generated entity files with the correct annotations. However, I want to generate the setters and getters instead of having to code them myself.
Many people are referring to this article: How to generate Entities from an Existing Database. The first command in the article is:
php app/console doctrine:mapping:import --force AcmeBlogBundle xml
I cannot find the app folder nor the console file from the installation of Symfony and Doctrine. My composer.json file contents are:
{
"require": {
"doctrine/orm": "2.4.*",
"symfony/yaml": "2.*"
},
"autoload": {
"psr-0": {"": "src/"}
}
}
I got this from the Getting Started with Doctrine article. Am I missing a dependency in my composer.json file? Where can I find app/console?
EDIT 1:
Paul Andrieux, I added "symfony/framework-standard-edition": "2.5.*" to my composer.json file. Now I have a folder vendor/framework-standard-edition. This contains an app folder which contains the console file. However, I get an error for because the "console" script is attempting "require_once DIR.'/bootstrap.php.cache'" and the bootstrap.php.cache does not exist. What should this file contain? do I need to create it myself? What other steps should I take after creating or acquiring this file?
I found way to generate Getter & Setters just use Doctrine ORM.
you should use orm:convert-mapping generate XML mapping file then use orm:generate-entities generate XML to PHP class
orm:convert-mapping [--filter FILTER] [-f|--force] [--from-database] [--extend [EXTEND]] [--num-spaces [NUM-SPACES]] [--namespace [NAMESPACE]] [--] <to-type> <dest-path>
orm:generate-entities [--filter FILTER] [--generate-annotations [GENERATE-ANNOTATIONS]] [--generate-methods [GENERATE-METHODS]] [--regenerate-entities [REGENERATE-ENTITIES]] [--update-entities [UPDATE-ENTITIES]] [--extend EXTEND] [--num-spaces NUM-SPACES] [--no-backup] [--] <dest-path>
A. My composer.json (I'm use doctrine 2.5.*)
...
"require": {
"doctrine/orm": "^2.5"
},
...
B. Config cli tools
My cli-config.php
NOTE: use method createXMLMetadataConfiguration NOT createAnnotationMetadataConfiguration
<?php
$db = [
// ...
];
$paths = [ __DIR__ . '/../Model/'];
// this place should use function Setup::createXMLMetadataConfiguration
$config = \Doctrine\ORM\Tools\Setup::createXMLMetadataConfiguration($paths, false);
$entityManager = \Doctrine\ORM\EntityManager::create($db, $config);
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
C. generate XML
php vendor/bin/doctrine orm:convert-mapping -f --namespace='Model\' --from-database xml app/Model
path app/Model must match you paths when set in file cli-config.php
D. generate PHP class
php vendor/bin/doctrine orm:generate-entities --generate-annotations="true" app/
this will generate the right content and right namespace to what you want
According to your composer.json file, you didn't install symfony2, only it's yaml component.
Just follow this guide if you want to install symfony2 full stack: http://symfony.com/get-started
Related
I want to create a new entity in my ORO platform application by using the make:entity command from the MakerBundle.
I expect it to create an entity in my bundle Acme\Bundle\TestBundle which I set in my config_dev.yml by using:
maker:
root_namespace: 'Acme\Bundle\TestBundle'
So I execute
bin/console make:entity Test
which returns
! [NOTE] It looks like your app may be using a namespace other than "Acme\Bundle\TestBundle".
!
! To configure this and make your life easier, see:
! https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#configuration
created: src/Acme/Bundle/TestBundle/Entity/Test.php
created: src/Acme/Bundle/TestBundle/Repository/TestRepository.php
[ERROR] Only annotation mapping is supported by make:entity, but the
<info>Acme\Bundle\TestBundle\Entity\Test</info> class uses a different format. If you would like
this command to generate the properties & getter/setter methods, add your mapping configuration, and then
re-run this command with the <info>--regenerate</info> flag.
I've tried to run the command once again, which works. But obviously this is not the way how it's meant to work. So how can I fix this mapping error?
I started with the standard Symfony 5 project by using Symfony new myapp.
I add the config file in config/packages/dev/maker.yaml
maker:
root_namespace: 'App\Core'
To generate the entity in the src/Core folder, I got the following error:
➜ symfony console make:entity Post
created: src/Core/Entity/Post.php
created: src/Core/Repository/PostRepository.php
[ERROR] Only annotation mapping is supported by make:entity, but the <info>App\Core\Entity\Post</info> class uses
a different format. If you would like this command to generate the properties & getter/setter methods, add your
mapping configuration, and then re-run this command with the <info>--regenerate</info> flag.
To avoid showing the error in the console, I install the patch created by vklux / maker-bundle-force-annotation
Step 1: install package
composer require cweagans/composer-patches
Step 2: apply the patch in composer.json
{
// {...} composer.json content
"extra": {
"patches": {
"symfony/maker-bundle": {
"Provide flag to force annotation in make entity command": "https://raw.githubusercontent.com/vklux/maker-bundle-force-annotation/master/maker-force-annotation-flag.patch"
}
}
}
}
Step 3: composer update
composer update
Step 4: try make:entity with the additional command option
➜ symfony console make:entity Post --force-annotation
created: src/Core/Entity/Post.php
created: src/Core/Repository/PostRepository.php
Entity generated! Now let's add some fields!
You can always add more fields later manually or by re-running this command.
New property name (press <return> to stop adding fields):
>
✅ 🚀 👍 Everything works fine now.
It's a bug in the doctrine version please try this version good luck
composer req doctrine/doctrine-bundle:2.2
I found a solution to create new entities with Symfony maker using doctrine/doctrine-bundle version higher than 2.2.4
These steps worked for me with:
"doctrine/doctrine-bundle": "^2.7"
PHP 8.1 (I migrate from php 7.4 in my case)
STEP 1: replace type: annotation by type: attribute inside doctrine.yaml
STEP 2: Enter entity root with namespace:
symfony console make:entity App\Entity\test
This question already has answers here:
Symfony3 ClassNotFoundException after bundle creation
(4 answers)
Closed 5 years ago.
I've tried to automatically generate a bundle in Symfony3, using instructions on the official site: https://symfony.com/doc/current/bundles.html, but failed.
I created a directory \src\TestBundle in my project with a file TestBundle.php in it. I extended my class TestBundle from Bundle and added the use Symfony\Component\HttpKernel\Bundle\Bundle; line before the class declaration.
I added my new bundle name to the bundles list in AppKernel.php.
Finally, when I execute $ php bin/console generate:bundle --namespace=TestBundle, I get this error:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestBundle" from namespace "TestBundle".
Any ideas why that happens?
If you launch this command:
php bin/console generate:bundle --namespace=TestBundle
Symfony create the Bundle for you, you don't need to create file and add It into the AppKernel file.
Try to remove all your files and reference about TestBundle and after launch only the command, Symfony will create and register the Bundle for you.
Instead i you want to create manually thos file you need to create the directory \src\TestBundle and inside It file TestBundle.php.
After you need to register It into your AppKernel.
After you need to tell to composer to load that bundle, if is not already done use this configuration for autoload inside composer.json
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
And finally launch the command inside your console:
composer dump-autoload
I am making a php application using propel ORM. It gives me the following message when I try to run it:
Fatal error: Uncaught Error: Class 'Propel\Runtime\Propel' not found in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php:2 Stack trace: #0 C:\MAMP\htdocs\Conference\vendor\bin\list.php(6): require_once() #1 {main} thrown in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php on line 2.
In my config.php generated file I have this written:
'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper'
What does it all mean? Am I missing some file or what?
I think you are missing a step in the building.
I assume you have your schema.xml file complete and you also have a propel.yaml (or with allowed extension file) properly configured. Also I assume you got Propel with Composer.
If you have all that the next steps are:
1) Open a terminal and go to your project directory, where the schema.xml and propel.yaml files are.
2) Execute the following command to get yout generated-sql (I have to do it this way on Windows):
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:build
3) Get your model classes with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel model:build
4) After generating the classes, you have to autoload them. Open your composer.json file with your text editor and add the following:
"autoload": {
"classmap": ["generated-classes/"]
}
It should look like this, for example:
{
"require": {
"twig/twig": "~1.0",
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["generated-classes/"]
}
}
5) To finish the classes autoloading, you need to execute on your console:
composer dump-autoload
6) And for the runtime connection settings run this for comunicate classes at runtime:
c:\MAMP\htdocs\Conference\vendor\bin\propel config:convert
7) Assuming you have created your database, the last thing you need to do is create the tables, this is with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:insert
And there you go! That works for me every time I build a project.
I tried generate getters and setters in Symfony 3.0.1
when i run command
php bin/console doctrine:generate:entities VendorName/MyBundle/EntityName
i have error
Namespace "VendorName\MyBundle\EntityName" does not contain any mapped entities.
where is the mistake?
Edit-1: First generate entity with YAML format
Edit-2: I tried generate getters and setters for vendor bundle
Also i try with command php bin/console doctrine:generate:entities VendorNameMyBundle:EntityName and have another error:
Can't find base path for "VendorName\MyBundle\Entity\EntityName" (path: "/home/site/vendor/vendorname/mybundle/Entity", destination: "/home/site/vendor/vendorname/mybundle/Entity").
As John Pancoast points out in his answer to a different question:
Doctrine does not support PSR-4 when doing anything with code generation. It has to do with how they map class namespaces to filesystem paths and how PSR-4 allows class/namespace paths that don't directly map to the filesystem.
https://github.com/doctrine/DoctrineBundle/issues/282
To clarify what exactly is needed to resolve the error message; You have to edit your bundle's composer.json file, and also change the bundle's folder structure.
in composer.json change psr-4 to psr-0:
"autoload": {
"psr-4": { "Acme\\Bundle\\AwesomeBundle\\": "" }
},
to:
"autoload": {
"psr-0": { "Acme\\Bundle\\AwesomeBundle\\": "" }
},
Change bundle's folder structure from:
vendor
+--acme
+--awesome-bundle
|--Controller
|--Entity
to:
vendor
+--acme
+--awesome-bundle
+--Acme
+--Bundle
+--AwesomeBundle
|--Controller
|--Entity
The following command will no longer throw an exception:
bin/console doctrine:generate:entities AwesomeBundle
You have mistake in command, you trying to generate entities but you provide class name for one entity. Try the following for all entities:
php bin/console doctrine:generate:entities VendorName/MyBundle
or if you want just one entity:
php bin/console doctrine:generate:entity VendorName/MyBundle/EntityName
Symfony 3.22 with src/AppBundle/Entity/User.php
if you add new field using ORM
**/**
* #ORM\Column(name="last_login", type="datetimetz")
*/
private $lastLogin;**
just use
use php bin/console doctrine:generate:entities AppBundle
it will check all your Entities r and update getters and setters
then use
php bin/console doctrine:schema:update to update your database
use
php bin/console doctrine:schema:update --force in PROD enviroment
I've problem in configuring Propel with Composer in my php project.
this is how appears my tree directory:
project/
|--/public_html/index.php
|--/app/
| |--data/
| | |--propel.json
| | |--schema.xml
| |--vendor/
| |--composer.json
In /data/ folder I would store all my propel files, that is generated-classes/ , generated-conf/ and generated-sql/ .
To realize this purpose, with a terminal in /data/ folder, I put the commands in the following sequence:
$ propel sql:build
$ propel model:build
$ propel config:convert
and all go right.
To make more suitable work, in composer.json I've added this extra feature:
"autoload": {
"classmap": ["./data/generated-classes/"]
}
so that, almost in theory, putting
require '../app/vendor/autoload.php';
inside index.php should be enough. Unfortunately, when I try to use one propel classes inside this page, returns the error
Type: Propel\Runtime\Exception\RuntimeException
Message: No connection defined for database "my_api". Did you forget to define a connection or is it wrong written?
File: 'C:\pathToMyProject'\project\app\vendor\propel\propel\src\Propel\Runtime\ServiceContainer\StandardServiceContainer.php
Line: 279
I thought that propel doesn't find the propel.json file stored in /data/folder.
As extra, if in index.php I simply add
require_once '../app/data/generated-conf/config.php';
all goes right.
There's a trick to autoload propel without use this last require_once? (obviously keep the tree as is).
Thanks for reading.
The order of CLI commands is important:
composer install or update to fetch propel
then the commands to generate the models with propel
then re-scan / re-generate the autoloading files with composer dump-autoload --optimize
You could include the configuration file in the bootstrap process of your application - like you already have.
Or you could use the files directive in Composers autoload section
to define file(s), which should be included on every request.
Referencing: https://getcomposer.org/doc/04-schema.md#files
"autoload": {
"files": ["./data/generated-conf/config.php"],
"classmap": ["./data/generated-classes/"]
}