how does JTable's store() works?

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
dimsum
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sat Jun 17, 2006 9:47 am
Location: Jakarta - Indonesia
Contact:

how does JTable's store() works?

Post by dimsum » Wed Mar 12, 2008 4:26 pm

i can't seem to understand how the store() method works.. can anyone explain to me?

the wiki said and i quote
Inserts a new row if id is zero or updates an existing row in the database table.
ok.. but how does this method knows which data to insert into which column? and what about the updated data that i just edited?
i don't see any line of code that connects the inputboxes to a certain column..

coz i have this function that uses the store() method that NEVER updates a row and always INSERT new rows regardless of the primary key i mentioned on the Table class.

any replies would be very appreciated..

cheers..

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: how does JTable's store() works?

Post by Bodom78 » Thu Mar 13, 2008 12:16 am

Code: Select all

<input class="inputbox" type="text" name="title" size="40" maxlength="255" value="<?php echo $this->row->title; ?>" />
name="title" in the input field <-- binds to a database field named "title"

Yes saving decides wither to save a new item or update a previous item based on the content ID so you have to post that as well, usually through a hidden field like

Code: Select all

<input type="hidden" name="id" value="<?php echo $this->row->id; ?>" />

User avatar
dimsum
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sat Jun 17, 2006 9:47 am
Location: Jakarta - Indonesia
Contact:

Re: how does JTable's store() works?

Post by dimsum » Sat Mar 15, 2008 8:21 am

thx for your reply,

but i already did that.. i think i got bigger problem than figuring out store() for now.
this component that i've been trying to develop turns out to use 3 different database tables. and i don't see how store() can update to 3 different tables. so i'm figuring using SQL query to update or insert the datas.

my question is:
1. can i do that ? (using SQL to store etc.)
2. how can i get the data's from the forms ? any methods in particular ? i have text, text area, date, and multiple list inputs..

thank you..

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: how does JTable's store() works?

Post by Bodom78 » Sun Mar 16, 2008 3:45 am

Well Store is a nice and easy way to bind data to a table, but you don't have to use it, you can use your own sql inserts.

Where you have your store function I believe you can get all the posted variables by using JRequest::getVar

maybe something like:

Code: Select all

$db = & JFactory::getDBO();

$title = JRequest::getVar( 'title, '', 'post', 'string', JREQUEST_ALLOWRAW );
$text = JRequest::getVar( 'text', '', 'post', 'string', JREQUEST_ALLOWRAW );

//-- MAYBE DO SOME SORT OF NESTED INSERT FOR MULTIPLE TABLES 
//-- OR EXECUTE A SEPERATE INSERT FOR EACH ONE BY ONE
$sql = "INSERT INTO mytable (title) VALUES ('$title')";
$db->setQuery($query);

$sql = "INSERT INTO mytable2 (text) VALUES ('$text')";
$db->setQuery($query);

//-- YOU CAN CHECK FOR A SUCCESSFUL QUERY BY USING SOMETHING LIKE
//-- if (!$db->query()) { echo "Fail"; }


radiant_tech
Joomla! Apprentice
Joomla! Apprentice
Posts: 41
Joined: Sat Dec 15, 2007 3:02 pm
Location: Washington DC Metro

Re: how does JTable's store() works?

Post by radiant_tech » Tue Mar 18, 2008 2:54 pm

name="title" in the input field <-- binds to a database field named "title"
I don't think this is quite correct and may be the source of dimsum's problem. It appears that the fields to be added to the database must have BOTH name= and id=.

I was having a similar problem with hidden fields and found that adding id= solved the problem.
Denise

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: how does JTable's store() works?

Post by Bodom78 » Wed Mar 19, 2008 11:03 pm

I assumed there is always an ID being passed along, if not then you insert new data rather then update the current. The id can be placed in the hidden input field like a few posts up.

Usually when binding does not work for me, it's because I have mistyped the name of a database field in the table class.

User avatar
dimsum
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sat Jun 17, 2006 9:47 am
Location: Jakarta - Indonesia
Contact:

Re: how does JTable's store() works?

Post by dimsum » Thu Mar 20, 2008 4:50 pm

thanks radiant_tech i will try that tonite.. i hope ur right.
Bodom: i'm sure i don't have any typo on my database class coz the datas is successfully loaded to the forms.
and about the getVar function, can i get parameters from the form inputs? or i have add the values to the url ?

thank you


Post Reply