How to generate entities and cruds in SF4? - php

I've just installed symfony 4 and after noticing the structure being slightly different, searching for some config files and editing around a bit, I was planning to go and generate my first entities and cruds.
However, I found that symfony 4 does no longer support the doctrine:generate:entity command.
Instead I found that symfony now offers the MakerBundle that comes with a range of simple commands to generate the most basic code snippets.
What I am wondering is if there is still a way to interactively generate an entity and/or crud.
I tried installing the SensioGeneratorBundle but that doesn't yet seem compatible with symfony 4.

Use MakerBundle:
composer req doctrine maker
For example, create your entity:
php bin/console make:entity Product
If you want use annotations, run:
composer req annotations
...then you need this informations:
Examples the commands for to works with entities (database)
Symfony use Doctrine.
If your don't have database, run this command:
php bin/console doctrine:database:create
If you want create entities in your database:
php bin/console doctrine:schema:create
If your need update your entities, run this command:
php bin/console doctrine:schema:update --force
For help, run command:
php bin/console list doctrine
So, your can generate entities if you already have database, see list doctrine.

Create a plain old PHP object, add some doctrine annotations to it for the properties which are columns, then do a doctrine:migrations:diff. A migration file will be created with the SQL required. Then you run doctrine:migrations:migrate and the SQL will be executed.

What I am wondering is if there is still a way to interactively generate an entity and/or crud.
Why not use maker, which you mentioned?
composer require maker --dev
Then, run:
bin/console make:entity

Related

Display commands of one namespace in symfony2

Is there a way to display all commands of only one namespace? all commands of only doctrine for instance?
It'd be really nice since I have a lot of commands and a lot of command namespaces .
I do know | grep <namespace> but looking for a built-in symfony option if it exists
Given that I'm using symfony2.0
Enter only the beginning of the namespace to list the available commands:
$ php app/console doctrine
[InvalidArgumentException]
Command "doctrine" is not defined.
Did you mean one of these?
doctrine:query:sql
doctrine:query:dql
doctrine:schema:drop
doctrine:mapping:info
[…]
But it won't display all the commands if the number is low:
$ php app/console doctrine:cache
[InvalidArgumentException]
Command "doctrine:cache" is ambiguous (doctrine:cache:clear-result, doctrine:cache:clear-query and 1 more).
The list command can also be used, it will sort the commands and add a description:
$ php app/console list doctrine
Symfony version 2.3.28 - app/dev/debug
Usage:
command [options] [arguments]
Options:
--help -h Display this help message
[…]
Available commands for the "doctrine" namespace:
doctrine:cache:clear-metadata Clears all metadata cache for an entity manager
doctrine:cache:clear-query Clears all query cache for an entity manager
doctrine:cache:clear-result Clears result cache for an entity manager
doctrine:database:create Creates the configured databases
doctrine:database:drop Drops the configured databases
doctrine:ensure-production-settings Verify that Doctrine is properly configured for a production environment.
[…]
doctrine:schema:create Executes (or dumps) the SQL needed to generate the database schema
doctrine:schema:drop Executes (or dumps) the SQL needed to drop the current database schema
doctrine:schema:update Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata
doctrine:schema:validate Validates the doctrine mapping files
$ php app/console list doctrine:cache
Symfony version 2.3.28 - app/dev/debug
[…]
Available commands for the "doctrine:cache" namespace:
doctrine:cache:clear-metadata Clears all metadata cache for an entity manager
doctrine:cache:clear-query Clears all query cache for an entity manager
doctrine:cache:clear-result Clears result cache for an entity manager

Generate Entities from an Existing Database

I am new to Symfony and now I am currently doing an application using this framework. I am now trying to generate entities from an existing database and while I run the following command:
php app\console doctrine:mapping:convert annotation .\src\AppBundle\Resources\config\doctrine
I've got this error message:
No Metadata Classes to process.
Could you please tell me what's happening with this?
From the docs, it seems you are missing the --from-database argument. I've never used that tool though.
http://symfony.com/fr/doc/current/cookbook/doctrine/reverse_engineering.html
php app/console doctrine:mapping:convert xml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force

Symfony2 Import entities from existing database with Doctrine

I am a beginner with symfony. I have tried to follow the official documentation http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html to import a entities from MySQL to a symfony2 project in a Debian testing box. but I didn't succeeded.
Then searching here and there I have found this Generating a single Entity from existing database using symfony2 and doctrine here, but I cant make it work, my console:
"ask Doctrine to introspect the database and generate the corresponding metadata files"
root#khs01wxl001:/var/www/organizer$ php app/console doctrine:mapping:import --force organizerscheduleBundle php
Importing mapping information from "default" entity manager
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Dept.orm.php
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Desg.orm.php
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Dir.orm.php
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Schedule.orm.php
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Username.orm.php
> writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Userrole.orm.php
root#khs01wxl001:/var/www/organizer$
So far so good, but now "you can ask Doctrine to build related entity classes by executing the following two commands.
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle
but when I do it the first one doesn't work for me:
root#khs01wxl001:/var/www/organizer$ php app/console doctrine:mapping:convert annotation ./src/organizer/scheduleBundle/Resources/config/doctrine/
No Metadata Classes to process.
root#khs01wxl001:/var/www/organizer$
Any suggestion?
Try with:
# write the structure to annotation file (I prefear to use YML instead annotation but should be the same)
$ php app/console doctrine:mapping:convert annotation ./src/organizer/scheduleBundle/Resources/config/doctrine/metadata/orm --from-database --force
# Import the structure
$ php app/console doctrine:mapping:import organizerscheduleBundle annotation
# Generate Entities file class
$ php app/console doctrine:generate:entities organizerscheduleBundle
More docs here:
http://docs.doctrine-project.org/en/2.0.x/reference/tools.html#reverse-engineering
Not all will be imported (the doc says 70-80% but in my opinion is less).
PS. A best pratice is to call the bundle OrganizerScheduleBundle (look uppercase letters) with organizer as company name, so inside the developer/company name folder there will be all the bundles.
If you are using symfony 3 and wanna use the annotations, i used this:
php bin/console doctrine:mapping:import --force AcmeBlogBundle annotation
This generate all you need, without needing the doctrine:mapping:convert

symfony2 tutorial - generate bundle

I am new to symfony2 but not to symfony. I am currently doing the symblog tutorial. Its going ok so far however I have come a little stuck on the generate bundle?
The tutorial says to run the following command
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml
After completing that it should add reference/generate code to the bundle in the
app/AppKernel.php
app/config/routing.yml
However it has not added anything?... I'm a bit confused? The console outputs the following but the code references to the bundle have not been generated in the files
Welcome to the Symfony2 bundle generator
Your application code must be written in bundles. This command helps
you generate them easily.
Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).
See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more
details on bundle naming conventions.
Use / instead of \ for the namespace delimiter to avoid any problem.
Bundle namespace [Blogger/BlogBundle]:
You can use this without interaction . then it won't ask you anything
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction
From generate:bundle --help
If you want to disable any user interaction, use --no-interaction but don't forget to pass all needed options:
php app/console generate:bundle --namespace=Acme/BlogBundle --dir=src [--bundle-name=...] --no-interaction
I have bash functions for these long commands.
genbundle () {
php app/console generate:bundle --namespace=$1/$2Bundle --bundle-name=$2Bundle --dir=src/ --format=yml
}
You can use it like this: "genbundle Acme Blog", It'll create a BlogBundle in the Acme app.
If I understood what's going on correctly, you have to go on the process. It's step-by-step where you answer the questions. Keep answering everything asked, and then the console will tell when it created the bundle.
Note: the value in [...] are the default value if you just press enter when the console asks you something.
Old Command:
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction
In new symfony version 3.1.4, "app/console" is no more working.
Instead add "bin/console" in above command
So the New command will be
php bin/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction

How to generate entity from existing table in symfony2?

I have table "my_table" with some fields.
I want generate Entity in MyBundle used "my_table". But I don't want recreate all entities in MyBundle.
How can I do this?
Here is the way you can do it,
First step, ask Doctrine to introspect the database and generate the corresponding xml or yml metadata files.
php app/console doctrine:mapping:convert [xml|yml] Path/To/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter=MyTable
Second step, ask Doctrine to import the schema and build related entity classes by executing the following two commands.
php app/console doctrine:mapping:import MyBundle [xml|yml|annotation] --filter=MyTable
php app/console doctrine:generate:entities Path\To\MyBundle\EntityFolder\\MyTable
Take a look at the How to generate Entities from an Existing Database section of the documentation
Simple Working Solution for Symfony 2.7 option annotation and for [/xml/yml] see http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
do 3 commands in 3 steps:
Command #1:
$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Meeting"
Output:
writing C:\xampp\htdocs\localxyz\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml
Command #2:
$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Meeting"
Output:
Processing entity "Meeting"
Exporting "annotation" mapping information to "C:\xampp\htdocs\localxyz\src\Entity"
Command #3:
$ php app/console doctrine:generate:entities AppBundle:Meeting --no-backup
Output:
Generating entity "AppBundle\Entity\Meeting"
generating AppBundle\Entity\Meeting
where:
AppBundle is exactly your "AppBundle" in 2.7 symfony
Meeting is the target table (case sensitive)
TO BE SURE, check this directory:
C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml
C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/MeetingOriginal.orm.xml
AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others.
It works very well for me.
For explanation please read: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity
Although this is an old post but if someone gets following error,
Database does not have any mapping information.
Check
If your table name is blog_post then in filter option use BlogPost and not blog_post
Reference: https://stackoverflow.com/a/27019561/6504104
though this is covered by answers above but I missed it and was getting this error
So I wanted to make this explicit
Also in symfony >= 3.4 its' php bin/console e.g.
php bin/console doctrine:mapping:import --force AppBundle xml --filter="BlogPost"
and then
php bin/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="BlogPost"
Thanks...

Categories