Symfony doctrine doctrine:mapping:convert filter is not working - doctrine

I need to import database structure from existing database.
I tried with
doctrine:mapping:import "App\Entity" annotation --path=src/Entity
doctrine:mapping:convert xml src/Entity --from-database --force
And I have this error :
Table [tableName] has no primary key
So I would import only tables than I need for my project following this
topic : Generating a single Entity from existing database using symfony2 and doctrine
and I have the same error.
Have you got a solution for me please?
Thanks in advance.

You can add a 'schema_filter' on the Dbal to only have the doctrine:mapping:import 'see' (or avoid reading from) specific table names.
doctrine:
dbal:
connections:
user_db:
# only use for `bin/console doctrine:mapping:import`
schema_filter: /^(oauth_)/
orm: # configs...

Doctrine requires all tables to have a primary key.
If you can't add primary key to existing tables check here:
Symfony Doctrine Models for Entity Without Primary Keys
https://medium.com/#joaoneto/solves-doctrine-orm-error-with-tables-without-primary-key-on-mysql-when-mapping-the-database-1ce740610b51

Related

How to add a index on a constrained foreign key In Laravel for PostgreSQL

I'm seeding a CSV file with about 2.000.000 entries into a PostgreSQL database, and some fields need to be dispatched in specific tables with relationship.
For the relationship, I'm using this in my migration:
$table->foreignId('foo_id')->constrained('foos');
When trying to improve the bad seeding performances with PostgreSQL, I realised there's was no index on the profession_id column, so I added one like this:
$table->foreignId('foo_id')->constrained('foos')->index();
But I don't see it in the indexes list of my main table, why ? Also, I don't get any error at migration refresh.
I don't have this problem in mysql and indexes are added but not in PostgresSql
Is it possible to add an index to a foreign key ? If yes, how ?
In migration :
$table->foreignId('foo_id')->constrained('foos')->index();

using laravel migrations to access tables already created

I have some tables that are already created in the database. The migrations seem to require that you first destroy the table and then create it. From what I understand, the creation process generates a "model" at the same time as well as "getters" and "setters" for the fields.
The problem is that I have a database full of tables already. I would like to access the tables that have already been created using Laravel as an interface. Is there a way to generate a model, getters and setters for tables that already exist?
How can I best do this?
TIA
The default model generator doesn't create any getters or setters. All it does is create a basic model class with the correct model name based on the table name. You can create the models without actually creating the migration. The command is php artisan make:model ModelName.
So the answer to your question is no, there isn't any. You need to manually create each model and add the methods yourself. You might try your luck with some laravel package which might do this for you.
Note : Laravel Eloquent is an ActiveRecord implementation. You don't need any getters or setters to work with the table columns. Even a completely empty model with the correct name to match the table would be enough to start using Eloquent.

Update Schema via an Eloquent Model, Laravel 5.2

i'd like to know if it's possible at all to update the table schema in a Migration through a specific Eloquent Model, or if i actually need to pass in the name of the Table and Connection every single time.
I ask this because in my case this requires an additional configuration file that my package must publish to the end users, apart from the already required table Eloquent model (which is used for other purposes)
You can update schema later and add or drop columns and/or index.
To do this you create a new migration and add the changes there. It will change the table over your previous version.
More info in Laravel documentation.
For renaming the table
Schema::rename($from, $to);
https://laravel.com/docs/5.2/migrations#renaming-and-dropping-tables

Laravel 4 database constraints

I defined relations between different models(belongsto(), hasmany()).
do I have to add the foreign key constraints also in the database?
yes, you need to add the FKs in a separate migration after all your table migrations.
Refer to the docs for any additional details.
http://laravel.com/docs/schema#creating-and-dropping-tables

rails 3:how to generate models for existing database tables

I've configured my database.yml to point to my existing mysql database
how can I generate models from it?
rails generate model existing_table_name
only gives an emty model..
You can try Rmre. It can create models for existing schema and it tries to create all relationships based on foreign keys information.
A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type:
ModelName.find_by_name('foo')
Given a name that exists in the DB, you should see results.
Rails doesn't infer relationships though, but if your database follows Rails conventions they are easily added.
Update
I've noticed this particular lack of explicitness ("magic") is a source of confusion for newbies to Rails. You can always look in schema.rb to see the models and all the fields in one place. Also, if you would prefer to see the schema for each model in the model file, you can use the annotate_models gem, which will put the db schema in a comment at the top of the model file.
Your answer is:
$ rake db:schema:dump
That will set a new db/schema.db to create a schema of your DB.
ActiveRecord doesn't parse a schema definition. It asks the DBM for the table defs and figures out the fields on the fly.
Having the schema is useful if you are going to modify the tables via migrations.
Schema Dumping and You will help you dump it to use as a reference for building migrations.
ActiveRecord makes some suppositions about the table naming and expects an id field to be the primary key with a sequential number as the type. Having the migrations would help you to refactor the tables and/or fieldnames and types, but you can do those same things via your DBM's command-line. You don't really have to follow ActiveRecord's style but doing so helps avoid odd errors and lets AR infer things to make your life easier.
Could try Magic Model Generator
Take a look at rare_map gem.
https://github.com/wnameless/rare_map
It works both on Rail 3 and 4.

Resources