【问题标题】:I have to contain two models with json field value in cakphp我必须在 cakephp 中包含两个具有 json 字段值的模型
【发布时间】:2020-09-10 05:53:55
【问题描述】:

log table with product id product table 我的日志表包含具有 json 数据的数据字段,例如

{"product-id":"14","product-name":"test","product-url":"\/projects\/test\/products\/lecture-details\/8","product-type":"lecture","product-price":"0"}

我想使用存储在数据文件中的产品 ID 加入产品表。我该怎么做?

log table "product-id": "14" 此字段需要包含在产品表中。

$getQuery = $this->Log->find('all')->matching('Users', function ($q) { return $q->where(['Users.is_deleted' => 'n']);})->contain(['Users','Products'])->select(['user_id' => 'Users.id', 'username' => 'Users.username','fname' => 'Users.fname','lname' => 'Users.lname', 'email' => 'Users.email','view_date' => 'Log.dt_created_on','data' => 'Log.data'])->where($conditions);

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    添加此代码日志关联模型

    $this->hasOne('Products', [
          'className' => 'Products',
          'foreignKey' =>false,
          'conditions' => array("Products.id=JSON_VALUE(cast(log.data as nvarchar(256)),'$.\"product-id\"')")
    ]);
    

    【讨论】:

    • 它在服务器上不起作用。 SQL 错误 [207] [S0001]:列名“$.product-id”无效。
    • 你应该只转换数据类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2016-08-30
    • 2019-04-13
    • 2019-03-24
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多