0
-=== I don't want to use :id as a primary key, but I don't see
0
-=== <tt>set_primary_key</tt> anywhere. What do I do?
0
+=== So where's my :id column?
0
-If you're working with a table that doesn't have a <tt>:id</tt> column, you
0
-can declare your properties as you usually do, and declare one of them as a
0
+DataMapper will NOT create an auto-incrementing <tt>:id</tt> key for you
0
+automatically, so you'll need to either explicitly create one with
0
-
property :name, String, :key => true
0
+
property :id, Fixnum, :serial => true
0
-You should now be able to do <tt>Class['name_string']</tt> as well. Remember:
0
-this column should be unique, so treat it that way. This is the equivalent to
0
-using <tt>set_primary_key</tt> in ActiveRecord.
0
+You can choose to use a natural key by doing
0
+ property :slug, String, :key => true
0
-=== How do I make a model paranoid?
0
+Remember, DataMapper supports multiple keys ("composite keys"), so if your
0
+model has two or more keys, no big deal
0
- property :deleted_at, DateTime
0
+ property :store_id, Fixnum, :key => true
0
+ property :invoice_id, Fixnum, :key => true
0
-If you've got deleted_at, your model is paranoid auto-magically. All of your
0
-calls to <tt>##all()</tt>, <tt>##first()</tt>, and <tt>##count()</tt> will be
0
-scoped with <tt>where deleted_at is null</tt>. Plus, you won't see deleted
0
-objects in your associations.
0
+=== How do I make a model paranoid?
0
-
=== Does DataMapper support Has Many Through?0
+
Create a property and make it a ParanoidDateTime or ParanoidBoolean type.0
+ property :deleted_at, ParanoidDateTime
0
+ property :deleted, ParanoidBoolean
0
-=== What about Self-Referential Has And Belongs to Many?
0
+All of your calls to <tt>##all()</tt>, <tt>##first()</tt> will be scoped
0
+with <tt>:deleted_at => nil</tt> or <tt>:deleted => false</tt>. Plus,
0
+you won't see deleted objects in your associations.
0
-Sure does. Here's an example implementation:
0
- include DataMapper::Resource
0
- :join_table => "task_relationships", :left_foreign_key => "parent_id",
0
- :right_foreign_key => "child_id"
0
-You'll notice that instead of <tt>foreign_key</tt> and
0
-<tt>association_foreign_key</tt>, DataMapper uses the "database-y" terms
0
-<tt>left_foreign_key</tt>, and <tt>right_foreign_key</tt>.
0
=== Does DataMapper do Single Table Inheritance?
0
-
Oh yes, and particularly well too.0
+
This is what the Class data-type is for:0
include DataMapper::Resource
0
+ property :id, Fixnum, :serial => true
0
property :type, Class ## other shared properties here
0
class Salesperson < Person; end
0
-You can claim a column to have the type <tt>:class</tt> and DataMapper will
0
-automatically drop the class name of the inherited classes into that column of
0
+You can claim a column to have the type <tt>Class</tt> and DataMapper will
0
+automatically drop the class name of the inherited classes into that field of
0
-=== What about Class Table Inheritance?
0
-Class Table Inheritance is on the drawing board and everyone's drooling over
0
-it. So no, not yet, but soon.
0
=== How do I run my own commands?
0
-You're probably asking for <tt>find_by_sql</tt>, and DataMapper has that in
0
-it's ActiveRecordImpersonation, but if you want to go straight-up DataMapper,
0
-you'll want to use <tt>repository.query</tt>
0
+ repository.adapter.query("select * from users where clue > 0")
0
+ repository(:integration).adapter.query("select * from users where clue > 0")
0
- repository.query("select * from users where clue > 0")
0
This does not return any Users (har har), but rather Struct's that will quack
0
like Users. They'll be read-only as well.
0
-<tt>repository.
.query</tt> shouldn't be used if you aren't expecting a result set
0
+<tt>repository.
adapter.query</tt> shouldn't be used if you aren't expecting a result set
0
back. If you want to just execute something against the database, use
0
-<tt>repository.
.execute</tt> instead.
0
+<tt>repository.
adapter.execute</tt> instead.
0
-=== Can I batch-process a ton of records at once?
0
- User.each(:performance_rating => "low") do |u|
0
- u.employment_status = "fired"
0
+=== Can I get an query log of what DataMapper is issuing?
0
-With ActiveRecord, doing a <tt>User.find(:all).each{}</tt> would execute the
0
-find, instantiate an object for EVERY result, THEN apply your transformations
0
-to each object in turn. Doesn't sound too horrible unless you have a TON of
0
-records; you WILL grind your system to a screeching and bloody halt.
0
+Yup, to set this up, do:
0
-DataMapper's <tt>#each</tt> works in sets of 500 so the amount of objects
0
-instantiated at a time won't make your computer think it's a victim in a Saw
0
-movie. Once it's done executing your block on the first set of 500, it moves
0
+ DataMapper.logger = Logger.new("/path/to/your/logger.log", "w+")
0
-What's more is <tt>#each</tt> is secretly a finder too. You can pass it an
0
-options hash and it'll only iterate on 500-item sets matching your query.
0
-Don't send it <tt>:offset</tt> though, because that's how it pages. You can
0
-overload the page size by sending it <tt>:limit</tt>
0
-=== Can I get an SQL log of what queries DataMapper is issuing?
0
-Yup, when you issue <tt>Repository.setup</tt>, tack on the <tt>log_stream</tt>
0
-and <tt>log_level</tt>:
0
- DataMapper::Repository.setup({
0
- :adapter => 'mysql', :host => 'localhost', :username => 'root',
0
- :password => 'R00tPaswooooord', :database =>
0
- 'myspiffyblog_development', :log_stream => 'log/sql.log', :log_level => 0
0
-By supplying the <tt>log_stream</tt> you're telling DataMapper what file you
0
-want to see your sql logs in. <tt>log_level</tt> is the
0
-Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/] level of output you
0
-want to see there. 0, in this case, says that you want to see all DEBUG level
0
-messages (and higher) sent to the logger. For more information on how to work
0
-with Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/], hit up
0
-http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/.
0
Incidentally, if you'd like to send a message into the DataMapper logger, do:
0
- repository.adapter.logger.info "your message here"
0
+ DataMapper.logger.debug { "something" }
0
+ DataMapper.logger.info { "something" }
0
+ DataMapper.logger.warn { "something" }
0
+ DataMapper.logger.error { "something" }
0
+ DataMapper.logger.fatal { "something" }
Comments
No one has commented yet.