【问题标题】:Load and use Codeigniter model in non Codeigniter class在非 Codeigniter 类中加载和使用 Codeigniter 模型
【发布时间】:2017-10-05 11:55:48
【问题描述】:

我只是想知道是否有办法在其他非 Codeigniter 类中使用 codeigniter 模型...让我举个例子。

我有一个扩展 PHPUNIT_Framework_testCase 的 MyTestClassTests 类

<?php        
require_once '../../vendor/autoload.php';
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Interactions\WebDriverActions;
use Sunra\PhpSimple\HtmlDomParser;

class MyTestClassTests extends PHPUnit_Framework_TestCase
{
    public function testDoSomething()
    {
       // Do some test
       // get results
       // Store results via Codeigniter Model, if possible?
       $results = 'some results';
       $this->load->model('results');
       $this->results->import($results);
    }
}

现在一旦测试完成,我想将测试结果存储到数据库中。有没有办法在当前类中调用/初始化 CodeIgniter 模型以使用它并存储数据?该文件位于 Codeigniters 控制器文件夹中。

如果您需要任何其他信息,请告诉我,我会提供。谢谢!

【问题讨论】:

    标签: php codeigniter phpunit


    【解决方案1】:

    由于您似乎在进行单元测试,因此您应该考虑使用ci_phpunit-test,这样可以更轻松地将 PHPUnit 与 CodeIgniter 3.x 一起使用。

    由于您正在进行单元测试,因此以下内容可能不适用。这些示例仅适用于完全实例化的 CI 框架。在这种情况下,有几种方法可以让独立类访问 CI 对象。

    一种方法是在类属性中捕获 CI 实例。

    class MyTestClassTests extends PHPUnit_Framework_TestCase
    {
        protected $CI;
    
        public function __construct()
        {
            // Assign the CodeIgniter super-object
            $this->CI = & get_instance();
        }
    
        public function testDoSomething()
        {
           // Do some test
           // get results
           // Store results via Codeigniter Model, if possible?
           $results = 'some results';
           //use the class property to access CI classes and methods
           $this->CI->load->model('results');
           $this->CI->results->import($results);
        }
    }
    

    第二种方法使用 PHP 魔术方法__get。优点是编写代码要容易得多。缺点是效率有点低,因为每次访问 CI 实例时都会执行额外的代码。

    class MyTestClassTests extends PHPUnit_Framework_TestCase
    {
        /**
         * Enables the use of CI super-global without having to define an extra variable.
         *
         * @param   $var The CI property or method to access
         * @return  mixed
         */
        public function __get($var)
        {
            return get_instance()->$var;
        }
    
        public function testDoSomething()
        {
           // Do some test
           // get results
           // Store results via Codeigniter Model, if possible?
           $results = 'some results';
           //you get to write code as if you were part of the CI object.
           //IOW, you write code normally
           $this->load->model('results');
           $this->results->import($results);
        }
    }
    

    【讨论】:

    • 谢谢!
    【解决方案2】:

    为了在您的非 codeigniter 类中使用 codeigniter 模型,您必须先实例化 CI。

    在您的情况下,下面的代码将起作用。

    $CI = & get_instance()
    $CI->load->model('results');
    $CI->results->your_function();
    

    【讨论】:

    • 能否请您提供一个关于我的案例的示例!?
    • 我可能必须包含一些文件? index.php?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多