RedBeanPHP\Facade::bindFunc PHP Method

bindFunc() public static method

This method can be used to setup a decode/encode scheme or perform UUID insertion. This method is especially useful for handling MySQL spatial columns, because they need to be processed first using the asText/GeomFromText functions. Example: R::bindFunc( 'read', 'location.point', 'asText' ); R::bindFunc( 'write', 'location.point', 'GeomFromText' ); Passing NULL as the function will reset (clear) the function for this column/mode.
public static bindFunc ( string $mode, string $field, string $function ) : void
$mode string mode for function: i.e. read or write
$field string field (table.column) to bind function to
$function string SQL function to bind to specified column
return void
    public static function bindFunc($mode, $field, $function)
    {
        self::$redbean->bindFunc($mode, $field, $function);
    }

Usage Example

Example #1
0
 /**
  * Test Facade bind function method.
  * Test for MySQL WKT spatial format.
  */
 public function testFunctionFilters()
 {
     R::nuke();
     R::bindFunc('read', 'location.point', 'asText');
     R::bindFunc('write', 'location.point', 'GeomFromText');
     R::store(R::dispense('location'));
     R::freeze(true);
     try {
         R::find('location');
         fail();
     } catch (SQL $exception) {
         pass();
     }
     R::freeze(false);
     try {
         R::find('location');
         pass();
     } catch (SQL $exception) {
         fail();
     }
     $location = R::dispense('location');
     $location->point = 'POINT(14 6)';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'point');
     $location = $location->fresh();
     asrt($location->point, 'POINT(14 6)');
     R::nuke();
     $location = R::dispense('location');
     $location->point = 'LINESTRING(0 0,1 1,2 2)';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'linestring');
     $location->bustcache = 2;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point, 'LINESTRING(0 0,1 1,2 2)');
     R::nuke();
     $location = R::dispense('location');
     $location->point = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'polygon');
     $location->bustcache = 4;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point, 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))');
     R::bindFunc('read', 'location.point', NULL);
     $location->bustcache = 1;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point === 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))', FALSE);
     $filters = AQueryWriter::getSQLFilters();
     asrt(is_array($filters), TRUE);
     asrt(count($filters), 2);
     asrt(isset($filters[QueryWriter::C_SQLFILTER_READ]), TRUE);
     asrt(isset($filters[QueryWriter::C_SQLFILTER_WRITE]), TRUE);
     R::bindFunc('read', 'place.point', 'asText');
     R::bindFunc('write', 'place.point', 'GeomFromText');
     R::bindFunc('read', 'place.line', 'asText');
     R::bindFunc('write', 'place.line', 'GeomFromText');
     R::nuke();
     $place = R::dispense('place');
     $place->point = 'POINT(13.2 666.6)';
     $place->line = 'LINESTRING(9.2 0,3 1.33)';
     R::store($place);
     $columns = R::inspect('place');
     asrt($columns['point'], 'point');
     asrt($columns['line'], 'linestring');
     $place = R::findOne('place');
     asrt($place->point, 'POINT(13.2 666.6)');
     asrt($place->line, 'LINESTRING(9.2 0,3 1.33)');
     R::bindFunc('read', 'place.point', NULL);
     R::bindFunc('write', 'place.point', NULL);
     R::bindFunc('read', 'place.line', NULL);
     R::bindFunc('write', 'place.line', NULL);
 }