I'm struggling with the connection/relation from the main class/table to a related class in symfony 1.4 with doctrine. The relation is
$relatedClass->identifier == 'e'.$mainClass->id;
I know that I could realise it easily without this prefix but I am working with an existing database and existing subcomponent, so I can't change this relation.
Any ideas? Thanks!
You cannot implement this is a relation in Doctrine because it is not a true foreign key.
If you cannot change the existing column, is it possible to create a new column? If so, write a migration to add a column to the relatedClass table with a proper foreign key relationship.
I finally made a 'pseudo relation' like this:
$results = Doctrine::getTable($relatedClass)
->createQuery('alias')
->addWhere('alias.identifier=?','e'.$event->getId())
->execute();
It has several advantages:
no changes of the existing tables necessary
no adding of a useless column to a already huge database
It is not elegant but it is the solution with the most advantages so far.
Related
As you know, rails will add an id column as primary key implicitly when creating a table using migration.
My question is, if I want to add another column implicitly, is it possible? how?
I believe you will want to look into custom generators, particularly overriding the existing templates:
http://guides.rubyonrails.org...
Maybe this is the file you want to copy/paste into your project directory and then edit?
http://github.com...
I'm looking to rewrite my project in Rails. It's currently written in PHP (CodeIgniter), I've come to the point where I'm writing more libraries and core extensions than I'm writing new code. I've been looking at some Rails tutorials and I'm liking what I see so far (even though I feel you have less control of what's been passed around). However it seems there's little information out there (maybe I'm not looking in the right places) on database tables without models.
For example, I need a table called user_verification_token
CREATE TABLE IF NOT EXISTS `user_verification_token` (
`user_id` int(11) NOT NULL,
`token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`is_used` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
It doesn't make sense for me to create a model for this, right?
First question: How do I generate a migration to create a table alone without any model?
Second question: When this table is filled with data, how do I associate with it. e.g. Associate it with the User model (if possible)/a user object so I can find a user by a token (provided is_used = 0) and return the user's data?
I apologize in advance if this is a newb question, I can do all these comfortably with PHP but I don't know how to do this on Rails.
You need a model for this table. It does make sense for you to have a model for the user_verification_token table. In order to get the associations and all the other functionality you'll want from this data, you need to create a model and the associations for it.
To answer the first question: run rails generate migration your_migration_name
For the activity_type table you mention in the comment on the original question, you might be interested in the RailsLookup gem to help automate some of the work required to make good use of lookup tables in Rails. There is a blog post describing it and the source can be found on GitHub
Your current scenario does require a model, although when you need to store data like categories e.g Sports(football,tennis,cricket,swimming,basketball,etc), you can store them as constants in your config->initializers->constant eg. SPORT_CATEGORIES = [["Football","football"],["Tennis","tennis"],etc], alternatively if you have more cols to store you can create a model then create the default rows as you would in a php .sql file but in ruby of course :) For example:
sport_categories = [
{:category => "football", :category_type => "manly" },
{:category => "Tennis", :category_type => "manly" },
etc
]
sport_categories.each do |attributes|
Model_name_here.find_or_initialize_by_category(attributes[:category]).tap do |p|
p.category_type = attributes[:category_type]
p.save!
end
end
Then you run rake db:seed.
I will suggest you to create model for this, as you will have complete access to user_verification_token table via UserVerificationToken model which will be associated to user.
Create a model because all the validations can be done in a model.
eg:validates :user_id. So there is no need to include those constraints while creating the table
I'm currently looking at migrating an existing system (written in spaghetti PHP) over to rails. The problem is, it has to run off of a live database. A lot of the ID columns on these different tables aren't named id. For instance, the customers table has an ID column called Customer_ID. Upon looking, I just realised that rails does infact seem to find by the primary key instead of a specific column called id.
Will I face a lot of problems later with the naming of these ID columns, specifically in stuff like relationships?
After v2.3.8, set_primary_key :col_name is deprecated.
self.primary_key = 'col_name' is recommended.
http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html
Change primary key attribute in model by using
set_primary_key :col_name
I have a previously separately managed sql file containing rather simple but large database. Would there be a way to import this sql file and generate ruby code as models using this data as a starting point for my future development?
Thank you for your help!
Yes!
It will take some work!
And you'll need to post a WHOLE HELL OF A LOT more detail to get more than that. ;-)
Taking a stab:
Rails can use legacy databases with a lot of effort manually specifying foreign key columns, table names, etc. It can be done. My suggestion, though, would be to convert the data in-place in whatever database you have by using a lot of ALTER TABLE RENAME... work and same for columns to make the old DB conform to Rails' convetions (primary key == 'id', table name is plural underscore'd version of model name, all that) before doing the import, and then you can just use plain vanilla ActiveRecord and all will be easy.
For instance, when I generate an Event model, the table automatically sets to the public schema. How do I specify it to get set to a different schema?
Furthermore, how do you alter the schema of an existing table? Perhaps move it to a different schema?
Thank you!
Disclaimer: I don't know rails, so I'm going to give very postgresql-oriented answers here. For the first part of your question, there is quite possibly a much better way to do this, by making rails specify the schema when creating tables.
In PostgreSQL, tables are searched for in schemas according to the search_path setting. This is set by default to "$user",public. Tables are created in the first schema found in the search path that exists. So if you connect as "my_user", it will try to create tables in "my_user", and fall back to creating them in "public" if "my_user" doesn't exist.
So one approach is to update the "search_path" setting used for the user you connect to the database to make schema changes. For example you can say ALTER USER my_user SET search_path = my_app, public. If you then create a "my_app" schema then subsequent CREATE TABLE foo(...) commands executed by "my_user" will put the new table into "my_app".
You can change the schema of a table using ALTER TABLE foo SET SCHEMA my_app.
Create a migration to generate your new schema. ActiveRecord can't update you schema to you it's the pattern system. You can try sequel or DataMapper if you want update you schema from your code.