Problems with JTable::getInstance('frontpage', 'Table');

This forum is for general questions about extensions for Joomla! version 1.5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
Stian
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 247
Joined: Sat May 20, 2006 7:58 am
Location: Norway

Problems with JTable::getInstance('frontpage', 'Table');

Post by Stian » Thu Dec 27, 2007 4:11 pm

Hi, this is perhaps quite stupid, butI have the following code:

Code: Select all

global $mainframe;

		JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_frontpage'.DS.'tables');
		$table =& JTable::getInstance('frontpage', 'Table');
		$table->ordering = $table->getNextOrder();
		$table->content_id = 45;
		if (!$table->store()) {
			echo $table->getError();
			die();
		}
The SQL generated by this is " UPDATE jos_content_frontpage SET `ordering`='6' WHERE content_id='45' "

What I would like to do is to add a new frontpage item, and not update an existing one. So the SQL should be an INSERT.

Please help!

Stian

SotL
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Sun Nov 25, 2007 1:57 pm

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by SotL » Fri Dec 28, 2007 12:34 pm

Hi Stian

table->store() function  depends on the value of "id". I am not 'completely' sure here, but I do believe that unless "id" is empty it produces an update query.

What I mean is that you need to take out:

$table->content_id = 45;

otherwise it will always "update".

The function runs "insert" when id is empty and an update when id has a value already.

I could be wrong, but as far as I know that is how it works.

Try a little test and see, but I am pretty sure that is the way it works.

Hope this helps

Stian
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 247
Joined: Sat May 20, 2006 7:58 am
Location: Norway

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by Stian » Fri Dec 28, 2007 12:42 pm

I think you're quite right, BUT jos_content_frontpage doesn't have a unique auto increment field (I think it's one of very few in Joomla), which means that this functinonality doesn't work on this table.

Stian

SotL
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Sun Nov 25, 2007 1:57 pm

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by SotL » Fri Dec 28, 2007 12:50 pm

I am not sure how many records you have in the database as of right now, but if not to many you could ADD it. Just make it "auto_id" rather then content_id and then go to your records starting at the top and edit them from 1 twhatever you have. After that you can go back to the "auto_id" and make it a primary key and add auto increment to it and from there it will take care of itself :)

I do not know of a way to make store() work without an auto_increment field. So maybe someone with more knowledge in this area will come along and help out.

Stian
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 247
Joined: Sat May 20, 2006 7:58 am
Location: Norway

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by Stian » Fri Dec 28, 2007 12:53 pm

I don't think it's wise to mess with the Joomla! core tables. I'll just use a regular SQL INSERT INTO for now. But I think it should be stated somewhere that that you can't use the JTable class to update this particualer table...

Stian

SotL
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Sun Nov 25, 2007 1:57 pm

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by SotL » Fri Dec 28, 2007 5:24 pm

I agree that it is not wise, and should be imported into the final release, however adding another "field" to the table will not in any way mess up any other code.

It simply provides a way to use Joomlas core functions rather then self scripting to the database (bypassing security). The only "issue" that would arrise is when importing or upgrading to a newer version...you would once again need to add the extra field which is not good because a person could easily forget, but there is no side effect for any other code in the core as it simply does not know that tat extra field is available to be used...and thus isn't used.

So it is a matter of choice. I do hope the devs consider adding an auto_id field to it upon release of the final version of 1.5

Stian
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 247
Joined: Sat May 20, 2006 7:58 am
Location: Norway

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by Stian » Sat Dec 29, 2007 10:54 am

I have posted this issue to http://forum.joomla.org/index.php/topic,247531.0.html

Thanks for your input!

Stian

User avatar
masterchief
Joomla! Hero
Joomla! Hero
Posts: 2316
Joined: Fri Aug 12, 2005 2:45 am
Location: Brisbane, Australia
Contact:

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by masterchief » Tue Jan 01, 2008 8:03 am

The content_id is technically a foreign key - it's not appropriate for it to have an auto-increment value because it must marry with a known content item.
Andrew Eddie - Tweet @AndrewEddie
<><
http://eddify.me
http://www.kiva.org/team/joomla - Got Joomla for free? Pay it forward and help fight poverty.

SotL
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Sun Nov 25, 2007 1:57 pm

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by SotL » Tue Jan 01, 2008 7:57 pm

That makes since, so let me ask you this then. Is the only way to do an insert in this table is by custom coding rather then using Joomla!'s built in functions? Or are we missing something here?

The other thing is, would it be technicaly wrong to build this table (in the core install) with another field called auto_id that could be auto incremented so the table->store() function would insert or update?
Last edited by SotL on Tue Jan 01, 2008 7:59 pm, edited 1 time in total.

User avatar
masterchief
Joomla! Hero
Joomla! Hero
Posts: 2316
Joined: Fri Aug 12, 2005 2:45 am
Location: Brisbane, Australia
Contact:

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by masterchief » Tue Jan 01, 2008 10:22 pm

We should actually make the table class a bit smarter to handle this, but yes, it's a manual job.  But since content_id is a primary key, you could use a statement like REPLACE INTO to that would cover both insert and update operations with only one statement.
Andrew Eddie - Tweet @AndrewEddie
<><
http://eddify.me
http://www.kiva.org/team/joomla - Got Joomla for free? Pay it forward and help fight poverty.

Stian
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 247
Joined: Sat May 20, 2006 7:58 am
Location: Norway

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by Stian » Thu Jan 03, 2008 6:49 pm

I never ment to change the existing field to auto increment, but add a new one. That would take care of business, wouldn't it?

S

User avatar
masterchief
Joomla! Hero
Joomla! Hero
Posts: 2316
Joined: Fri Aug 12, 2005 2:45 am
Location: Brisbane, Australia
Contact:

Re: Problems with JTable::getInstance('frontpage', 'Table');

Post by masterchief » Thu Jan 03, 2008 9:58 pm

No, the table doesn't need an extra field.
Andrew Eddie - Tweet @AndrewEddie
<><
http://eddify.me
http://www.kiva.org/team/joomla - Got Joomla for free? Pay it forward and help fight poverty.


Locked

Return to “Extensions for Joomla! 1.5”