Results 1 to 5 of 5

Thread: How to handle relationships in after_save logic hook

  1. #1
    mminnie's Avatar
    mminnie is offline Sugar Community Member
    Join Date
    May 2005
    Location
    Sacramento, CA
    Posts
    97

    Default How to handle relationships in after_save logic hook

    I am having trouble getting relationship information in an after_save logic hook for a new record. Here is my scenario:

    My website makes a REST call to a custom SugarCRM webservice to enroll a dog in a training class. I want to send an email out to the dog owner after the class enrollment is made. I have custom modules cl_classes (the class sessions)(, dogs_dog_module (the doggies!), and cl_classenrollment (a module for linking dogs to class sessions).

    Here is a snippet of code from my custom webservice that creates the cl_classenrollment record:
    PHP Code:
            $classenrollment BeanFactory::newBean('cl_classenrollment');
            
    // If I don't call save here, the relationships aren't created.  Presumably because the cl_classenrollment->id needs to be set to make a relationship? 
            
    $classenrollment->save();   

            
    $classenrollment->load_relationship('dogs_dog_module_cl_classenrollment_1');
            
    $classenrollment->dogs_dog_module_cl_classenrollment_1->add($dogid);
            
    $classenrollment->load_relationship('cl_classes_cl_classenrollment_1');
            
    $classenrollment->cl_classes_cl_classenrollment_1->add($classsessionid);
            
    $classenrollment->save(); 
    And in my after_save logic hook, here is where I start gathering the information to send an email:
    PHP Code:
            if (empty($bean->fetched_row['id'])) {     // <--- only send email if we are creating a new record
                    // Get the class session info.  
                    
    $bean->load_relationship('cl_classes_cl_classenrollment_1');  
                    
    $aclasssession reset($bean->cl_classes_cl_classenrollment_1->getBeans());  // <--- This comes back empty/no relationships unless I save the cl_classenrollment bean before adding the relationships

                    // Get the dog bean so we can reference by name.
                    
    $bean->load_relationship('dogs_dog_module_cl_classenrollment_1');
                    
    $adog reset($bean->dogs_dog_module_cl_classenrollment_1->getBeans());   // <--- This comes back empty/no relationships unless I save the cl_classenrollment bean before adding the relationships 

    It seems the most logical place to put the email logic is in the after_save hook of the cl_classenrollment record as I only want to send out emails for newly enrolled dogs.

    Any ideas as to how best to get related record information and send an email out in the after_save logic hook on new records only?


    If I'm stubborn enough, I just might figure it out myself.
    SugarCRM Version 6.5.9 (Build 8653)
    System: Linux 3.2.0-32-generic Ubuntu 12.04 LTS
    PHP Version 5.3.10-1ubuntu3.4
    MySQL Version 5.5.24

  2. #2
    Francescas's Avatar
    Francescas is offline Sugar Community Member
    Join Date
    Dec 2011
    Location
    Colorado
    Posts
    508

    Default Re: How to handle relationships in after_save logic hook

    You are right, you have to have an id (a saved record) before you can add the relationship and the relationship needs to exist before you can retrieve it.
    So try putting your email code in the after_relationship_add logic hook on your class enrollment module.
    That way your enrollment is complete before you email, the relationships will all exist then.

    You will have your $bean (your class enrollment module ) and your $arguments['related_module'] and $arguments['related_id']
    Check your related_module so you fire this off only once (else it will fire when you add the class, and when you add the doggie)
    In the logic_hooks.php of your class enrollment module:
    PHP Code:
    $hook_array['after_relationship_add'][] = Array(1'send_on_relationship''custom/modules/<your_module>/<your_script>.php','send_email''send_on_relationship'); 
    It will be something like:
    PHP Code:
    class send_email
    {
       function 
    send_on_relationship ($bean,$event,$arguments){
          if (
    $arguments['related_module'] == "dogs_dog_module"){
             
    $dog_id $arguments['related_id'];
             
    $class_enrollment_id $bean->id;
             
    //prepare and send the email
            
          
    }
       }

    Francesca
    =========
    SugarCRM Professional 6.5.15
    Linux
    Apache
    MySql
    PhP

  3. #3
    mminnie's Avatar
    mminnie is offline Sugar Community Member
    Join Date
    May 2005
    Location
    Sacramento, CA
    Posts
    97

    Default Re: How to handle relationships in after_save logic hook

    Is the "relationship->add()" handled differently when it is a one-to-one or many-to-many, or one-to-many?

    In this blog post, the relationship is added in the before_save hook. Why would this work?


    If I'm stubborn enough, I just might figure it out myself.
    SugarCRM Version 6.5.9 (Build 8653)
    System: Linux 3.2.0-32-generic Ubuntu 12.04 LTS
    PHP Version 5.3.10-1ubuntu3.4
    MySQL Version 5.5.24

  4. #4
    Francescas's Avatar
    Francescas is offline Sugar Community Member
    Join Date
    Dec 2011
    Location
    Colorado
    Posts
    508

    Default Re: How to handle relationships in after_save logic hook

    Quote Originally Posted by mminnie View Post
    Is the "relationship->add()" handled differently when it is a one-to-one or many-to-many, or one-to-many?

    In this blog post, the relationship is added in the before_save hook. Why would this work?
    I don't think there is a problem with how you add the relationship (which is what Angel's blog addresses), but rather how you retrieve it in the after save. I think, and I can be corrected on this, that the after_save logic hook triggers before the relationship is created. My understanding of the order is: before save, actual save, after save, before relationship add, actual add relationship, after relationship add. So in the after save you do not yet have the relationship and therefore cannot retrieve it.

    Does this help?
    Francesca
    Francesca
    =========
    SugarCRM Professional 6.5.15
    Linux
    Apache
    MySql
    PhP

  5. #5
    mminnie's Avatar
    mminnie is offline Sugar Community Member
    Join Date
    May 2005
    Location
    Sacramento, CA
    Posts
    97

    Default Re: How to handle relationships in after_save logic hook

    It does make sense that I can't retrieve the relationship in the after_save given the order of how the hooks fire.

    I was focused on my after_save hook logic not having access to the relationships. I didn't test whether or not the relationships are actually created if I don't call the first save() on my bean. I don't think Sugar would "queue" the new relationships and then create them after the bean is saved. Or does it? I guess it would have to in order for a relationship->add() to work successfully in a before_save logic hook. I guess that is something for me to test when I am bored.

    Thanks for reaching out and helping.


    If I'm stubborn enough, I just might figure it out myself.
    SugarCRM Version 6.5.9 (Build 8653)
    System: Linux 3.2.0-32-generic Ubuntu 12.04 LTS
    PHP Version 5.3.10-1ubuntu3.4
    MySQL Version 5.5.24

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Custom Date fields + After_Save Logic Hook
    By chrislynch8 in forum Help
    Replies: 1
    Last Post: 2012-06-20, 01:07 AM
  2. Replies: 9
    Last Post: 2012-05-12, 05:43 AM
  3. Get original value in after_save logic hook
    By mikesolomon in forum Developer Help
    Replies: 2
    Last Post: 2011-10-12, 02:12 PM
  4. after_save logic hook question
    By Keenn in forum Developer Help
    Replies: 2
    Last Post: 2011-03-24, 09:42 AM
  5. after_save Logic Hook and Subpanels
    By bsc in forum Developer Help
    Replies: 3
    Last Post: 2010-09-30, 04:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •