Jaume Albaigès

Why the bean contains different information after creating a record from an editview or a quickcreateview?

Discussion created by Jaume Albaigès on Dec 16, 2014
Latest reply on Mar 6, 2015 by Jaume Albaigès
Recently, I wrote a logic hook whose purpose was to update a calculated field in a related module once a record was created, modified or deleted in a certain module.

After a hard and long research, I finally wrote the code below, which seems to work properly for achieving the original goal.

Anyway, I don't fully understand this code. ;-P

I have some knowledge gaps I would like to fill. Here they are:

1) The most important is the one in the title of this thread: why the bean contains different information after creating a record from an editview or a quickcreateview? In the first case (editview) the related ID is already available after record saving. In the second one, I need to load some extra information from the database to obtain the related ID.

2) Related to the previous question, why $bean->my_related_id is a different type of object? In the first case (editview) it is a string while in the second one (quickcreateview) is something more complex (it contains the rows array and probably some more stuff).

3) Talking about best practices, is it ok to detect the view of the created record (editview/quickcreateview) by using the is_string() function? Could this test fail under certain circumstances? Could this be done in a better way?

Thank you very much for sharing your knowledge!


PS. This topic is related to this one I posted some weeks ago. Logic hook executes correctly for all actions except for creating from quick create

class my_class
    public function my_function(&$bean, $event, $arguments)

        if ($event == 'after_delete') {
            // Retrieve related record id
            $db =  DBManagerFactory::getInstance();
            $query = "SELECT my_related_id FROM relation_table
                        WHERE my_id = '".$arguments['id']."'";
            $result = $db->query($query, true);
            $row = $db->fetchByAssoc($result);
            $related_id = $row['my_related_id'];
        } else // after_save event
            if (is_string($bean->my_related_id))
                // If relation is a string the action was in the
                // editview and $related_id is already available
                // in the bean
                $related_id = $bean->my_related_id;
     else {
                // If relation is not a string then the action
                // was in the quickcreateview and $related_id is
                // not already available in the bean. We need to
                // load it from database.
  // The operation loads an array into "rows" bean
                // property. $related_id is the key of the only
                // array element
                $related_id = key($bean->my_related_id->rows);

        $bean->db->query("UPDATE my_related_module SET my_calculated_field = my_value WHERE id_c = '{$related_id}'");