Perhaps instead of using "before :save", u can try the following alternative:

"
class Interest
   ...
   after :question=, :increment_interested_users
   def increment_interested_users
     question.interested_users += 1
   end
end
"

DM provides before & after hooks for any method, not just :save. I
find it really a very convenient feature.

Cheers !!

==

On Tue, Mar 10, 2009 at 2:06 AM, Rafael George <[email protected]> wrote:
>
> Thanks it's look like that the way to go, but i tried it and did not
> work. Here is the code for the two model involve and the code for the
> hook.
>
> class Question
>
>  include DataMapper::Resource
>
>  # properties
>
>  property :id,    Serial
>  property :title, String, :nullable => false, :length => (1..255)
>  property :body,  Text
>  property :interested_users, Integer, :default => 0
>
>  timestamps :at
>
>  # associations
>
>  belongs_to :user
>
>  has n, :answers
>  has n, :interests
>  has n, :users, :through => :interests
>
> end
>
> class Interest
>
>  include DataMapper::Resource
>
>  # properties
>  property :user_id, Integer, :key => true
>  property :question_id, Integer, :key => true
>  timestamps :at
>
>  # associations
>
>  belongs_to :user, :child_key => [:user_id]
>  belongs_to :question, :child_key => [:question_id]
>
>  # hooks
>  before :save, :save_interested
>
>  def save_interested
>    question = self.question
>    question.interested_users = question.interested_users+1
>    self.question.save
>  end
> end
>
>
> On Mon, Mar 9, 2009 at 12:03 PM, Rafael <[email protected]> wrote:
>>
>> Hi,
>>
>> I'm not a PHP developper, and I'm quite new to DataMapper, but I'm
>> pretty sure this is not the right approach. I believe you should use
>> the hooks (aka callbacks or events) to make this kind of process. The
>> reference is here http://datamapper.org/doku.php?id=docs:hooks, and
>> your code should look like this:
>>
>> class Interest
>>  include DataMapper:Resource
>>   # properties
>>   property :user_id, Integer, :key => true
>>   property :question_id, Integer, :key => true
>>   property :interested_users, Integer, :default => 0
>>   timestamps :at
>>
>>   # associations
>>   belongs_to :user, :child_key => [:user_id]
>>   belongs_to :question, :child_key => [:question_id]
>>
>>   # hooks
>>   before :save, :add_one
>>
>>   protected
>>     def add_one
>>       self.interested_users = self.interested_users + 1
>>     end
>> end
>>
>> I believe the hooks are called inside a transaction, so it should be
>> safe to do this. I used "interested_users" as Integer just to
>> illustrate, but you can fix it according to your needs.
>>
>> Unfortunately the documentation is very scarce, I hope to have some
>> time to help fix that in the near future. Hope this helps.
>>
>> Cheers,
>> Rafael
>> www.rafaelrosafu.com
>> >
>>
>
>
>
> --
> Rafael George
>
> >
>



-- 
http://ngty77.blogspot.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to