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

天大地大妈咪最大 提交于 2019-12-05 02:34:43

问题


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.


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/23019307/it-appears-redbeanphp-4ks-removed-rsetstricttypingfalse-what-is-a-workaroun

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