After fetch related beans and update some fields and fetch again gives old records

in_customer_bean = BeanFactory::getBean("IN_Customers",$customer_id);

        if ($in_customer_bean->load_relationship('in_locations')) {

   

            $relatedBeans = $in_customer_bean->in_locations->getBeans();

              foreach(  $relatedBeans  as key => $value){

                  $value->name = abc;

                  $value->save();

               }
               $relatedBeans2 = $in_customer_bean->in_locations->getBeans();

// Now this beans get me old recoard not updated.

            return $relatedBeans;

        }

  • Are you doing this in a logic_hook? 

    There are a couple of things to check:

    - You probably did this already but, check that you are actually looping through related beans, if you relationship name is not correct you won't loop through at all.

    - I assume you are not looking at your return value to determine what you get, since you are still returning the old bean $relatedBeans, not the one you got after the update $relatedBeans2

     

    - you may be running into caching issues, the system retrieves what is in the cache and perhaps the beans have not been updated in cache?

    When you retrieve a newly saved Bean in a logic hook, for example, you need to tell it explicitly to ignore the cache.

    For example:

    $cs = BeanFactory::retrieveBean('Cases', $case_id, array('use_cache' => false));

    Perhaps try loading the relationship again, re-loading the relationship may give you the latest values

     if ($in_customer_bean->load_relationship('in_locations')) {

       $relatedBeans2 = $in_customer_bean->in_locations->getBeans();

    }

     

    I hope this helps,

    Francesca

     

  • Hi Francesca,

    I'm also faced same scenario i.e retrieveBean() returns old bean values instead of latest values from database.

    Adding 'use_cache' => false helped me on this case. 

     

    Thanks for the suggestion.

    Regards,

    Selva