我正在检索叶子的路径,并且嵌套集中有重复的叶子名称。执行查询后,将重复叶名的不同路径合并在一起。
我试图修复sql代码,但无法达到预期的结果
foreach($leafs as $id=>$leaf) {
$sql_fetch = 'SELECT parent.name, node.category_id '
. 'FROM vender_category AS node, '
. 'vender_category AS parent '
.'WHERE (node.lft BETWEEN parent.lft AND parent.rgt) '
.'AND node.name = "'.$leaf['name'].'" '
.'ORDER BY parent.lft ';
$venderCategories[] = Yii::app()->db->createCommand($sql_fetch)->queryAll();
}
$categoryChains = array();
foreach ($venderCategories as $key => $venderCategory) {
$categoryChains[] = array(
'name' => implode(' / ', array_column($venderCategory, 'name')),
'category_id' => implode(array_unique( array_column($venderCategory, 'category_id'))),
);
}
此代码将具有相同名称的Leaf路径合并在一起
array ( 'name' => 'smartphone / accessories / car / accessories' );
我想要2个同名'accessories'的叶子像这样分开:
array ( 'name' => 'smartphone / accessories );
array ( 'name' => 'car / accessories' );
谢谢!
答案 0 :(得分:0)
实际上这很容易,但是由于我是编程新手,所以对我来说很难:)
这是我的解决方法。只需添加一个条件即可查询。
$sql_fetch = 'SELECT parent.name, node.category_id '
. 'FROM vender_category AS node, '
. 'vender_category AS parent '
.'WHERE (node.lft BETWEEN parent.lft AND parent.rgt) '
.'AND node.name = "'.$leaf['name'].'" '
**.'AND node.category_id = "'.$leaf['category_id']. '" '**
.'ORDER BY parent.lft ';