is it possible to generate models from db, but only specified tables in doctrine?
I can't locate a method in Doctrine_Core class for this.
Thanks.
From the command line should work:
doctrine-cli generate-models-yaml path/to.yml
Look into the Doctrine_Cli class, it should have specified method.
Related
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.
Is it possible to generate a Propel migration file without modifying the schema? I only need to insert values.
Thanks.
You can generate a blank migration like this:
propel migration:create
Yes. Just call the diff target in the Propel 1 command, which will create a migration class.
(Propel on MySQL for me always creates spurious differences e.g. between FLOAT and DECIMAL, which I delete manually. I don't know if it would refuse to create a class if no differences were found).
Propel migration system is not able to insert values, only update your database structure when you update your schema.
If you use Symfony2, you can use the command propel:fixture:* to insert data for test purpose.
When I add a new table that has some relations to my database and then run Add-Migration I see that code is generated in the Up method to add the table and its relations.
However, I prefer to define the relation using the fluent API in the OnModelCreating method. How do these two methods interact? Can I delete the code from the Up method that defines the relation for instance?
Each of them has completely different purpose:
OnModelCreating is used for inline fluent-API definitions of your model. These definitions together with default conventions, data annotations and configuration classes forms the complete definition of the model.
Explicit migration defines what must be done to database to migrate it to the form required by your current model
Now, how those two relate? Migration has two inputs which are used to generate migration code (Up and Down methods). One input is the last migration record stored in __MigrationHistory table in the database. This record contains serialized model representing the database. This input is optional because first migration must work without it. The second input is mandatory - it is your current model which is retrieved by executing the code in your current assembly => Add-Migration will execute your OnModelCreating to get the current model and compare it with the model retrieved from the database. The result of comparison is content of Up and Down methods in the explicit migration.
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.
is there any way to retrieve mapping table name for an Entity in Entity-framework in program? I know you can use .ToTraceString() to get the command text and then extract the table name, but ToTraceString() method is very slow. is there any other way like using ObjectContext.MetadataWorkspace?
Thanks
In the EF v1.0 ToTraceString is the only way.
Thanks for your answer, but the code above works for LINQ to SQL, it does not work for Entity framework, because the entity classes in EF dont have table name attribute.the mapping information is in edmx file in XML.
You can use <dataContext>.<EntityName>.CommandText property to get the name of the Entity. Maybe that helps...