【问题标题】:How to load Model in Cakephp directly如何直接在 Cakephp 中加载模型
【发布时间】:2017-05-11 08:47:55
【问题描述】:

在使用 ./cake test app AllModel 在 Cakephp 应用程序中测试我的所有模型之前,我必须进行一些操作。我使用 Cakephp 2.9.7。这个函数在AllModelTest.php 中声明,看起来像下面这样:

class AllModelTest extends CakeTestSuite {
    public static function suite() {        
        $suite = new CakeTestSuite('All model tests');
        $suite->addTestDirectory(TESTS . 'Case/Model');
        return $suite;
    }
}

如何访问我的模型并直接从该文件进行一些操作?我试过App::uses('MyModel', 'Model'); - 那是行不通的。 $this我不能使用,因为它给了我错误PHP Fatal error: Using $this when not in object context in path....

【问题讨论】:

  • 你能试试 $this->loadModel('CakeTestSuite');
  • $this->loadModel('Your Model Name');
  • @AlivetoDie 我不能打电话给$this 因为我收到错误PHP Fatal error: Using $this when not in object context in path...
  • 这可能对你有用:andy-carter.com/blog/…

标签: php cakephp cakephp-2.9


【解决方案1】:

您可以使用ClassRegistry

$yourModel = ClassRegistry::init('YourModelName');

【讨论】:

    【解决方案2】:

    只需使用 ClassRegistry

    $var = ClassRegistry::init("ModelName");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多