Welcome Guest, Not a member yet? Register   Sign In
Using MySQL JSON Columns in query builder class
#1

I need to work with json data in mysql column. So,I've written the mysql query(for instance JSON_EXTRACT method) like this:

PHP Code:
$query $this->db->query("select * FROM articles WHERE JSON_EXTRACT(`status` , '$.\"1\".\"is_delete\"') > 0")->getResultObject(); 


In action, this work truly. Now, I need to change my query to Query Builder Class like this:

PHP Code:
$builder $this->db->table('articles')->select('*')->where('status > '"JSON_EXTRACT(`status` , '$.\"1\".\"is_delete\"')");
$builder->get()->getResult(); 

In action, in query builder class, my query doesn't work truly. what's wrong with query builder class?! How to work with the query builder class in this case?!
Reply
#2

See: http://codeigniter.com/user_guide/databa...cific-data

You can use a string or RawSQL
Reply
#3

@superior 
Sure, I knew. But both of them have had a bug(table alias name) in CI4 +. I need to add mysql table name alias, after that, string or RawSQL doesn't work. For instance:

PHP Code:
$whereClause "JSON_EXTRACT(`artl.status` , '$.\"1\".\"is_delete\"') > 0";

//With where string

$builder $this->db->table('articles artl')->select('*')->where($whereClause)->get()->getResult();

//With RawSql

$builder $this->db->table('articles artl')->select('*')->where(new RawSql($whereClause))->get()->getResult(); 

Now we see this error and CI4 can't find the column:

Code:
Unknown column 'artl.status' in 'where clause'
Reply
#4

Look at the quotes in your where caluse.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

@InsiteFX 

you right, I remove `` and my problem is fixed. thanks.

PHP Code:
$whereClause "JSON_EXTRACT(artl.status , '$.\"1\".\"is_delete\"') > 0"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB