简体   繁体   English

Lithium Framework MySQL原始查询

[英]Lithium Framework MySQL raw query

I am fairly new to Lithium PHP Framework but I do know how to use the basic database abstraction method. 我对Lithium PHP Framework相当陌生,但我确实知道如何使用基本的数据库抽象方法。 I am using a plugin to build menu trees and it works great, although I want to use Lithium's abstraction layer for the functions. 我正在使用一个插件来构建菜单树,并且效果很好,尽管我想对功能使用Lithium的抽象层。 I'm stuck on this: 我坚持这一点:

public function get_menuItems($menu_id) {
  // retrieve the left and right value of the $root node  
  $result = $this->database->query("SELECT * FROM ".$this->tbl_menu_items." WHERE menu_id = '$menu_id'");
  if ($this->database->num_rows($result)) {
    $right = array();
    $result = $this->database->query("SELECT node.title, node.type, node.class_name, node.content, node.id AS id, node.lft AS lft, node.rgt AS rgt, (COUNT(parent.title) - 1) AS depth FROM ".$this->tbl_menu_items." AS node CROSS JOIN ".$this->tbl_menu_items." AS parent WHERE node.menu_id = '$menu_id' AND parent.menu_id = '$menu_id' AND node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.id ORDER BY node.lft");
    $tree = array();
    while ($row = mysql_fetch_assoc($result)) {
      $tree[] = $row;
    }
  } else {
    $tree = false;
  }
  return $tree;
}

More so the $result part. $result部分更是如此。 I don't know how Lithium could handle all the "AS"'s and whatnot. 我不知道锂怎么能处理所有的“ AS”之类的东西。 I tried a few different ways to execute the query as is but no luck. 我尝试了几种不同的方式执行查询,但是没有运气。

Any help would be greatly appreciated and I hope I am explaining it well enough. 任何帮助将不胜感激,我希望我能解释得足够好。

If your php version is 5.4 you can use li3_tree behavior . 如果您的php版本是5.4,则可以使用li3_tree行为 Take a look at it, as it is a good example on how to implement such recursive behavior. 看一下它,因为它是如何实现这种递归行为的一个很好的例子。

Also, I would strongly suggest you to take a look at "Using Models" and "Adding functionality to Lithium models" as your example code could benefit a lot. 另外,我强烈建议您看一下“使用模型”“向锂模型添加功能”,因为您的示例代码可能会受益匪浅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM