It appears RedBeanPHP 4KS removed R::setStrictTypi

2020-06-23 07:49发布

I am using RedBeanPHP along with an API I am writing to make calls to an existing Database. Everything works great except some of the tables have underscores in their names. According to RedBean "underscores and uppercase chars are not allowed in type and property names."

When searching for solutions people recommended the use of the function.

R::setStrictTyping(false);

This would override the rules and allow you to dispense a bean such as

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

However this appears to be missing in RedBeanPHP 4KS because when I put the SetStringTyping line in I recieve the following error:

Plugin 'setStrictTyping' does not exist, add this plugin using: R::ext('setStrictTyping')

There is no plugin for this.

Is there a workaround for this override? Since I am working with an existing DB schema its not as easy to just change all the table names to conform to RedBeanPHP standards at this point. Nor, as others suggested, just switching to a different system all together such as using Doctrine.

标签: php redbean
2条回答
成全新的幸福
2楼-- · 2020-06-23 07:52

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.

查看更多
疯言疯语
3楼-- · 2020-06-23 08:15

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;
}
查看更多
登录 后发表回答