How are the "lft" and "rgt" fields calculated

3,939 views
Skip to first unread message

jmcbade

unread,
May 9, 2013, 4:10:36 AM5/9/13
to joomla-...@googlegroups.com
If I don't calculate them, they stay "0" as I insert rows - even if I rebuild the tree.

Can someone hep me understand the use for these,
How they are calculated
How they are re-calculated

I want to add rows to the DB tables that use these programmaticly, not with the backend for a migration. 

SP Upgrade does a fantastic job for the base language but to import Joomfish, I am still having problems.

Thanks for some help here.

-John

Mark Dexter

unread,
May 9, 2013, 9:24:22 AM5/9/13
to joomla-...@googlegroups.com
It is much simpler to use the API to insert rows into a nested table.
If you really want to do it from SQL, you could read this link:
http://www.sitepoint.com/hierarchical-data-database/. Good luck. Mark
> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! CMS Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to joomla-dev-cm...@googlegroups.com.
> To post to this group, send an email to joomla-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/joomla-dev-cms?hl=en-GB.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

John McBade

unread,
May 9, 2013, 7:42:53 PM5/9/13
to joomla-...@googlegroups.com
Mark,

Thanks for the answer.

Can you or anyone give me an example of using the API to do this?

I would want to do this from a framework basis. I'm not writing an extension for Joomla at this point.

I'm the "new kid" remember?

Thanks,

-John
> You received this message because you are subscribed to a topic in the Google Groups "Joomla! CMS Development" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-cms/pkHm7F5yer8/unsubscribe?hl=en-GB.
> To unsubscribe from this group and all of its topics, send an email to joomla-dev-cm...@googlegroups.com.

jmcbade

unread,
May 9, 2013, 8:23:08 PM5/9/13
to joomla-...@googlegroups.com
Ok, I realize that my question is likely out of scope of this group.  Maybe I could ask something like this in the framework group instead.

One of my frustrations is most things I am finding are pretty much assuming I would be using the framework to do a CMS.

If I can get some help here, of course I would greatly appreciate it.  Meanwhile I will just keep trying to figure this all out.

The value for those in the community though is if I could get more functional in this, I would be able to contribute more to the community as a whole.

That's the goal :)  Maybe I am just too new at all this to be very useful.  *sigh*  Catch-22

Thanks,

-John

Michael Babker

unread,
May 9, 2013, 8:34:39 PM5/9/13
to joomla-...@googlegroups.com
Table classes that extend from JTableNested have the logic in place to manipulate the lft/rgt fields (this affects our assets, categories, menu, tags, and usergroups tables.  Take a look at the store method for dealing with insert/update - https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/table/nested.php#L686

Mark Dexter

unread,
May 9, 2013, 9:51:16 PM5/9/13
to joomla-...@googlegroups.com
I always start with how things are done in the CMS. If you study the
save() method of the file
administrator/components/com_categories/models/category.php, you'll
see the steps done saving a category using the API. Now, a lot of
those steps might not be needed, depending on how much control you
have over the data coming in. The key method is JTableCategory store()
($table->store()). That does the SQL to insert the new row into the
table.

Then we do the rebuildPath() and rebuild() methods. I'm not sure why
we need the rebuildPath() there to be honest. If you are doing a big
import, you could probably just do one rebuild of the entire table
when you are done. You would probably need to test this.

You could also look at Matias' JUpgrade extension
(http://extensions.joomla.org/extensions/migration-a-conversion/joomla-migration/11658)
and see how he handles converting categories from 1.5 to 2.5. He must
have solved the same issue.

The most basic steps are to populate the table's properties (one per
column) and then call the table's store() method. Here is an example
of some code Hannes Papenburg wrote years ago to create a bunch of
dummy categories for testing:

<?php
$itemsPerLevel = 5;
$level = 4;

recursiveCategoryGenerator($level, $itemsPerLevel);
echo 'done';
function recursiveCategoryGenerator($level, $itemsPerLevel, $parent = 29)
{
if($level > 0)
{
for($i = 0; $i < $itemsPerLevel; $i++)
{
$table = JTable::getInstance('category');
$table->setLocation($parent, 'last-child');
$table->extension = 'com_content';
$table->title = 'Level'.$level.'Item'.$i;
$table->alias = 'level'.$level.'item'.$i;
$table->description = '';
$table->published = 1;
$tabel->access = 1;
$table->params = '{}';
$table->check();
$table->store();
//echo $table->id;var_dump($table);die;
recursiveCategoryGenerator($level - 1, $itemsPerLevel, $table->id);

}
}
}

You can see he's just populating a few of the table's fields and then
calling the check() and store() methods. The check() method just makes
sure you have the required values and that the row is valid. Note that
he is NOT setting lft and rgt. These are set for you by the store()
method.

Hope that helps a bit. Good luck. Mark

German Kalmera

unread,
May 9, 2013, 7:50:04 PM5/9/13
to joomla-...@googlegroups.com
Hi folks,

I am looking for Joomla 3.0 tutorial with a clear video.Where I can buy or have free video tutorial?

Looking forward for answers,

Regards,

German

http://www.sentrograsia.org        (IGA-TV)
RADIO-IGA 24/7- i sirbishinan djadumingu 8:00am-9:30 i 10:00-11:30am i djaluna Skol di Beibel 7:15pm lo ta LIVE)

Chris Davenport

unread,
May 10, 2013, 2:24:32 AM5/10/13
to Joomla! CMS Development
I wrote this after the first time I had to deal with JTableNested...

http://docs.joomla.org/Using_nested_sets

If anyone would like to update it or add to it, that would be great.  It was written a while back and I don't know if anything has changed in the meantime.

Chris.

Chris Davenport
Joomla Production Leadership Team

jmcbade

unread,
May 11, 2013, 9:56:08 AM5/11/13
to joomla-...@googlegroups.com
Thanks everyone.  All helpful stuff.

As I said, I am trying to first write something that runs outside of a Website context.  I presume the "CLI" approach is what I need to focus on.

Being something more than a beginner, but maybe less than an intermediate PHP coder, when I look at the tutorials, they all seem to assume that I will want to build a website using the framework. 

Maybe someone has some links or pointers on leveraging the framework, without building or running inside a website context?  I think I am a lot closer then I was. 

So, does some tutorial that might get me using the DB and doing CRUD with it outside of a website exist?


Thanks,

-John

On Thursday, May 9, 2013 5:10:36 PM UTC+9, jmcbade wrote:

elin

unread,
May 13, 2013, 2:42:36 PM5/13/13
to joomla-...@googlegroups.com
I think  you are making things too complicated.  If you have a table class that extens JTableNested 100% of the calculations are handled for you as long as you use the API for store() and delete().   You should never have to do anything else directly.

Even in a CLI context you should be using the API for your calculations and nothing else.

Here is an application I have that interacts with JTableNested in an number of places. Notice that I never do any calculation of the nesting values directly even in the worst situations.

Elin

elin

unread,
May 13, 2013, 2:45:53 PM5/13/13
to joomla-...@googlegroups.com
Also sorry these are some CLI applications. 
 It's extremely simple to run CLI Apps on the platform or framework, Andrew also has some examples as do many other people around and as does the cli folder of the cms.

Elin

jmcbade

unread,
May 21, 2013, 11:03:27 AM5/21/13
to joomla-...@googlegroups.com

Ok,

Using this code as a starting point (couple mis-typings and some other issues :)  I am MOST of the way there.  I can do categories. 

How would do I do Articles??? :)

Code snipit:

<?php
$itemsPerLevel = 5;
$level = 4;

recursiveCategoryGenerator($
level, $itemsPerLevel);
echo 'done';
function recursiveCategoryGenerator($level, $itemsPerLevel, $parent = 29)
{
        if($level > 0)
        {
                for($i = 0; $i < $itemsPerLevel; $i++)
                {
                        $table = JTable::getInstance('category');
                        $table->setLocation($parent, 'last-child');
                        $table->extension = 'com_content';
                        $table->title = 'Level'.$level.'Item'.$i;
                        $table->alias = 'level'.$level.'item'.$i;
                        $table->description = '';
                        $table->published = 1;
                        $tabel->access = 1;
                        $table->params = '{}';
                        $table->check();
                        $table->store();
                        //echo $table->id;var_dump($table);die;
                        recursiveCategoryGenerator($level - 1, $itemsPerLevel, $table->id);

                }
        }
}

Thanks!

-John

jmcbade

unread,
May 21, 2013, 11:16:22 AM5/21/13
to joomla-...@googlegroups.com
Duh!  (Maybe my skills are improving?)

Use a JTableContent object.  (FacePalm)

I THINK I have it sorted.

I may become useful here yet.  Could happen :)

Sure am trying!


-John

On Thursday, May 9, 2013 5:10:36 PM UTC+9, jmcbade wrote:
Reply all
Reply to author
Forward
0 new messages