Importing database structures in Symfony - php

As I said many times I'm new in Symfony2 framework and I ask about it a lot. Here is my new problem. I'm trying to import my mysql database into my symfony application. I'm following this tutorial http://www.sitepoint.com/building-a-web-app-with-symfony-2-bootstrapping/
This is the code that I run;
php app\console doctrine:mapping:import
This is the error that I get;
[DoctrineORMMappingMappingException] Property "bookid" in "BookHeadline" was already declared, but it must be declared only once
In the link there is a comment about this issue and it says remove the FK 'bookid'. I tried it but even the sql query did not work. Also this is the sql query.

Can you paste the table structure for that entity?
Also take a look at Symfony2 docs about importing your database

Related

Map database views in Doctrine Migrations Bundle

There does not seem to be proper documentation available on how to configure and use database views with the doctrine migrations bundle.
One probably is not able to map SQL statements which will end up creating/updating a database view (from the sql given somewhere) when migrations:diff and migrations:migrate are run.
If an entity is mapped to a database view with the #table(name="view_name") markup, it ends up causing an error / new table being attempted, instead of understanding that its a database view being used.
Is there a solution? Am I missing something?
I'm not sure that doctrine can get out of the box views. For all I know, you'll have to cheat.
Or:
I think you have to write the migration script yourself.You can generate an empty one and then write the create-statements into it.
In the repository you integrate native sql. The result you map to your entity or DTO.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/native-sql.html

How to generate a table from an entity in symfony2?

This might be a dumb question but I'm clueless as to how to go about this.
I've got the entity "MailEntity". However, My database does not contain a table that corresponds with this entity yet.
Question
I would like to know how to generate the table that corresponds with the Entity I created. I've been looking for this but whatever I search on google, the same results seem to pop up constantly.
Update
I've come to know that I can achieve what I want by doing php app/console doctrine:schema:update. Adding the --dump-sql parameter will dump the sql before the schema is actually updated.
However, I would like to do this for a single Bundle. A single Entity would be even better. I just want to create a table from the MailEntity without changing anything else in the database.
If you already have the entity classes ready to go, simply run:
app/console doctrine:schema:update
This should list all the queries doctrine needs to run to create the tables. Run the command with the --force flag to execute them. It's documented here
It might be worth looking at the doctrine migrations bundle, which allows you to diff the current DB status with the entities, and generate migration scripts based on that diff see the docs
You could use php app/console doctrine:schema:update.
This command will tell doctrine to execute the necessary SQL so that your database reflects your doctrine schema. Add --force to actually execute the query.
If you want to check the generated SQL command first, you can use --dump-sql. It will print what would be executed with --force.
Related documentation you may read for further information
EDIT : If you want to create the table for a single entity, the easiest way is to dump the generated sql with --dump-sql and then extract the line responsible of your MailEntity table creation, then execute it manually.
I would not recommend to do it this way though, you should let Doctrine synchronise your database on its own. This kind of tricks may result in errors in your database.

Doctrine [Semantical Error] Error: Class xxx has no field or association named yyy

For last few days I'm experiencing following issue with doctrine - since I am not allowed to paste any source code, I'll try to describe briefly:
I am using doctrine orm and I need to add a new column to an existing table in DB - mapping between DB and entities is done via xml mapping file - here are the steps I've proceeded:
I've added into the entity file - let's call it Entity.php - new field for that newColumn
I've added info about this newColumn into the XML mapping file as new XML element 'field'
I've executed doctrine command to change the schema of the DB based on edited mapping file
I've updated the query in EntityRepository.php file to include this new column.
When I then run the application, I am still getting this error:
[Semantical Error] Error: Class Entity.php has no field or association named newColumn
So, if I am understanding this properly, it is saying that in the Entity.php is not field newColumn to which should be the new DB column mapped.
But that is not the case, since it was the very first step I've done.
I've already tried this:
Checked there is no typo in name of the newColumn across all files
Checked the field in Entity.php has proper access modifiers - i.e. it is not private
Cache was cleared for the case that some bad version of Entity.php was stored
I've restarted apache server which the application runs on
Check your metadata cache. If you're using some external caching mechanism (like Memcached or xcache) it's probably shared across your vhosts. If one vhost populates the cache with its own mapping metadata (after apache restart), second vhost just uses it and doesn't care about different .dcm.xml mappings.
If it's your development server/vhost, it's usually much better to disable ORM caching at all (config/autoload/database.local.php).
Maybe my problem and solution would help somebody.
/* *
* #ORM\Column(name="user_id")
*/
protected $userId;
No, there was no typo in variable name. Access is correct and everything looked to be fine.
Some of you probably already see the problem.
Yes. It's /* * instead of /**. I took me about hour to find it :]
What was strange - it was working in join, but not when I have used it in where.
run this command
php bin/console doctrine:cache:clear-metadata on both APP_ENV=prod and APP_ENV=dev
Have the same problem. The solution was to replace in query condition like
LOWER(l.bank_title) LIKE :search
with its camelCase variant:
LOWER(l.bankTitle) LIKE :search

Rebuild model without loss data in MySQL for Symfony

What is the best way to rebuild a model without loss data in MySQL for Symfony?
What if I have a big site, and a lot of data in the database and I would like after six months to add few new fields to database?
You can use migration.
Doctine manual
Symfony task for migrations
Slideshare presentation
Slideshare presentation
So you need write migrations, migrate, and build your models, forms, etc.
I suggest you use #denys281 for Symfony1.4 ....in Symfony2 however its VERY simple ... just use the command :
php app/console doctrine:schema:update --force
It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and generates the SQL statements needed to update the database to where it should be. In other words, if you add a new property with mapping metadata to Product and run this task again, it will generate the "alter table" statement needed to add that new column to the existing product table. So it doesnt remove any data
There is also a DoctrineMigrations bundle for Symfony2 if you fancy that route -> http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

Doctrine-CLI database creation issue

I have Doctrine setup in my Zend Framework application and I built my schema YAML file. But when I tell Doctrine to build the tables it says it does but it doesn't actually make them. It creates the models, and will create the DB but it will not populate the DB with the tables and throws no errors. Does anyone have a guess or know why this is not working?
Thank you.
The issue was that I had a model I created for testing already in the folder. Evidentially Doctrine thought that was a bad idea and then didn't make the tables in the DB.

Categories