简体   繁体   中英

Casebox object field depending on another field in the same parent object

I have a CaseBox object, with one field ( age ) intended to depend on another field ( birthdate ). As I understand the developer docs, I should make age of type Objects , write a server-side function, and set the config property's source element on age to pass the function and field.

My config for the age field:

{
   "source": {
      "requiredFields": "birthdate"
      ,"fn":"Casebox\\CoreBundle\\Service\\Util\\Utility.calculateAge"
   }
   ,"readOnly": "true"
   ,"autoLoad": "true"
}

I have independently validated calculateAge() ; it accepts a scalar value, makes a DateTime from it if needed, and does a year-based diff . So that part works.

But age does not automatically populate. I get the following entries in the core log file, when I enter the age field:

[2016-11-02 17:57:18] request.INFO: Matched route "app_remote_router". {"route_parameters":{"_controller":"Casebox\\RpcBundle\\Controller\\RpcApiController::routeAction","coreName":"demotwo","_route":"app_remote_router"},"request_uri":" http://192.168.33.3.xip.io/c/demotwo/remote/router "} []

[2016-11-02 17:57:18] security.DEBUG: Read existing security token from the session. {"key":"_security_main"} []

[2016-11-02 17:57:18] security.DEBUG: User was reloaded from a user provider. {"username":"root","provider":"Symfony\\Bridge\\Doctrine\\Security\\User\\EntityUserProvider"} []

[2016-11-02 17:57:18] request.CRITICAL: Uncaught PHP Exception Symfony\\Component\\Debug\\Exception\\FatalThrowableError: "Function name must be a string" at /var/www/casebox/vendor/caseboxdev/core-bundle/src/Service/Browser.php line 443 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at /var/www/casebox/vendor/caseboxdev/core-bundle/src/Service/Browser.php:443)"} []

[2016-11-02 17:57:18] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []

Inside Browser.php , I logged all the data I could find. The line in question (presently 443) is unmodified from the original CaseBox distro, and it passes parameters to the target. What I found is that it passes all the parameters, as an array.

So ultimately my guess is that my method needs to accept an array, and manually find birthdate and its value.

My guess was partially correct. I changed calculateAge like so:

function calculateAge(&$p) { 
    if (!empty($p['objFields']['birthdate'])) {
        $birthday = $p['objFields']['birthdate'];
    }
    else {
        return 0;
    }

    //...proceed with calculation...

The second part was that my coworker fixed the CaseBox source file. The relevant block was:

$method = explode('.', $fieldConfig['source']['fn']);
$class = new $method[0]();
$rez = $class->$method[1];

And the new code effectively casts the method name from an array to a string:

$method = explode('.', $fieldConfig['source']['fn']);
$class = new $method[0]();
$rez = $class->$method[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM