I'm trying to import an existing table in my Database using Doctrine's reverse engineering, but this table I'm importing doesn't have a primary key.
I know Doctrine doesn't allow to import tables without any primary keys, but I would like to know if there's a possibility to do it. I've been trying to edit Doctrine's code to allow it, but without any results for now.
Does anyone know a method? Or, if it's not possible, what are my options? I can't edit the database because it's not mine, so I need to manage this to import the table without "touching" the database.
So after trying a lot, I'd found a couple methods that can solve this. I will explain them to everyone who finds themselves in the same situations as me:
1st method: The one #mulquin said. This method solves the problem by exporting the table and then adding, manually, a primary key using code. Once this is done, it can be imported using Doctrine.
2nd method: Found by me. Creating the table using code directly and connecting it manually instead of using make:entity. You should create the two files, in Entity and Repository, and then write the code to make it match the table in the database, using #ORM\Table, #ORM\Column, etc. and writing everything just like Doctrine would generate it. You can take an existing entity generated by Doctrine to make sure you're not missing anything.
Related
I am trying to figure out how best to modify a MySQL Table's existing Column using the CakePHP Migrations plugin. I do not need to add or drop the column, I simply want to modify a string column's length.
Currently the column is defined as a varchar(50); I am repurposing the column and wish to define it as varchar(2000).
The goal of the migration is to be part of an automated deployment taking place on a standard CakePHP web app installation on a typical web server.
Near as I can tell, it looks like the only way (other than an ALTER statement) to accomplish this using the Migrations Plugin would be to:
rename the column
add the new column
Move/copy the existing data to the new column
drop the old column
Perhaps I have missed the discussion in the documents and countless tutorials and how to's out there on a better way to accomplish this, but this seems like a cumbersome and self defeating method.
I have been through both the CakePHP Migration Plugin's documentation and the Phinx's documentation but am failing to see the recommended method for this change. I appreciate any input for this.
Unfortunately the Phinx docs aren't that complete, there seem to be various undocumented methods, like the one you are looking for: \Phinx\Db\Table::changeColumn()
https://github.com/robmorgan/phinx/blob/v0.4.6/src/Phinx/Db/Table.php#L392
https://github.com/robmorgan/phinx/issues/682
The following should work
$table = $this->table('table_name');
$table->changeColumn('column_name', 'string', [
'limit' => 2000
]);
$table->update();
I am currently working on a huge refactoring project. We have taken over a classic PHP/MySQL project, where most code is procedural, duplicated, and there is very little hint of an architecture.
I am planning on using Doctrine to handle our Data Access, and have all of my tables mapped to entities. However, our MySQL tables are largely messed up.
The table I am currently working with has over 40 columns, and is not normalized by any means. A quick example of what we have:
Brand
id
name
poNumber
orderConfirmationEmail <---- these should go into a BrandConfirmations entity
shippingConfirmationEmail <-----
bill_address <---- these should go into a BrandAddress entity
bill_address2 <-----
city <------
.
.
.
Ideally, what I would like to have is for Doctrine to pull out the fields that reference different Entities, and actually put them into those Entities. So for instance id, name, and poNumber would get pulled out into a Brand entity. orderConfirmationEmail and shippingConfirmationEmail would get pulled out into a BrandNotification entity. Next, bill_address, and the rest of the address fields would get pulled out into a BrandBillAddress entity. Is there a way to configure Doctrine to split the table into these models for me, or do I have to custom write code myself that would do that?
If I do have to write the code to split this table myself, do you have any resources or advice that tackle a similar issue? I haven't been able to find many yet.
The latest version of Doctrine 2 supports what they call embeddables: http://doctrine-orm.readthedocs.org/en/latest/tutorials/embeddables.html. It may solve some of your problems. However, it requires D2.5+. Currently, S2 uses Doctrine 2.4. You could experiment with using the very latest doctrine.
What you can do is make your domain models (entities) act as though you had value objects. So $brand->getOrderConfirmation() would actually return an order confirmation object. You have to do some messing around to keep everything mapped to one table and you might be limited on some of your queries but it's not that hard. The advantage is that the rest of your new applications deals with proper normalized objects. It's only the internal persistence code that needs to get messy.
There are quite a few links on this approach. Here is one: http://russellscottwalker.blogspot.com/2013/11/entities-vs-value-objects-and-doctrine-2.html
Your best bet of course is to refactor your database schema. I like to do kind of a raw dump of the original database into a yaml file with the desired object nesting. I then load the yaml file into the new schema. If you are really lucky then you might even be able to create new views for your existing application which will allow it to keep working in parallel with your new application.
So as title pretty much describes, I'm trying to utilize phinx for database migration, but I have AUTO-INCREMENT column, which is not used as the primary key. As far as I see, this can't be done with phinx.
I realize, that I could probably do without that column altogether, but it's the part of the huge legacy code and, at the moment at least, I don't have the time to refactor entire app to ensure that column is not used anywhere. If my conclusion is correct and phinx is not able to achieve this, I'd appreciate some alternatives, possessing described functionality
You can use the up and down phinx method to create this table using execute fontion with pure sql.
See this
I have two tables in DB (topic, topic_content):
what kind of entities i should create for symfony2?
I think, i should have something like this in my symfony structure (Entities/Topic.php, Entities/Topic_content.php) help me please..
Yes, you would create Topic and Topic Content. And likely also a User Entity (because user_id looks like a foreign key).
However, the idea in Symfony2 is to approach the application from the Model site instead of the database site. Quoting https://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started-database.html:
Development Workflows
When you Code First, you start with developing Objects and then map them onto your database. When you Model First, you are modelling your application using tools (for example UML) and generate database schema and PHP code from this model. When you have a Database First, you already have a database schema and generate the corresponding PHP code from it.
For database first, there is a generator that will derive objects based on your schema:
https://github.com/beberlei/DoctrineCodeGenerator
The recommended approach though is to have Doctrine generate the db schema from your Entities.
Quoting Getting Started - Generating the DB Schema
Doctrine has a Command-Line Interface that allows you to access the SchemaTool, a component that generates the required tables to work with the metadata.
It requires some setup, which is explained in the guide. Once you have that, you simply tell Doctrine to generate or update your schema, whenever your object structure changes.
What I'm trying to figure out is how to add new fields to a table, using Symfony2 with Doctrine2.
I used this to initially create the Entity:
php app/console doctrine:generate:entity --entity="MyMainBundle:ImagesTable" --fields="title:string(100) file:string(100)"
And I used this to create/update the tables on the database:
php app/console doctrine:schema:update --force
Now if I wanted to add new fields to the ImagesTable entity, is there an easy way to do it using the console, or do I have to manually edit the entity. I am just using 1 entity as an example right now, but in reality, there are many entities I'd be changing; so, there has to be an easier way to do it.
I've been manually editing them to create relationships, so if there is an easier way to do that as well, that'd be great.
I remember this being a lot easier with Symfony1.4 - all I had to do was create the database/tables using phpMyAdmin, and Symfony was able to generate the models with no issues.
I really hope I'm missing something here, because this won't work if I have to manually edit every entity for every change.
Doctrine generator commands are intended to help the developer to quickly prototype an idea. They generally don't produce production ready code, and the code needs to be checked to see if it contains what you want.
You can still create your model in phpmyadmin and use Doctrine reverse engineering tools, but it also doesn't produce production ready code, only intended to use in prototyping.
Creating database/tables beforehand doesn't really work well with Doctrine2, as the underlying relation between tables may not be the same as the relation between objects of your model. The whole point of ORM is to think in classes and letting Doctrine do the rest of the work for you.
Doctrine is not intended to write your entities for you, it gives you tools to build your data model, which you use to code your model in Php.
If you don't like to code your entities by hand (which is what all developers using doctrine does), you may want to have a look at RedbeanPHP, a zero-config ORM framework for PHP. It creates the database tables, columns, indexes on the fly depending on the data model you use.