version that can work with PDO as well as with MySQL, depending on
what database connection u use in dbconnect.inc.php. I timed the
results with microtime() before and after the function call and for a
tree with 755 nodes to an extent of maximum 5 levels it took PDO about
0.73 seconds to echo all the SQL queries to the browser, whereas MySQL
takes 1.22 seconds for the exact same operation
(P.S.: I don't mean this thread to change into an off-topic PDO vs.
MySQL bash. Just sayin'):
Here's the code (you can make your own dbconnect.inc.php file ;) )
<?php
require("./dbconnect.inc.php");
function rebuild_tree($parent_id, $left, $dbtype)
{
global $dbh; // this is my PDO handle
global $dbh2; // this is my MySQL handle
$right = $left+1;
$sql = "SELECT id FROM keywords WHERE parent_id='" . $parent_id . "'
AND approved = 1";
//echo $sql;
switch($dbtype)
{
case "MYSQL":
$res = mysql_query($sql, $dbh2);
while($row = mysql_fetch_assoc($res))
{
$right = rebuild_tree($row['id'], $right, $dbtype);
}
break;
case "PDO":
$stmt = $dbh -> prepare($sql);
$stmt -> execute();
while ($row = $stmt -> fetch(PDO::FETCH_ASSOC))
{
$right = rebuild_tree($row['id'], $right,$dbtype);
}
break;
default:
echo 'wtf?';
break;
}
echo('UPDATE keywords SET lft='.$left.', rght='. $right.' WHERE id="'.
$parent_id.'";' . "\n");
return $right+1;
}
// call the function or it won't do anything :)
rebuild_tree(0, 1, "PDO"); // replace PDO with MYSQL if that's the
connection you use
?>
You can copy the browser output and paste it into the SQL tab of
PHPMyAdmin. I find this method to be safer because it allows me to see
the queries before they're executed (and possibly see empty fields or
syntax errors before they're committed to the DB)
Best regards,
Marv
On Sep 25, 12:04 am, marvelade <marvel...@gmail.com> wrote:
> Good news and bad news:
>
> I presumably found a way:
>
> <?php
>
> require("./dbconnect.inc.php");
>
> function rebuild_tree($parent_id, $left)
> {
> global $dbh;
>
> // the right value of this node is the left value + 1
> $right = $left+1;
>
> // get all children of this node
> $stmt = $dbh -> prepare("SELECT id,keyword FROM keywords WHERE
> parent_id='" . $parent . "' AND approved = 1");
> $stmt -> execute();
> while ($row = $stmt -> fetch(PDO::FETCH_ASSOC))
> {
> $right = rebuild_tree($row['id'], $right);
> }
>
> // we've got the left value, and now that we've processed
> // the children of this node we also know the right value
> echo('UPDATE categories SET lft='.$left.', rght='.
> $right.' WHERE id="'.$parent_id.'";' . "\n");
>
> // return the right value of this node + 1
> return $right+1;
> }
>
> rebuild_tree('0',1);
>
> ?>
>
> Bad news : my server doesn't want to do it due to memory issues:
>
> Fatal error: Out of memory (allocated 5505024) (tried to allocate 40
> bytes) in /customers/nonimportante.es/nonimportante.es/httpd.www/mptt/
> build_lr_query.php on line 14
>
> Any suggestions?
>
> thx + greetz,
> Marv
>
> On Sep 24, 7:45 pm, cricket <zijn.digi...@gmail.com> wrote:
>
> > On Fri, Sep 24, 2010 at 11:49 AM, marvelade <marvel...@gmail.com> wrote:
>
> > > I suppose I need to add 'lft' and 'rght' fields to my database but how
> > > do I calculate these values? I never heard of MPTT before and I think
> > > I understand the basics after reading an article about it (http://
> > > articles.sitepoint.com/article/hierarchical-data-database/2), but I
> > > can't really think of an algorythm that would update an existing
> > > database table when only the parent info is available...
>
> > > Any pointers?
>
> > I once tackled that very problem but wasn't successful. I still think
> > it must be possible to write a script to do it but, in the end, I just
> > created a text file and totted through it "by hand". I did search
> > online and, I believe, posted a query to this list, but couldn't find
> > anything.
>
> > > Can I use the CakePHP tree functionality with just the parent_id info
> > > (I'd guess not?) ?
>
> > I'm not sure. I suspect not. It seems to me, though, that MPTT would
> > be the way to go for what you're doing.
>
> > BTW, do check out this article:http://bakery.cakephp.org/articles/view/tree-helper-1
>
> > It's a little bit confusing but it's definitely useful. I think it'd
> > suit your situation. Let me know if you need some pointers.
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment