Doctrine 2 mapping - cant auto generate yml files - php

I'm trying to get doctrine 2 work for me (I used to work with d1)
but I can't get doctrine generate the models (like on doctrine1)
I have downloaded the lastest version of doctrine 2.1
so my directory tree looks like that :
C:\doctrine\bin - got it from the tar file
C:\doctrine\Doctrine - got it from the tar file
C:\doctrine\models - created by me for mapping info
I also edited the bin doctrine.php file and added cli-config.php file and its looking like that:
C:\doctrine\bin\doctrine.php --> http://pastebin.com/Ki62hbTX
C:\doctrine\bin\cli-config.php --> http://pastebin.com/cPv4fXX1
Now when I'm going to cmd this is the result:
c:\doctrine\bin>php doctrine orm:schema-tool:create
No Metadata Classes to process.
I checked the paths of files, write permissions, db login info and every thing but I can't find out how to read db and generate the class
any idea will be great, thanks

There might be several problems here, but let me start by saying that I just moved to doctrine2 from 1.2 and it is way easier to use php annotations in doctrine2. Then you're creating the class yourself....everything just seems to make sense a little more.
But, if you want to use YAML, be aware that with YAML and Doctrine2, the YAML hierarchy is completely different. So you can't just load your doctrine1 yaml into doctrine2.
Here's the guide to YAML in doctrine 2: http://www.doctrine-project.org/docs/orm/2.0/en/reference/yaml-mapping.html
The one thing that I found is that Doctrine2 wanted a specific folder for my namespace. So my entities are in entities/NAMESPACE/ rather than just entities/. Also note that you need a separate file for each class and the naming conventions are very strict.

Related

How to refactor path names like `AppBundle:SomeFolder:someFile` in Symfony 2/3 based project to be used in Symfony 4+?

I am working on a Symfony project which was started based on Symfony 2 and currently uses Symfony 3.4. A lot of code in the project specifies paths using the : notation, for example:
$this->render('AppBundle:Products:products_list.html.twig');
// ==> /path/to/project/src/AppBundle/Resources/views/Products/products_list.html.twig
$this->getRepository('AppBundle:Products\Product');
// ==> /path/to/project/src/AppBundle/Entiy/Products/Product.php
The Project should now be updated to Symfony 5. Since Bundles are not (should be) used any more, a lot of these paths will change and need to be updated.
Doing this for entity classes is not a big deal, the ::class notation can be used without specifying any path:
$this->getRepository(Product::class);
But how to update all the other paths which do not represent a path but some asset or config file, etc.? Is there any rule of thumb on how 'AppBundle:Products:products_list.html.twig' should be updated when it is moved to /path/to/project/templates/app/products/products_list.html.twig?
EDIT:
Of course I can manually update all paths, e.g. from AppBundle:Products:products_list.html.twig to app/products/products_list.html.twig. Although some search/replace in all sources files might help, this is a cumbersome process.
Updating the class paths, e.g. from 'AppBundle:Products\Product' to Product::class also need manually work, but this approach has two major advantages:
The search/replace could be done in while still working with Symfony 3.4. The changes could be tested before performing the update. When updated to Symfony 5 no further changes would necessary, the MyEntity::class notation would simply deliver the new paths.
The path is not hard coded. If the file location changes again, no further updated is required.
Of course I am aware that these advantages are a benifit of using the ::class method. The question is, if there is any way in Symfony to achieve (nearly) the same for non-class files, like assets, config files, etc. Or is hard cording the paths the way to go?

doctrine zend zf3 orm entity mapping

Problems generating PHP entites from a database with tables. I am trying to generate entities, getters/setters and any associated annotations from tables already defined in a database.
I have been following several doctrine tutorials but cannot get the entities to successfully generate
Environment (windows xampp PHP 7.1): ZendFramework 3 skeleton application downloaded.
"zendframework/zend-component-installer" : "^1.0 || ^0.7 || ^1.0.0-dev#dev",
"zendframework/zend-mvc" : "^3.0.1",
"zfcampus/zf-development-mode" : "^3.0",
"zf-commons/zfc-user" : "^3.0",
"zendframework/zend-log" : "^2.9",
"doctrine/doctrine-orm-module" : "^1.1",
"zendframework/zend-json" : "^3.1"
from "App_Root" running the command below generates PHP classes with variable=>field mapping values for each table(note the directory matches namespace structure that is defined):
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="ModuleName\\Entity\\" --force --from-database annotation ./EXPORT
(files generated in /EXPORT/ModuleName/Entity)
So far so good.
Now trying to generate the getters and any additional annotation data using
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate:entities ./EXPORT/ModuleName/Entity
Produces:
[OK] No Metadata Classes to process.
For the life of me I cannot find any configuration with orm:generate:entities that points to some form of Metadata Classes... Is there a step I'm missing?
Adjusting the has no effect.
Please note that to originally get this running I had to adjust the base config to even connect to the database (vendor/doctrine/doctine-orm-module/config/module.config.php) i.e. the script obviously did not pickup any config from the skeleton MVC application.
I've tried installing ZFTool which has it's own issues when running from command line so this was a dead end for me(in zf3 anyway)
So far my options seem to be:
Find a way to generate:entities to run via command line with the current tools
Find a way to run the ORM tool with context of the ZF3 skeleton app trying to utilise the config
Stick with at least the 'annotation' classes generated above and manually add getters/setters and continue... (not what I want to do)
Dump doctrine and use /Zend/Db libraries with either TableGateway of customer Select() objects using DBAdapter.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html#configuration
https://samsonasik.wordpress.com/2013/04/10/zend-framework-2-generate-doctrine-entities-from-existing-database-using-doctrinemodule-and-doctrineormmodule/
How to generate entities from database schema using doctrine-orm-module and zf2
creating mapped ORM Entities without the cli?
Any assistance with a breakdown of steps would be great. Docs and posts online are not effective for me, possibly because I'm missing some simple configuration or directive...
Can you please try to run the below command and see if it helps. I have made minor modifications to it.
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate:entities ./EXPORT --generate-annotations=true
If the above does not work, you may give this one a try too, just in case.
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate:entities ./EXPORT/ModuleName --generate-annotations=true

Cleaning a Symfony project and installing a new bundle

Im very new to Symfony, yet Im already familiar to the MCV model, after a while reading the documentation I have now the standard edition of Symfony 2 running on my server.
Fresh start
As you know Symfony comes with a hello world app and some welcome scripts, how can I clean all of that?, even better, is there a way to install a Symfony project without the examples bundles?
Data base structure restore
In the past, every time I developed an app with database, I created the structure of the tables using phpmyadmin and then an initial php script that restore that database if I need to install my app on other server. Working with Symfony is quite different, I read that it use doctrine and the Symfony core restore the database format using some internal files. I have a bundle that I need to install, I know that this bundle/app use MySQL, I set the config file with all my MySQL information but I dont know how I can run a sript that will restore a clean database needed for that particular app. I know it has something to do with php app/console schema:update but not so sure.
Thanks for any orientation.
how can I clean all of that?, even better, is there a way to install a Symfony project without the examples bundles?
There is no way to do that in Symfony2 and it looks like it never become a part of the Symfony2 project:
"I think that we need clear instructions on how to remove the Acme demo code by hand. I'm -1 on added a CLI command to do that automatically."
-- fabpot on github
The things you should do:
Remove the src/Acme directory
Remove $bundles[] = new Acme\DemoBundle\AcmeDemoBundle(); (line 25) in app/AppKernel.php
Remove the related routes from app/config/routing_dev.yml (_welcome, _demo_secured and _demo)
You can remove everything in the security: key in app/config/security.yml and place enabled: false in it. However, this isn't needed and it works like a nice boilerplate for your own security
Your other question: Symfony2 don't have a 'view' layer. You can choose to use whatever you want. But Symfony2 included 2 ORMs in their framework by default, Doctrine and Propel. You can read more on how to work with these libraries in the documentation: Doctrine and Propel.

Zend 1.11 and Doctrine 2 Auto generate everything needed from already existing database

I am new to ORM and I am really keen to learn it. I successfully managed to install all classes and configurations for Doctrine 2.1 with Zend 1.11.x by following this tutorial.
http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/ Which uses Bisna plugin and doctrine scripts.
Now my problem is he is clearly explaining how to create entities and tables through doctrine classes but do not explain how to auto generate the proxies and repo classes from already existing database which helps me to select, insert and update. I always create my databases using MySQL Workbench.
I also followed the below tutorial as well
http://www.zend.com/en/webinar/Framework/70170000000bSrG-webinar-zf-v-1-doctrine-v-2-20101214.flv
My database is so complex with relationship flowing across every possible way. If I follow the steps which is explained in these tutorials I will never complete my project. Can any one please explain how to start using Doctrine after configuration. Considering I already have a database and my Model folders are empty. I have my folder sructure as below.
C:/zf/library/Doctrine
C:/zf/library/Symfony
C:/zf/library/ZC -- (my model which should contain the proxies and repo of Doctrine. At the moment it contains nothing.)
C:/zf/library/Zend
C:/zf/scripts/doctrine.php
Please help me!
I posted this same post yesterday and no one replied to my post. Please let me know if you need anymore information from me.
Thank you,
Karthik
According to Doctrine you should create your entities first yourself and then create your database schema from these entities.
But because you already have a database you probably don't want that. It is possible to convert your database to Doctrine2 entities in PHP, XML or Yaml.
You should take a closer look at the commandline tools Doctrine offers with the Bisna glue because there you can generate a lot of stuff.
To generate your entities FROM your database consider the following command:
php doctrine.php orm:convert-mapping --from-database php ../library/Application/Entity
You can also define the namespace and a base class which your entities have to extends with: --namespace=namespace and --extends=class.
Doctrine2 warns you to convert your database to entities because not everything could be auto detected or supported. For instance ENUM datatypes are not supported by default in Doctrine2 so converting your database will raise an error.
It's a good idea to check all your entities especially associations before you use them. Hope it helps you.
If I understand your question correctly, you have your entities already configured and need to auto-generate your proxy and repository classes.
Both can be created using the following Doctrine CLI commands from your application's root directory:
php scripts/doctrine.php orm:generate-proxies
php scripts/doctrine.php orm:generate-repositories library/
If you're looking for a way to auto-generate your entity classes, unfortunately I don't think a solution is available for this yet.
A support rep at ORM Designer said they are "working on" this feature and that it is "very demanded." Here's hoping it will be included in ORM Designer 2.0 since there is generally a lot of repetitive work involved in coding/mapping entity classes that could likely be automated.
You can use the orm:generate-entities command if you provide mapping information in either XML or YAML format.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/tools.html#entity-generation
For development, set proxy generation to be automatic in your config, otherwise, use the orm:generate-proxies command.
Unless you need to customise your repositories, generic ones are created in the entity manager when requested. To specify custom repositories, simply use the repository-class entity mapping attribute.
See http://www.doctrine-project.org/docs/orm/2.1/en/reference/xml-mapping.html#defining-an-entity for an example

Standard idiom for adding new models to an app built on Symfony + Doctrine

What is the a standard way of adding new model to my app built on Symfony + Doctrine while maintaining all previous models and their meta-data (like relationships).
What am I really looking for: A command / procedure that will be equivalent of ./script/generate model FooModel in Ruby on Rails (which does not have any sort of reset db / reset models while generating)
If these two are different things, and I am chasing the wrong ghost (I would like to think I am not), please let me know.
EDIT: Updated the question.
You shouldn't be overriding the base classes, as these will be mostly be auto-generated whenever you do build:all or doctrine:build-model etc. Use the classes generated in the lib/model directory eg YourModel.class.php if you want to add new methods etc. Then your new models will be generated alongside your existing ones.
Standard process is to add the new model and any relationships it requires to schema.yml
Then do ./symfony doctrine:build-all (or :build --all for symfony 1.3/1.4)
As richsage says, you shouldn't be editing the base classes, so this operation is totally non destructive.
Doctrine also has functionality for migrations so that you an update the database schema easily as you deploy the new code into production:
http://www.doctrine-project.org/documentation/cookbook/1_0/en/symfony-and-doctrine-migrations
Newer versions of doctrine (1.1 +, symfony 1.3+) include the generate-migrations-diff task, which can create migrations for you. This is covered very well here:
Extra changeColumns in Doctrine generate-migrations-diff
[edit: the author of the question above has copy/pasted it below as well now]
The generate-migrations-diff doesn't diff two different yaml files. It actually compares your models and your yaml file and then generates a migration based on the differences. If you start from a db that is in sync with your yaml and classes, your workflow to make schema changes should be:
Change yaml file
Run generate-migrations-diff to diff your current (changed) yaml with your (unchanged) models. This will generate a migrations file in your doctrine/migrations directory (or whatever migrations_path is set to in your doctrine config).
Run migrate to run the migration created in step 2 and modify your database
Run generate-models-yaml to generate new classes based on your yaml file. These go where you've specified your generated models go (models_path in your doctrine config).
Run generate-sql to generate a SQL file. This will go where your doctrine sql_path config is set to.

Categories