Load_relationship php error

Hi, I’ve had a lot of issues getting custom modules to relate to each other in suitecrm. What I’ve done so far in this instance was create a module named ‘sales_bookings’ i deployed from the module builder and given a 1 to many relationship (1 account to many bookings).

This is the relationship definition from my vardefs in the sales_Booking cache folder (inside cache/modules/sales_Booking):
link relationship

and I’ve used a before_save logic hook, the first two lines of the function are

5. $bean->load_relationship(‘sales_booking_accounts’);
6. $account = $bean->sales_booking_accounts->getBeans();

in which the getBeans() line gets flagged in the apache2 error log but nothing from the suitecrm log with this error:

PHP Notice: Undefined property: AOD_IndexEvent::$sales_booking_accounts in custom/modules/sales_Booking/NameMove.php on line 6
PHP Fatal error: Uncaught Error: Call to a member function getBeans() on null in /var/www/html/goodgreekcrm/custom/modules/sales_Booking/NameMove.php:6

I’ve tried the usual QR&R and rebuilding relationships to see if maybe some auto-generated files haven’t been made yet but after reading through the working with beans section of the docs the relationship name looks right

Did you created the relationships from Studio or did you created manually? IF you did it manually. Have you tried to create it from Studio?

No I created the relationship while the module was still in the module builder. I have set-up a new module for testing this and created the same type of relationship in studio after deploying and get the same error. Vardefs checks out so I’m not sure what the bean name is being checked against

Looking at my cached vardefs, I have the relationship named ‘accounts_moves_moves_1’ which was made in studio. As a test I created a logic hook that tests the load_relationship and getBeans function here:


if ($bean->load_relationship(“accounts_moves_moves_1”)
{
//$account = $bean->accounts_moves_moves_1->getBeans();
}
else
{
$bean->description = ‘failed’;
}

After running this I don’t receive a ‘failed’ in my description, it wasn’t until i changed the relationship name so I believe that it is loading the relationship. Once I uncomment the getBeans line I get the same error in the logs. This is on a brand new install of 7.11.18

PHP Notice: Trying to get property ‘name’ of non-object in /custom/modules/Moves_Moves/Name.php on line 8

This is when trying to use $bean->name = $account->name
So now the fatal errors have started just giving notices, but it still isn’t able to find the related bean. I’ve gone through the Working with Beans to make sure I was using the link name of the relationship but don’t see anything obvious here… I’ve worked through this issue a few months ago. Looking at my database i see a table named ‘accounts_moves_moves_1_c’ which I’ve tried using as the relationship name with no luck. But the table does include the ID name of the moves_moves record and the account - which I guess can be used to rope them together and pull fields but the docs show a much nicer way with getBeans…

I have solved this issue but not using the the preferred method. Here is my snippet in case anyone else has problems with this. It searches the table containing related records between Accounts and Moves_Moves and finds the account ID which is related to the current bean after they are selected

$servername = "localhost";
$username = "sql username";
$password = "sql password";
$db = "crm db";

$conn = mysqli_connect($servername, $username, $password, $db);

$sql = "SELECT accounts_moves_moves_1accounts_ida
            FROM accounts_moves_moves_1_c
            WHERE accounts_moves_moves_1moves_moves_idb = '" . $bean->id . "' && deleted = 0";

    if ($conn){
        //connection succeeded
        $result = mysqli_query($conn, $sql);
        $id = mysqli_fetch_row($result);
    
        $account = BeanFactory::getBean('Accounts', $id[0]);
        $bean->name = $account->name;

        mysqli_free_result($result);
    }
    else{
        //failed
    }

    mysqli_close($conn);
}
1 Like

Just one thing. Based on my experience, it’s not good idea to include DB information in your code. It is better to use SuiteCRM methods to avoid security issues and maintenance related problems. Take a look into this sample to see how DB information is called: