Using the PHP SDK in Business Logic possible?

It is possible to use the PHP SDK in Business Logic (PHP)?
I’m trying to create a new object and save it when a event handler get’s called, but I don’t get it working.


<?php
namespace com\backendless\proproject\events\persistence_service;



use backendless\core\extension\BasePersistenceEventHandler;
use backendless\BackendlessCollection;
use backendless\model\GeoPoint;
use backendless\model\GeoCategory;
use backendless\services\persistence\BackendlessDataQuery;
 
use backendless\proproject\models\BLObjectType;
use backendless\proproject\models\BLProjectObject;
use backendless\proproject\models\BLGroup;
use backendless\proproject\models\BLCustomProperty;



 
/**
* BLProjectObjectTableEventHandler handles events for all entities. This is accomplished
* with the @annot({ Asset:"BLProjectObject"}) annotation. 
* The methods in the class correspond to the events selected in Backendless
* Console.
*/
 
/** @annot({"Asset":"BLProjectObject"}) */ 



class BLProjectObjectTableEventHandler extends BasePersistenceEventHandler 
{
 
 public function afterCreate( $runner_context, $blProjectObject, $execution_result ) {



 // add your code here
 
 echo "\n";
 echo "ProjectObject - ". $blProjectObject->getName() ." Created";
 echo "\n";



 $group = new BLGroup();
 $group->setName("PHP");
 Backendless::$Persistence->save( $group );
 }

Getting error:

Fatal error: Class ‘backendless\proproject\models\BLGroup’ not found

Is there some example of how to use the PHP SDK in Business Logic (if possible at all)?

Thanks a lot!

Hello, Barry Kostjens.
Thanks for you feedback. We reproduced this problem. Approximately this problem will be fixed at the next week.

p.s.
Besides, you should use this construction for you task:
Backendless::$Data->of(“BLGroup”)->save( $group )

Thanks a lot Oleg, can you reply here when things are fixed?

Yes, Barry. We will notify you when it is fixed.

Regards,
Mark

Hi Guys,

Do you have a status update on this?

Thanks a lot :slight_smile:

Hello, Barry!

This issue has been already fixed and will be released soon. We shall notify you about that.
best regards,
Alex

Thanks a lot Alex.

Hi Alex,

Has the fix been rolled out? Im still getting the same error?

Thanks,

Barry

Hi, Barry!

I’m very sorry for this delay. Yes, it’s on production yet - you can download latest code from github, or generate it through console. Now both approaches of saving should work well: Backendless::$Data->of(“BLGroup”)->save( $group ) and Backendless::$Data -> save( $group );
Please, notify us after your try.
best regards,
Alex

Looking good so far! Thanks!

another question:

$blProjectObject->getOwnerId;

and

$blProjectObject->getOwner;

both return nothing.

How can i get the owner of the object?


public function afterCreate( $runner_context, $blProjectObject, $execution_result ) {


       // add your code here
        echo "\n";
        echo "ProjectObject - ". $blProjectObject->getName() ." Created";
        echo "\n";
        //$group = new BLGroup();
        //$group->setName("PHP");
        //Backendless::$Data -> save( $group );


        $ownerId = $blProjectObject->getOwnerId;
        print "OwnerId: ".$ownerId;
     }

Barry, you can retrieve ownerId using $blProjectObject -> getOwnerId(). If there is one - you’ll get it.

Make sure that you create object by authorized user. The easiest way to test it - is with REST console. Navigate to it and follow these steps:

  1. Click on “login” link in the right top corner
  2. Login any user
  3. Choose BlProjectObject table on the left
  4. Execute POST request with {} in body window
    After that you’ll be able to see ownerId in coderunner logs.
    best regards,
    Alex

Hi Alex,

The object is created using the iOS API. The owner is set, see this debug output from my iOS app:
print (“Try uploading blProjectObject: (blProjectObject) with owner: (blProjectObject.owner?.email)”)

output:
Try uploading blProjectObject: <ProProject.BLProjectObject: 0x7f9055c86e50> with owner: Optional(“xxxx@xxxxx.xxx”)

Now getting the message:
Fault! Error Creating BLProjectObject ‘test’: FAULT = ‘15000’ [Custom business logic execution has been terminated because it did not complete in permitted time - 20 seconds] <Custom business logic execution has been terminated because it did not complete in permitted time - 20 seconds>

I did not change anything, all that the business logic does currently is this:

class BLProjectObjectTableEventHandler extends BasePersistenceEventHandler


{
    
  public function afterCreate( $runner_context, $blProjectObject, $execution_result ) {


       // add your code here
        echo "\n";
        echo "ProjectObject - ". $blProjectObject->getName() ." Created";
        echo "\n";
        //$group = new BLGroup();
        //$group->setName("PHP");
        //Backendless::$Data -> save( $group );


        $ownerId = $blProjectObject->getOwnerId;
        print "OwnerId: ".$ownerId;
     }
    
}

Thanks. Changed that but I keep getting:

Fault! Error Creating BLProjectObject ‘tewtw’: FAULT = ‘15000’ [Custom business logic execution has been terminated because it did not complete in permitted time - 20 seconds] <Custom business logic execution has been terminated because it did not complete in permitted time - 20 seconds>

I guess that the problem is in row 13:

$ownerId = $blProjectObject->getOwnerId;

Can you try to change it to:

$ownerId = $blProjectObject->getOwnerId();

Can you please check if the object you’re trying to create is really created?

You mean in backendless itself. I don’t think so by reading the error but i’ll check.

Edit: Ok other than the error suggests: It is created 74 times (with the correct owner). This is because it stays in the local queue because I get an error back, and it retries every x seconds to handle the queue.

So now one question remains: Why do I get this error?

Do you have any other handlers for persistence? Especially “before” handlers?

No, im just testing with this small code I posted. No other handlers.

OOPS!! I was running code runner without the Code Runner Secret! Now all works ok!