【问题标题】:Save data to multiple database table with one model cakephp 3使用一个模型 cakephp 3 将数据保存到多个数据库表
【发布时间】:2019-06-05 04:28:30
【问题描述】:

我有两个具有同一张表的数据库。

db1.wafer_types 表:-

ID    waferType   name
1071  req_1       req_1_name
1072  req_2       req_2_name
1073  req_3       req_3_name

db2.wafer_types 表:-

ID   wafer_type_id    waferType   name
1    1071  req_1       req_1_name
2    1072  req_2       req_2_name
3    1073  req_3       req_3_name

模型(WaferTypesTable)

class WaferTypesTable extends Table {    
    public function initialize(array $config) {
        parent::initialize($config);

        $this->setTable('wafer_types');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');        
    }
}

实体(晶圆类型)

class WaferType extends Entity
{
    protected $_accessible = [
         '*' => true,
        'id' => false
    ];
}

控制器功能:-

public function waferTypeHistory($id,$userid){        
    $waferType = $this->WaferTypes->find()->where(['id'=>$id])->hydrate(false)->toArray();          
    $connection = ConnectionManager::get('default_history');
    $this->WaferTypes->setConnection($connection);
    $waferTypeHistory = $this->WaferTypes->newEntity();        
    $waferTypeHistory = $this->WaferTypes->patchEntity($waferTypeHistory, $waferType[0]);
    $waferTypeHistory->wafer_type_id = $id;
    $this->WaferTypes->save($waferTypeHistory);
    return true;
}

在 db2.wafer_types 表中成功插入数据。但是wafer_type_id 归档得到值'0'。我给它赋值,但它总是得到值'0'。 知道为什么它每次都获得价值'0'。

【问题讨论】:

    标签: cakephp orm cakephp-3.0


    【解决方案1】:

    问题可能是缓存模式,db1.wafer_types 没有wafer_type_id 列,因此 ORM 不会将其包含在保存过程中。

    您可以在更改连接后通过取消设置架构来测试它,如下所示:

    $this->WaferTypes->setSchema(null);
    

    我建议改用单独的(扩展)模型,您可以将其配置为默认使用db2 连接。一直在摸索连接和架构似乎是一个相当不稳定的解决方案。

    另见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      相关资源
      最近更新 更多