简体   繁体   中英

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. 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. 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. I don't know how Lithium could handle all the "AS"'s and whatnot. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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