I use CodeIgniter 2 with doctrine 2 and this is the project that I am working in
https://github.com/wildlyinaccurate/CodeIgniter-2-with-Doctrine-2
I need to generate Entity classes from existing Database
so I configured the Doctrine to be in development mode and I set the Database on the CodeIgniter
after that I write this command
php doctrine orm:convert-mapping --from-database annotation models/generated
the classes generated correctly from database but without any method,after that I write this command for generating entities
php doctrine orm:generate-entities --regenerate-entities="1" models/generated
but I find this error "No Metadata classes to process"
Thanks.
In the entity files generated, you neet do delete "use Doctrine\ORM\Mapping as ORM;"
and replace "ORM\" with ""
In Doctrine.php in libraries folder add these lines, to load the driver:
$driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings');
$config->setMetadataDriverImpl($driver);
Create your Mapping files.
And generate entities:
php cli-doctrine.php orm:generate-entities models --generate-annotations=true
Now you can create tables in your db:
php cli-doctrine.php orm:schema-tool:create
Most of doctrine documentation and tutorials are not correct. You should use this command:
php doctrine orm:generate:entities --generate-annotations=true models/generated
Here is correct line of code try now:
php doctrine orm:generate-entities --generate-annotations=true models/generated
Related
My objective is to get the schema from an open source project on a git repository below:
https://github.com/monarc-project/zm-core
I am trying to set up the doctrine CLI for this application to probe the database and generate schema but after installing the dependencies and running doctrine I get the following error:
$php vendor/bin/doctrine
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:
<?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);
From error message it seems there are two paths I can take:
I should not be using doctrine and instead use bin/console. However for console to work I need to upgrade to php 8.1 which doesn't meet this projects requirements. Thus is not an alternative.
If I still try to use doctrine I cannot see cli-config.php which I am presuming should have been there before. I tried to create one but then don't know which bootstrap.php should be used by cli-config.php. There are following bootstrap files present in vendor directory.
./vendor/symfony/polyfill-intl-normalizer/bootstrap.php
./vendor/symfony/polyfill-intl-idn/bootstrap.php
./vendor/symfony/polyfill-php81/bootstrap.php
./vendor/symfony/polyfill-ctype/bootstrap.php
./vendor/symfony/polyfill-php73/bootstrap.php
./vendor/symfony/polyfill-php80/bootstrap.php
./vendor/symfony/polyfill-mbstring/bootstrap.php
./vendor/symfony/polyfill-intl-grapheme/bootstrap.php
./vendor/symfony/polyfill-php72/bootstrap.php
./vendor/cakephp/utility/bootstrap.php
I am following this documentation since I want to auto generate entity depending on the table in my database, and at the XML generation stage I am passing this line php bin/console doctrine:mapping:import --force DoctrineMappingBundle xml. Since I dont have AppBundle, I created my own DoctrineMappingBundle.php in src/Entity/
My DoctrineMappingBundle.php looks like this
namespace App\Entity;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DoctrineMappingBundle extends Bundle
{
}
It does generate the XML files, however when I run the entity generation command
php bin/console doctrine:mapping:convert annotation ./src, it throws up this error
In MappingException.php line 317:
An error occurred in App\Entity\Entity\BlogComment
and
In AnnotationDriver.php line 60:
Class App\Entity\Entity\BlogComment does not exist
I am stuck at this point and I dont know why it could not generate the entity classes, and it seems to automatically add an extra Entity in the namespace but in my namespace Entity is only mentioned once in the namespace. I am not sure which configuration is causing this issue.
My src folder structure
src---------/
|--Controller/
|--Entity--/
|-----DoctinemappingBundle.php
|--Migrations/
|--Repository/
|--Kernel.php
First off, the docs make it clear that this functionality is no longer supported and if your read the github issue you will see why.
However the functionality is still there and your idea of using a fake bundle will work. Just need the mapping bundle to be in the src directory. And then a bit of copy/paste.
Here is a complete step by step approach to generating entities under S4.
// Install the software
mkdir mapping
cd mapping
composer create-project symfony/skeleton .
composer require orm-pack
// adjust DATABASE_URL in .env
Make a fake bundle for the mapping command:
// src/DoctrineMappingBundle.php
namespace App;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DoctrineMappingBundle extends Bundle
{
}
// Add the bundle to config/bundles.php
App\DoctrineMappingBundle::class => ['dev' => true],
Now we are ready to make some entities:
// Generate the xml mappings
bin/console doctrine:mapping:import DoctrineMappingBundle xml
// Mapping files generated under src/Resources/config/doctrine
// Generate the entities
php bin/console doctrine:mapping:convert annotation ./src
This generates the entities under src/App/Entity. At this point you will want to copy the generated entities to src/Entity and then remove the App/Entity directory. The namespace should already be correct.
Get rid of the mapping files by removing src/Resources as well.
This is as far as I took the exercise but you should be good to go.
I have some problem. I work on symfony 1.4. I want to add a new column in my table and generate in php getter and setter to use it.
So In my shema.yml I add my column in my table like this:
version : {type: integer}
userName_canonical : {type: varchar(255)}
email_canonical : {type: varchar(255)}
qualifications: {type: array}
qualification_autre: {type: string}
promo: {type: boolean, default: 0}
next_request_at: {type: date, default: null}<-----
But I search everywhere and I don't know how I can modify my entity and create this column in my database. Someone know which is the command using to modify my entity in symfony 1.4 ??
Thank in advance
According to the Symfony 1.4 documentation you need to run these commands after modifying your schema file:
php symfony doctrine:build --model
php symfony doctrine:build --sql
php symfony doctrine:insert-sql
Before running these commands, please read to this documentation page under the heading 'The ORM' just to make sure those commands are what you're looking for.
Doctrine entities can then be generated via this command
php symfony doctrine:build --model
Make sure to backup any existing work before doing this however, as it may overwrite any changes to existing classes.
Before you create the new model classes (using the doctrine:build commands), you should create migration files.
For me, the process is usually:
./symfony doctrine:generate-migrations-diff
This should create a new file in lib/migrations/doctrine with commands to add any new columns.
./symfony doctrine:build --all-classes
This will create or update the classes in lib/{model,form,filter}/doctrine/base to include the new columns.
./symfony doctrine:migrate
This actually runs the migration. Otherwise you'll get a SQL error because of the missing column. The migration clearly needs to be run on production as well; we have that as part of our deploy scripts to run every time.
If you've already built new classes, you'll need to add a few steps to the beginning of the above process:
Temporarily comment out or revert the schema.yml change
Run ./symfony doctrine:build --all-classes
Make the schema.yml change again
Go back to step 1, the generate-migrations-diff step above.
I want generate new "Banner" entity class:
vendor/bin/doctrine orm:generate:entities --generate-annotations="true" Entity/Banner
but I have this error:
[InvalidArgumentException] Entities destination directory
'Entity/Banner' does not exist.
The correct command is the next.
php vendor/bin/doctrine orm:generate-entities
You can see the docs in this page
But maybe your problem is other, like the directory does not exist.
Did you try like this?
vendor/bin/doctrine orm:generate:entities --generate-annotations="true" Banner
Use tab key to autocomplet the command, maybe that can help
I have Entity classes generated with Doctrine ORM, and ZF2.
I changed a table structure and I want to update an entity class, so I am trying to regenerate the entity class but it's not working.
I used the following code:
vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Album\Entity\" --force --from-database annotation ./module/Album/src/clear
I got the error:
[Doctrine\ORM\Mapping\MappingException]
Property "status" in "Album\Entity\TestRun" was already declared, but it must be declared only once
orm:convert-mapping [--filter="..."] [--force] [--from-database] [--extend[="..."]] [--num-spaces[="..."]] [--namespace[="..."]] to-type dest-path
I want to re-generate the entity class for a particular table
If you change the structure of your Entity file a simple
\vendor\bin\doctrine-module orm:schema-tool:update --force will alter the table according to your Entity definition.
If you still want to recreate the table simple drop the table from your mysql and run the update command. You may have some cache files left so clearing those might be a good Idea, too.
You can clear the complete cache like so:
\vendor\bin\doctrine-module orm:clear-cache:query
\vendor\bin\doctrine-module orm:clear-cache:result
\vendor\bin\doctrine-module orm:clear-cache:metadata
As mentioned above it might be a bad practice, but nevertheless I use the following commands to achieve the result you are asking about:
vendor\bin\doctrine-module orm:convert-mapping --filter='Evaluation' --namespace='MyModule\Entity\\' --force --from-database annotation ./module/MyModule/src/
and another command to generate getters and setters:
vendor\bin\doctrine-module orm:generate-entities --filter='Evaluation' ./module/MyModule/src/ --generate-annotations=true