Using JTable class in a front end extension

945 views
Skip to first unread message

Mr Goose

unread,
Jun 9, 2011, 11:28:30 AM6/9/11
to Joomla! General Development
Hello everyone, I've been lurking here for some time but this is my
first post. TBH, I've been slightly awestruck both by the complexity
of extension writing and the amazing skills and depth of knowledge of
the contributors to this group. A few weeks back I finally got up
enough courage to start working on a Joomla extension of my own.Now
I'm stuck because I cannot get my head around JTable class - or one
particular aspect of it:-

How do you use a Jtable class file in a front-end extension? That is,
how do you stop your front-end application calling the tables/
mycomponent,php (or whatever you call it) from the backend, i.e.
joomla/administrator/components/com_mycomponent/tables/mycomponent,php

This has me completely baffled because I see no reference in my front-
end code to the back-end joomla/administrator/components/
com_mycomponent/tables/mycomponent,php file. So how does it know it's
there and why can't I put it somewhere else? - i,e. in my front end:-
joomla/components/com_mycomponent/tables/mycomponent,php ?

Best wishes, Garf.

editor

unread,
Jun 9, 2011, 11:44:34 AM6/9/11
to Joomla! General Development
Further to my last, please forgive my silly typos. I meant:-

joomla/administrator/components/com_mycomponent/tables/mycomponent.php
joomla/components/com_mycomponent/tables/mycomponent,php

with a "." not a ","! lol. Sorry. Best wises, G.

Anju Wijeawardana

unread,
Jun 9, 2011, 11:51:15 AM6/9/11
to joomla-de...@googlegroups.com
is there any search module or plugin for ck forms

Andrew Eddie

unread,
Jun 9, 2011, 7:18:30 PM6/9/11
to joomla-de...@googlegroups.com
Hi Garf

Generally you just need to put your JTable classes in one location,
and I, by convention, usually put them in the administrator component.

You generally only need to use JTable for data manipulation. You'd
only use that on the frontend if you were doing an edit operation.
It's a fairly heavy class to be using to just "view" a record. If you
are using 1.6, the JTable class is no longer loaded by default as it
is in 1.5 (used to update the session table).

Does that help?

Regards,
Andrew Eddie
http://learn.theartofjoomla.com - training videos for Joomla 1.6 developers

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>
>

Tobias Wolf

unread,
Jun 14, 2011, 5:17:39 AM6/14/11
to joomla-de...@googlegroups.com
Hello,

Am 09.06.2011 um 17:28 schrieb Mr Goose:

This has me completely baffled because I see no reference in my front-
end code to the back-end joomla/administrator/components/
com_mycomponent/tables/mycomponent,php file. So how does it know it's
there and why can't I put it somewhere else? - i,e. in my front end:-
joomla/components/com_mycomponent/tables/mycomponent,php ?

i don't know if i can answer this properly, but i will take a shot. If i am completely wrong, maybe other will correct me ;)

To better understand the internal ongoings inside Joomla i have stepped through the php files with a debugger.

For Joomla to find its files and the references, it depends heavily on naming conventions. It just take the component name, strips off the 'com_' part, maybe uppercase the first character
and then searches for such a filename with with a properly named class.

This how Joomla sometimes seems to find the proper files and classes in a 'magic' way, but if you forgot to name your files accordingly, somehow everything breaks.

Hope i could help a little bit ...

Regards,

Tobias

Mr Goose

unread,
Jun 14, 2011, 3:27:35 PM6/14/11
to Joomla! General Development
Thanks for you help guys.

@ Andrew. I have stuck very closely to the naming conventions, and my
component *does* require editing at the front end. In fact there is no
real need for an admin part at all because registered users will use
and manage their own data. I am using 1.5 - though I appreciate I will
need to move to 1.6 in due course.

Is there a way to make my front-end component 1.5 reference a tables/
mycomponent.php file in the front end rather than the one in
administrator section?

Best wishes, G.

On Jun 10, 12:18 am, Andrew Eddie <mambob...@gmail.com> wrote:
> Hi Garf
>
> Generally you just need to put your JTable classes in one location,
> and I, by convention, usually put them in the administrator component.
>
> You generally only need to use JTable for data manipulation.  You'd
> only use that on the frontend if you were doing an edit operation.
> It's a fairly heavy class to be using to just "view" a record.  If you
> are using 1.6, the JTable class is no longer loaded by default as it
> is in 1.5 (used to update the session table).
>
> Does that help?
>
> Regards,
> Andrew Eddiehttp://learn.theartofjoomla.com- training videos for Joomla 1.6 developers

Herman Peeren

unread,
Jun 15, 2011, 2:02:55 AM6/15/11
to joomla-de...@googlegroups.com
I often use front-end editing in my custom (1.5) components: they are written for clients, where some employees (or members or whatever) have editing access to that component, but certainly not to the backend of the site. So, those components don't have files in /administrator/components. That's why I also put the /tables in the frontend: to keep everything together.

Here is an example how I use a sales-JTable in the frontend model:

        JTable::addIncludePath(JPATH_COMPONENT.'/tables');
        $salestable = JTable::getInstance('Sales', 'Table');

And then you can further use that table as you usually do (binding, etc.). Hope this helps you.

I first tried to instantiate that JTable-subclass as you would do in the backend:

$salestable =  $this->getTable('sales');

but that doesn't work. Probably because my /tables-folder is also in the frontend. Stopped figuring out alternative ways to get it working after I started using the above outlined method with addIncludePath and directly instantiating the JTable. Sorry for the terrible hardcoding of the table-name; not the nicest code-example of someone who normally talks about "dependency injection" etc... ;-)

I use JTable when possible: to keep all table-related Create-Read-Update-Delete (CRUD) as much as possible in one place.  Limitation is for instance that the primary key (PK) cannot be complex; in that case you either have to add an extra single-field PK to the table or have ot fall back to defining all CRUD yourself in the model (or get the help of other frameworks like Doctrine or Nooku that handle this better).

Herman Peeren

unread,
Jun 15, 2011, 3:21:09 PM6/15/11
to Joomla! General Development
You can also use only model-methods to use the above sales-JTable in
the frontend:

$this->addTablePath(JPATH_COMPONENT.'/tables');
$salestable = $this->getTable('Sales');

The same things happen as when you directly call
JTable::addIncludePath() and JTable::getInstance().

Before you add the table-path there are 2 paths in the $paths static
variable of JTable: /libraries/joomla/database/table (for the "common"
tables like "session", "user", but at the moment also still "content")
and administrator/components/com_yourcomponent/tables (so: in the
administrator-part).

Adding the frontend-path to the /tables (or putting the tables in the
backend) are necessary for the frontend use of the JTable-subclasses.

Gunjan Patel

unread,
Jun 16, 2011, 12:38:35 AM6/16/11
to Joomla! General Development
In models you can use table directly,

$row = & $this->getTable ( <table_name> );

If you want to use it in same component helper file or else than,

$row =& JTable::getInstance(<table_name>,<table_prefix optional>);


If you want to use it in other component,

JTable::addIncludePath(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_example'.DS.'tables');
$row =& JTable::getInstance(<table_name>,<table_prefix optional>);



On 16 June, 00:21, Herman Peeren <herman.pee...@gmail.com> wrote:
> You can also use only model-methods to use the above sales-JTablein
> the frontend:
>
>         $this->addTablePath(JPATH_COMPONENT.'/tables');
>         $salestable = $this->getTable('Sales');
>
> The same things happen as when you directly callJTable::addIncludePath() andJTable::getInstance().
>
> Before you add the table-path there are 2 paths in the $paths static
> variable ofJTable: /libraries/joomla/database/table (for the "common"

Mr Goose

unread,
Jun 25, 2011, 12:45:13 PM6/25/11
to Joomla! General Development
Thanks for all your help everyone. It was driving me crazy trying to
figure it out. Actually, I decided to go away and work on another part
of the project, in order to preserve my own sanity. I only returned to
the tables issue this afternoon and put some of your suggestions to
the test. I am delighted (and relieved) to report that Herman's
solution worked perfectly. In a nutshell, I replaced the the line:-

$row =& $this->getTable();

In my "joomla/components/com_mycomponent/models/mycomponent.php"

with:-

JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
$row = JTable::getInstance('MyComponent', 'Table');

This forced the model to use the TableMyComponent class in my
"joomla/components/com_mycomponent/tables/mycomponent.php" file

In other words, it read the tables file in my front end, not the
backend, at last! Problem solved!

Many, many thanks to everyone who contributed towards the solution.
Your efforts are very much appreciated.

Best wishes, Garf.
Reply all
Reply to author
Forward
0 new messages