如何在yii中使用相同名称的2个类模型之间做出改变?

时间:2015-03-26 09:47:52

标签: php yii

我有两个模块。它们中的每一个都具有相同的模型类名。在模块文件中,我加载了模型:

  $this->setImport(array(
            'cars.models.*',
            'cars.components.*',
            'application.modules.mymodel.models.*',
            'application.modules.myanothermodel.models.*',
        ));
    }

在我的情况下,mymodel和myanothermodel有一个同名的类( MyClass ),但功能不同。所以当我在控制器的汽车模块中调用它时

$mymodel = MyClass::model()->find('month = :month and year = :year and user_id = :user', array('month' => $month, 'year' => $year, 'user' => $user->id));

结果总是从application.modules.myanothermodel.models获取模型。有可能在它们之间产生某种差异吗?

1 个答案:

答案 0 :(得分:0)

是的,有。甚至在纯PHP中。使用namespaces表示

namespace models\mymodel\models;

class MyClass {}

namespace models\myanothermodel\models;

class MyClass {}

class AnotherClass {
    $classA = new \models\myanothermodel\models\MyClass();

/* or */

use \models\myanothermodel\models\MyClass;

class AnotherClass {
    $classA = new MyClass();

此外,您必须导入您使用的课​​程(手动调用require或使用autoloading