Using JDatabase in the Component Tutorial

170 views
Skip to first unread message

jjm...@juno.com

unread,
Feb 5, 2019, 11:58:22 AM2/5/19
to Joomla! General Development
I have been trying to change the code in 3.x component tutorial to allow for a table return of data as opposed to the JTable one row of data that is included in the tutorial. I am fairly new to php and joomla programming. When I try to use JDatabase it pops the abstract class error. What is the best way to go about using the JDAtabase method?

jjm...@juno.com

unread,
Feb 6, 2019, 9:01:23 PM2/6/19
to Joomla! General Development
Thanks for the response.. I don't think I asked the right question.


This is the part I'm struggling with 

/**
* HelloWorld Model
*
* @since  0.0.1
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var array messages
*/
protected $messages;

/**
* Method to get a table object, load it if necessary.
*
* @param   string  $type    The table name. Optional.
* @param   string  $prefix  The class prefix. Optional.
* @param   array   $config  Configuration array for model. Optional.
*
* @return  JTable  A JTable object
*
* @since   1.6
*/
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config); Since JTable only returns a row and JDatabase is an abstract class how can I rewrite this piece of code to work within the component frame work. The answer may be I cant. I'm just not sure

Alexandre Ponton

unread,
Feb 6, 2019, 9:35:11 PM2/6/19
to joomla-de...@googlegroups.com
I am not sure what you are trying to accomplish but if you want to retreive information from a database tables, you might want to consider using JDatabase.
Also, if you are new to PHP and Joomla, I suggest that you play with 'Modules' instead of 'Components' since it might be easier to learn and see the results.

Here is a quick example of using JDatabase. (https://docs.joomla.org/Selecting_data_using_JDatabase)
// Set your query into a variable.
$query = "SELECT * FROM #__categories";
//Initialize database connection
$db = JFactory::getDbo();
//Set your query into the database connection object.
$db->setQuery($query);
// Execute the query and return the results on $results as an Object.
$results = $db->loadObject();
// Print the object(s) value(s).
echo '<pre>';
print_r($results);

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Glenn Arkell

unread,
Feb 6, 2019, 10:02:15 PM2/6/19
to Joomla! General Development
Ok, so why would you want to get more than one record from within the JModelItem class?  If you want more than one record then you should be working in the JModelList class to get multiples.  So like Alexandre, I'm not sure of what you are trying to achieve within the "singular" processes.  Cheers.
Glenn

Josh

unread,
Feb 7, 2019, 10:36:15 AM2/7/19
to joomla-de...@googlegroups.com
Alexandre and Glenn I appreciate the responses. I will take a look at the jmodellist class to see if I can use that. I am trying to return a table of data and was following the tutorial. My biggest struggle was finding the correct class to use. With both the 2.5 and 3.0 versions I was getting lost. Thanks again

Sent from my iPhone
--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.


____________________________________________________________
Judge Judy Steps Down After 23 Years Over This Controversy
glancence-hality.com
http://thirdpartyoffers.juno.com/TGL3132/5c5c506391e6e50627d3fst03vuc
SponsoredBy Content.Ad

Glenn Arkell

unread,
Feb 7, 2019, 4:15:23 PM2/7/19
to Joomla! General Development
Hi Josh,
Can I suggest you create a free account with Joomla Component Creator (https://www.component-creator.com/en/) and create a simple component with a field or two in the database table, generate the component and then read through the file structure that is created.  This should give you a very good example of the MVC structure showing the singular model and plural model and how they work.  Then go back to the tutorial to see how the tutorial is trying to explain the steps involved.  This is what I did to start learning Joomla! and I found it invaluable.  I still periodically go back to Component Creator to find what new things they have implemented into this great tool.  Hope this helps.  Cheers.
Glenn
To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsub...@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Josh

unread,
Feb 7, 2019, 5:30:04 PM2/7/19
to joomla-de...@googlegroups.com
Thanks! I will definitely take a look. I’ve been working thru sql, php and joints. It’s pretty easy to get bogged down with all of the information out there

Sent from my iPhone
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.

Josh

unread,
Feb 13, 2019, 1:58:03 PM2/13/19
to joomla-de...@googlegroups.com
Glenn

I’ve used the component creator and found how to return a table of data. The problem now is I can’t find the actual sql statement. I’ve looked through every file a couple if times. While I find items that look like it should call data in the table it doesn’t do anything to change them. If you have any insights let me know. I will keep trying to break things. 

Thanks 
Josh

Sent from my iPhone
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.


____________________________________________________________
Sad News For Meghan Markle And Prince Harry
track.volutrk.com
http://thirdpartyoffers.juno.com/TGL3132/5c64689e8bf98689d768cst02vuc
SponsoredBy Content.Ad

Mark Dexter

unread,
Feb 13, 2019, 2:21:32 PM2/13/19
to Joomla! General Development
To update or insert a row using JTable, you set the columns using the JTable properties ($myTable->myColumnName = 'myValue';) and then user the store() method to save the row to the database ($myTable->store();). If you look at the store() method in JTable, you will see the SQL statements there. Hope that helps.

Mark Dexter

unread,
Feb 13, 2019, 2:26:04 PM2/13/19
to Joomla! General Development
Actually, I spoke to soon. The Joomla\Database\Table::store() method calls JDatabaseDriver:updateObject() or JDatabaseDriver::insertObject() to do the actual SQL statements.

Josh

unread,
Feb 13, 2019, 9:03:17 PM2/13/19
to joomla-de...@googlegroups.com
Mark. Thanks for the quick response. I’m not sure I asked the right question. I’m looking for something like select(*) from #__table. In the Joomlas example it’s found in the admin/models/fields/helloworld.php
I don’t see a select statement in the corresponding file
Sent from my iPhone
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.


____________________________________________________________
Oncologists Freak Out Over True Cause of Cancer
pro.healthresponses.org
http://thirdpartyoffers.juno.com/TGL3132/5c64cc436aeea4c4256dast01duc
SponsoredBy Content.Ad

Hannes Papenberg

unread,
Feb 14, 2019, 6:27:33 AM2/14/19
to joomla-de...@googlegroups.com
You are looking for the model, which would normally be in
/administrator/components/com_whatever/models/<modelname>.php

In the backend normally you have a singular named model for the single
item and a plural named model for the list. For example article and
articles. Articles contains the query that you are looking for.

Am 14.02.2019 um 03:02 schrieb Josh:
> Mark. Thanks for the quick response. I’m not sure I asked the right
> question. I’m looking for something like select(*) from #__table. In the
> Joomlas example it’s found in the admin/models/fields/helloworld.php
> I don’t see a select statement in the corresponding file
> Sent from my iPhone
>
> On Feb 13, 2019, at 1:26 PM, Mark Dexter <dexter...@gmail.com
>>> <https://www.component-creator.com/en/>) and create a simple
>>>> **//___^
>>>> On Tuesday, February 5, 2019 at 10:58:22 AM UTC-6,
>>>> jjm...@juno.com wrote:
>>>>
>>>> I have been trying to change the code in 3.x
>>>> component tutorial to allow for a table return
>>>> of data as opposed to the JTable one row of data
>>>> that is included in the tutorial. I am fairly
>>>> new to php and joomla programming. When I try to
>>>> use JDatabase it pops the abstract class error.
>>>> What is the best way to go about using the
>>>> JDAtabase method?
>>>>
>>>> --
>>>> You received this message because you are subscribed to
>>>> a topic in the Google Groups "Joomla! General
>>>> Development" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe
>>>> <https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe>.
>>>> To unsubscribe from this group and all its topics, send
>>>> an email to joomla-dev-gene...@googlegroups.com.
>>>> To post to this group, send email to
>>>> joomla-de...@googlegroups.com.
>>>> Visit this group at
>>>> https://groups.google.com/group/joomla-dev-general
>>>> <https://groups.google.com/group/joomla-dev-general>.
>>>> For more options, visit
>>>> https://groups.google.com/d/optout
>>>> <https://groups.google.com/d/optout>.
>>>
>>>
>>> ____________________________________________________________
>>> *Judge Judy Steps Down After 23 Years Over This Controversy*
>>> glancence-hality.com
>>> <http://thirdpartyoffers.juno.com/TGL3132/5c5c506391e6e50627d3fst03vuc>http://thirdpartyoffers.juno.com/TGL3132/5c5c506391e6e50627d3fst03vuc
>>> <http://thirdpartyoffers.juno.com/TGL3132/5c5c506391e6e50627d3fst03vuc>
>>> SponsoredBy Content.Ad
>>>
>>> --
>>> You received this message because you are subscribed to a
>>> topic in the Google Groups "Joomla! General Development" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe
>>> <https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe>.
>>> To unsubscribe from this group and all its topics, send an
>>> email to joomla-dev-gene...@googlegroups.com.
>>> To post to this group, send email to
>>> joomla-de...@googlegroups.com.
>>> Visit this group at
>>> https://groups.google.com/group/joomla-dev-general
>>> <https://groups.google.com/group/joomla-dev-general>.
>>> For more options, visit https://groups.google.com/d/optout
>>> <https://groups.google.com/d/optout>.
>>
>>
>> ____________________________________________________________
>> *Sad News For Meghan Markle And Prince Harry*
>> track.volutrk.com
>> <http://thirdpartyoffers.juno.com/TGL3132/5c64689e8bf98689d768cst02vuc>http://thirdpartyoffers.juno.com/TGL3132/5c64689e8bf98689d768cst02vuc
>> <http://thirdpartyoffers.juno.com/TGL3132/5c64689e8bf98689d768cst02vuc>
>> SponsoredBy Content.Ad
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Joomla! General Development" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> joomla-dev-gene...@googlegroups.com
>> <mailto:joomla-dev-gene...@googlegroups.com>.
>> To post to this group, send email to
>> joomla-de...@googlegroups.com
>> <mailto:joomla-de...@googlegroups.com>.
>> Visit this group at https://groups.google.com/group/joomla-dev-general.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> ____________________________________________________________
> *Oncologists Freak Out Over True Cause of Cancer*
> pro.healthresponses.org
> <http://thirdpartyoffers.juno.com/TGL3132/5c64cc436aeea4c4256dast01duc>http://thirdpartyoffers.juno.com/TGL3132/5c64cc436aeea4c4256dast01duc
> SponsoredBy Content.Ad
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.

EF Duggan

unread,
Feb 15, 2019, 11:53:47 AM2/15/19
to Joomla! General Development
If you want to see the actual select statement that is produced, turn on debugging. See https://docs.joomla.org/How_to_debug_your_code#Database_Queries

regards,
Edmond

Josh

unread,
Feb 15, 2019, 5:53:20 PM2/15/19
to joomla-de...@googlegroups.com
Thanks to everyone! I will keep digging and using your suggestions 

Sent from my iPhone
To unsubscribe from this group and all its topics, send an email to joomla-dev-gene...@googlegroups.com.

To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.


____________________________________________________________
Sad News For Meghan Markle And Prince Harry
track.volutrk.com
http://thirdpartyoffers.juno.com/TGL3132/5c6742c3653bd42c21c03st01vuc
SponsoredBy Content.Ad

jjm...@juno.com

unread,
Feb 17, 2019, 7:43:14 PM2/17/19
to joomla-de...@googlegroups.com
So I've found the location of the query that will change the table I want to return. It is on the site side of the component. I thought that the query was supposed to be on the admin side of the component? Also I can change tables but there seems to be something odd going on. When trying to change to different tables I get the error Unknown column 'a.name' in 'order clause'. I;m pretty sure I need to find the order clause but I am having trouble finding it. 
 
Thanks
Josh

Josh

unread,
Feb 17, 2019, 8:12:40 PM2/17/19
to joomla-de...@googlegroups.com
Never mind found it

Sent from my iPhone


____________________________________________________________
Drink 1 Cup Before Bed, Watch Your Body Fat Melt Like Crazy
mayserve-magestor.com
http://thirdpartyoffers.juno.com/TGL3132/5c6a066a3c7ac66952e6st03duc
SponsoredBy Content.Ad

jjm...@juno.com

unread,
Feb 21, 2019, 9:11:43 PM2/21/19
to joomla-de...@googlegroups.com, joomla-de...@googlegroups.com
Newest question. 
 
 
I have the code below in which I am trying to JOIN another table in the query. I believe the sql is correct but I cannot get it to return the data. It out puts a heading in the table that is  of the form COM_MEMBERS_I_MEMBERS_MEMBER_STATUS. THis last bit being pulled from the default.php in the component structure. WHat part of this am I missing to get the data returned along with the correct table heading?
 
Any help is much appreciated. 
 
Josh
	protected function getListQuery()
	{
            // Create a new query object.
            $db    = $this->getDbo();
            $query = $db->getQuery(true);

            // Select the required fields from the table.
            $query->select(
                        $this->getState(
                                'list.select', 'DISTINCT a.*'
                        )
                );

            $query->from('`#__members_` AS a');
            
		// Join over the users for the checked out user.
		$query->select('uc.name AS uEditor');
		$query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');

		// Join over the created by field 'created_by'
		$query->join('LEFT', '#__users AS created_by ON created_by.id = a.created_by');

		// Join over the created by field 'modified_by'
		$query->join('LEFT', '#__users AS modified_by ON modified_by.id = a.modified_by');
		
		// Join table created by JOsh'
		$query->join('LEFT', 'members_table AS mem_tb ON mem_tb.id = a.id');

Glenn Arkell

unread,
Feb 22, 2019, 3:11:54 PM2/22/19
to Joomla! General Development
Based on the code displayed:

What is the actual name of the table you're joining?  The usual naming of joomla tables have a prefix such as `123ab` and thus the database table of "123ab_users" is referenced in your component code as "#__users" as is displayed in your example code.  So does your component table follow this naming standard, in which case it should be "#__members_table".

Secondly, you aren't actually "selecting" any data elements from your table to be displayed.  There should be a $query->select('mem_tb.members_name'); type statement somewhere.
Cheers
Glenn
Study Reveals What Just A Single Diet Soda Does To Your...
nucific.com
http://thirdpartyoffers.juno.com/TGL3132/5c69ff68b821c7f6813dcst03vuc
SponsoredBy Content.Ad

 

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.



____________________________________________________________

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.

To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.



____________________________________________________________

Josh

unread,
Mar 7, 2019, 9:49:19 PM3/7/19
to joomla-de...@googlegroups.com
What is the “uEditor” in the statement uc. Name AS uEditor do?

Sent from my iPhone

Glenn Arkell

unread,
Mar 7, 2019, 11:38:11 PM3/7/19
to Joomla! General Development
This just provides a named data element of uEditor to show the "name" of the person from the the table "uc".  In straight SQL this is the same as SELECT uc.name AS uEditor FROM #__users AS uc WHERE id = somenumber.
Study Reveals What Just A Single Diet Soda Does To Your...
nucific.com
http://thirdpartyoffers.juno.com/TGL3132/5c69ff68b821c7f6813dcst03vuc
SponsoredBy Content.Ad

 

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.



____________________________________________________________

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.

To unsubscribe from this group and all its topics, send an email to joomla-dev-general+unsub...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.



____________________________________________________________

Man Makes Shocking Discovery At Ark's Final Resting Place
pro.healthresponses.org
http://thirdpartyoffers.juno.com/TGL3132/5c6f5a53d3f55a520636st03vuc
SponsoredBy Content.Ad

--
You received this message because you are subscribed to a topic in the Google Groups "Joomla! General Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/joomla-dev-general/c3HcYDYffqs/unsubscribe.
Reply all
Reply to author
Forward
0 new messages