Codeigniter Active Record JSON对象中的值

编程入门 行业动态 更新时间:2024-10-26 02:30:45
本文介绍了Codeigniter Active Record JSON对象中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个问题要问.首先,我有一个表,其中父母的 parent_id 为 0 ,而孩子的 parent_id 等于父母的id.所有子代的 parent_id 存储为json编码的数组(一个子记录可以有多个父代).

I have a question to ask. Firstly, I have a table, where the parent has parent_id is 0 and the child has parent_id equal parent id. parent_id of all children are stored as a json encoded array (one child record can have many parents).

因此,当我传递一个父母ID时,如何获得父母的所有孩子.我尝试了一下,但是它不起作用,我也没有任何头绪.

So, how can I get all the children of the parent when I pass one parent id. I tried but it won't work and I don't have a clue.

这是代码:

function get_child_product($parent_id, $limit, $start) { $this -> db -> from('product'); $this -> db -> where(json_decode('parent_id'), $parent_id); $this -> db -> limit($limit, $start); $this -> db -> order_by('order', 'asc'); $this -> db -> order_by('id', 'desc'); $query = $this -> db -> get(); return $query -> result(); }

问题已解决:

function get_child_product($parent_id, $limit, $start) { $this -> db -> from('product'); $this -> db -> like('parent_id', '"' . $parent_id . '"'); $this -> db -> limit($limit, $start); $this -> db -> order_by('order', 'asc'); $this -> db -> order_by('id', 'desc'); $query = $this -> db -> get(); return $query -> result(); }

推荐答案

如果我正确理解您的数据库中具有父关系的JSON编码数组,并且您只想获得某个父项的子级.事实是,数据库中的JSON对象只不过是字符串,您无法在查询中动态解码它们并使用where子句.

If I understand correctly you have a JSON encoded array in your database with the parent relationship and you want to get only children of a certain parent. The thing is that JSON objects in a database are nothing more than strings, you cannot dynamically decode them in the query and use a where clause.

您有两个选择:

1.查询所有孩子,然后使用PHP根据解码后的JSON对其进行过滤

1.Query all your children then use PHP to filter them based on the decoded JSON

2.使用mysql like 匹配json格式的字符串

2.Use mysql like to match a string in json format

function get_child_product($parent_id, $limit, $start) { return $this -> db -> from('product') -> like('parent_id', '"parent_id":'.$parent_id) -> limit($limit, $start) -> order_by('order', 'asc') -> order_by('id', 'desc') -> get() -> result(); }

请注意, like 参数应与JSON的语法匹配,因此,如果您将id包裹在"引号中,然后将其添加到参数中

Note that the like parameter should match the syntax of your JSON so if you're ids are wrapped in " quotes, then add them to the parameter

更多推荐

Codeigniter Active Record JSON对象中的值

本文发布于:2023-10-16 10:39:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中   Active   Codeigniter   JSON   Record

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!