Friday, December 31, 2021

[FIXED] I have to contain two models with json field value in cakphp

Issue

log table with product id product table My Log tables contained data field having json data like

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

I want to join products table with the product id stored in data file.How can I do that?

log table "product-id": "14" this field need to be contained with product table.

$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);

Solution

Add this code Log association model

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


Answered By - Priyanka Hazra

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.