我必须在cakphp中包含两个具有json字段值的模型

问题描述

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存储在数据文件中。该怎么做?

日志表“ 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);

解决方法

添加此代码日志关联模型

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