【问题标题】:Redbean and Yii conflict?Redbean 和 Yii 冲突?
【发布时间】:2012-04-07 23:37:55
【问题描述】:

这里是普通 Yii 控制器中的重要代码。

Yii::import('application.vendors.*');
    require_once('redbean/rb.php');

$config = Yii::app()->getComponents(false);

R::setup($config['db']['connectionString'],
$config['db']['username'],
$config['db']['password'])

$guest = R::dispense( 'guest' );
$guest->email = $row['Guest Email'];

错误发生在dispens() 行。

include(Model_Guestx.php) function.include:无法打开流:否 这样的文件或目录(路径 编辑\framework\YiiBase.php:418)

#0 路径 编辑\framework\YiiBase.php(418): CWebApplication->handleError()

问题是为什么 Yii 试图加载一个与 redbean 同名的 Model 正在寻找分配?

谢谢!

【问题讨论】:

    标签: php yii redbean


    【解决方案1】:

    我不知道是什么触发了 Yii 自动加载器,但你可以包含以下代码让 Yii 跳过任何由 Redbean 创建的模型:

    public static function autoload($className)
        {
        if(!strncmp("Model_", $className, strlen("Model_")))
            return true;
        //...rest of yii code...
    

    如果您将它添加到 YiiBase.php 代码的开头,它将忽略以“Model_”开头的模型,这是 Redbean 使用的。我知道这是一个 hack,但除非您创建打算与 Yii 一起使用的属于该命名类别的模型,否则这应该不是问题。

    【讨论】:

    • 这适用于 Redbean 创建的模型,但如果将模型添加到 Redbean 可能会导致问题,因为它们也是以 Model_ 开头的。
    • 这不是说 RedBean 模型也被 Yii 忽略了吗?为什么这个 hack 会对 RedBean 加载其模型的方式产生任何影响?
    • 它没有。我应该更具体一点:在我的例子中,我使用除了 Redbean 之外添加到 Redbean 的模型。那么它应该由自动加载器加载,但在这种情况下它不会。但我承认这可能有点特殊。
    【解决方案2】:

    Yii 有自己的类自动加载器,并且不知何故(可能在检查模型是否存在时)Redbean 创建了一个看起来像 PHP 文件的字符串。所以 Yii 尝试包含它。

    您可以在使用 Redbean 之前禁用 yii 自动加载器;然后在完成后重新启用它:

        // Turn off our amazing library autoload 
        spl_autoload_unregister(array('YiiBase','autoload'));   
    
        Yii::import('application.vendors.*');
        require_once('rb.php');
    
        R::setup('mysql:host=localhost;dbname=dbname', 'user', 'password');
    
        $guest = R::dispense('guest');
        $guest->email = $row['Guest Email'];
    
        // Once we have finished using the library, give back the 
        // power to Yii... 
        spl_autoload_register(array('YiiBase','autoload'));
    
    
        $this->render('index');
    

    功劳归于: http://www.yiiframework.com/wiki/101/how-to-use-phpexcel-external-library-with-yii/

    【讨论】:

      【解决方案3】:

      如果你想让它更通用,为什么不使用 beforeControllerAction() 和 afterControllerAction() 事件来禁用和启用专用于 redbena 的基本控制器中的 Yii 自动加载,或者如果你想单独使用 redbean。

      喜欢:

      public function beforeControllerAction(){
      spl_autoload_unregister(array('YiiBase','autoload'));   
      // other code to execute...
      parent::beforeControllerAction(); 
      }
      
          public function afterControllerAction(){
      spl_autoload_register(array('YiiBase','autoload'));
      
      // other code to execute...
      parent::afterControllerAction();
      }
      

      【讨论】:

        【解决方案4】:

        如果你想在没有任何选项的情况下使用 Readbean ORM,你可以使用 redbean 查询类型,如

        $result = R::$f->begin()->select('*')->from('users')->where(' username = ? ')->put($_xusername)->get('row');
        

        这里你不需要注册或取消注册自动加载

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 2021-10-21
          • 1970-01-01
          相关资源
          最近更新 更多