PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

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

 December 31, 2021     cakephp, cakephp-3.0     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing