It appears RedBeanPHP 4KS removed R::setStrictTyping(false). What is a workaround for dispensing beans with underscores?

回眸只為那壹抹淺笑 提交于 2019-12-03 17:22:32

Found out a solution. The check for underscores and uppercase chars only happens in the Facade. By adding this code:

R::ext('xdispense', function($type){
 return R::getRedBean()->dispense( $type);
})

You can then do the following without error.

$post_points = R::xdispense( 'user_points' );

Cool.

Or you could extend RedBeanPHP\Facade and override dispense function. Then use new wrapper class instead of R static.

public static function dispense( $typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE )
{
    if ( is_array($typeOrBeanArray) ) {
        if ( !isset( $typeOrBeanArray['_type'] ) ) throw new RedException('Missing _type field.');
        $import = $typeOrBeanArray;
        $type = $import['_type'];
        unset( $import['_type'] );
    } else {
        $type = $typeOrBeanArray;
    }

    if ( !preg_match( '/^[a-z0-9_]+$/', $type ) ) {
        throw new RedException( 'Invalid type: ' . $type );
    }

    $redbean = parent::getRedBean();
    $beanOrBeans = $redbean->dispense( $type, $num, $alwaysReturnArray );

    if ( isset( $import ) ) {
        $beanOrBeans->import( $import );
    }

    return $beanOrBeans;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!