Search code examples
phpmysqlredbean

how to use bindFunc method in redbeanphp?


i am working on a project that need to work with spatial object in mysql and i choose redbeanphp as ORM system in this project. i found redbeanphp so terrific and awesome. i search in google and found this link that say bindFunc method can help me in working with spatial object but i cannot find any example for using this method.

how this method work and how can i use this method?


Solution

  • According to the database part of that website you linked:

    As of RedBeanPHP 4.1 you can bind an SQL function to a column. This is useful for wrapping values when reading from / writing to the database. For instance, to use MySQL spatial data types you need to prepare the columns like this:

    R::bindFunc( 'read', 'location.point', 'asText' );
    R::bindFunc( 'write', 'location.point', 'GeomFromText' );
    
    $location = R::dispense( 'location' );
    $location->point = 'POINT(14 6)';
    
    //inserts using GeomFromText() function
    R::store( $location );
    
    //to unbind a function, pass NULL:
    R::bindFunc( 'read', 'location.point', NULL );
    

    This function was added to support spatial datatypes in MySQL as shown above. The documentation isn't amazing, so I recommend looking at the source code on Github if you want to dig in deeper.