如何绑定覆盖构造函数的模型



我有两个模型定义在我的Cakephp应用程序

Customer.php

class Customer extends AppModel {
    var $name = 'Customer';
    public function Customer($tableId){
    //.. code to assign a table name based on $tableId
    return parent::__construct("id", $this->useTable);
  }
}

CustomerOrder.php

class CustomerOrder extends AppModel {
    var $name = 'CustomerOrder';
    var $belongsTo = array('Customer ' => array(
            'className' => 'Customer',
            'foreignKey' => 'customer_id'
        ));
}

在这里,客户模型将根据构造函数参数动态地从三个mysql表中获取其数据库表名。当我查询第二个模型,因为客户已经覆盖了构造函数,我得到mysql错误,因为它不能调用正确的构造函数在绑定模型。

$belongsTo中是否有额外的参数来执行此操作?或者怎样才能做到这一点。

我想你是在用另一种语言的方式构造这个类。在php中,构造函数类似于

class Customer extends AppModel {
    var $name = 'Customer';  //note: this line is really not necessary
    public function __construct($id = false, $table = null, $ds = null) {
        //.. code to assign a table name based on $tableId
        return parent::__construct("id", $this->useTable, $ds);
    }
}

如果你做了那个改变,它还会给你那个错误吗?

(顺便说一句,$ds是数据源连接名称,您可以查看代码以获取更多信息)

最新更新