Doctrine2 does not generate getter/setter - symfony

I'm trying to reverse engineer a db schema using Doctrine.
If i do
php app/console doctrine:generate:entities NS/MyBundle/Entity/MyClassName
it successfully creates a class with private properties taken from DB, but there are not getter/setter method, so that i cannot use it. What am i doing wrong?

Please follow How to generate entities from existing database? As I don't think what you are doing is really going to work.

Related

Generate entity with PHPCR

Earlier, I used pure essence created a symfony that app/console doctrine: generate: entites, but at the moment, it is necessary to deal with the Symfony CMF. Could you write commands that you need to do in order to register the entity and generate getters and setters, which can juzat in the controller. For example, I need to create a username field, a password. Thanks for the help
For Doctrine PHPCR-ODM, the generator command has not yet been contributed by anybody. There is a discussion in the github tracker and it would be great if somebody ports that code from doctrine ORM or writes a generic generator in the process.
Luckily, an IDE can also generate getters and setters, and the mapping is not that complicated to write. You find the documentation in the Doctrine documentation.

Symfony2 Entity with DBAL (No ORM)

I'm working on a SF2 project where I can't use Doctrine2 as ORM, meaning that I already have a database with tables and data. I have to use plain SQL in my controller (I'm currently using DBAL to do that), and I have to create object in order to represent things.
When I used to work with Doctrine2, I create Entity by app/console doctrine:generate:entity and Doctrine2 is handling the whole stuff (update, persisting...)
But now, as I'm using DBAL, how can I create object (can I call it entity even if i'm not using ORM?) to fit my need ?
I was planning to do like usual : create an entity folder in my bundle with entities as objects without the ORM annotations, and create a method where I retrieve data from database using SQL (result of the query in an array) and hydrating it using getters/setters from the object.
Is it a good idea or do you have a better solution ? I'm beginning with SF2 and I read that some people create a service to retrieve data and then using data transformer to transform data into the object.
Thank you.
You are describing Active Record pattern. For this purpose you can use Propel that has native integration with Symfony. Read about it on Symfony's official documentation.
Also I would recommend you to use ORM. You can set your mapping with existing tables as you want: you can even omit some fields if you don't need them in entity. And Doctrine ORM will do all the hard work for you.
You can still use ORM without changing your database by creating classes from your database. You should read this in the symfony documentation

Doctrine does not see my custom Entity

I'm new to Symfony/Doctrine. I've create an entity "Merchant" based on a table I had created. It worked perfectly.
Now I've created an entity "Provider" and would like to generate the table for it.
I've used doctrine:mapping:import and it only imports the Merchant one. The Provider is nowhere to be found.
Thanks!
Edit : I've tried using "shema:update" but it returns "Nothing to update - Your database is already in sync".
Make sure your "Provider" class "name" or "namespace" is right and also do't forget to use
use Doctrine\ORM\Mapping as ORM;
if your entity class is in the annotation format ..
Sometimes may be it's file name is "Provide" rather than "Provider.php" and may be class name is not as per its namespace or also you have forgotten to use namespace ..
If you are trying create database schema based on your entities you should use different command.
Following one will drop and create the schema (pay attention that this will remove all your database data):
app/console doctrine:schema:drop --force
app/console doctrine:schema:create
Or you can just update existing schema:
app/console doctrine:schema:update --force
In addition to above command you can just look at sql changes that doctrine is going to apply to your db, without applying them:
app/console doctrine:schema:update --dump-sql
Make sure your database config file has your database name that matches your Entity's schema name.
If they are mismatched, no differences are notec, no SQL gets created
I had same problem. Console restart worked for me.

Symfony Doctrine Mapping:Import & Table Association

I am working with Symfony 2.3 on a new project using an existing database with numerous associations - many-to-many, one-to-many etc. During my initial import last week, I found somewhere in the docs stipulated that a doctrine:mapping:import would generate orm.yml files of my database which it did without a hitch. However, I also see that only ManytoOne relationships are generated in the yml files ... not any other kind of associations.
My statement was:
$ php app/console doctrine:mapping:import –em=buv DBImportTestBundle yml
Also, I did a generate entities to create classes and basic CRUD for each table using:
$ php app/console doctrine:generate:entities DBImportTestBundle
This also worked EXCEPT that I do not see any annotated associations generated in the doc blocks for any of the entity properties.
I'm looking through the docs but do not see any specific information on the exact requirements for associations on imported dbs. It could be I'm not looking in the right place.
I'm trying to determine the most efficient way to maintain my db schema within symfony/doctrine ... My understanding was that I would need to explicitly define certain associations manually but I'm not sure what the exact requirements would be OR if perhaps I'm simply not passing in the correct arguments to create my annotated associations via generate:entities.
Can someone point me to any docs that refer to what I'm talking about or explain the proper approach to defining complex associations within doctrine? Thank you.
$ php app/console doctrine:mapping:convert annotation ./src to generate entity classes with annotation mappings, before running :
$ php app/console doctrine:generate:entities DBImportTestBundle
there is a cookbook for that

Namespace alias for Doctrine

I became responsible for a large legacy web application and I am trying to slowly refactor it into the Symfony2 framework. The first thing I have done is to include Doctrine.
I have installed Doctrine with the help of Composer and set up a bootstrap file for it. My entities, to avoid future complications, already follow the namespacing scheme Company\BundleName\Entity\Object. The following works:
$em->getRepository('Company\\BundleName\\Entity\\Object')
->find($id)
;
I was unable to find any reference of how to inform Doctrine of namespace aliases as Symfony2 does, so I can write
$em->getRepository('CompanyBundleName:Object')
->find($id)
;
instead. How can I achieve that?
There is an easier way now:
$config = Setup::createAnnotationMetadataConfiguration(...);
$config->addEntityNamespace('CompanyBundleName', 'Company\BundleName\Entity');
will do what you want. It took me several hours hunting to find this! Its not in the docs anywhere I could find.
The functionality for this is set up in Symfony2 by the DoctrineBridge bundle, specifically the getMappingDriverBundleConfigDefaults function.
If you want to reflect this functionality without Symfony2, you'll need to extend the Doctrine entity manager and generate the prefix yourself in the getRepository function. It is not part of the Doctrine system.

Resources