Page 1 of 1
how does JTable's store() works?
Posted: Wed Mar 12, 2008 4:26 pm
by dimsum
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..
Re: how does JTable's store() works?
Posted: Thu Mar 13, 2008 12:16 am
by Bodom78
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; ?>" />
Re: how does JTable's store() works?
Posted: Sat Mar 15, 2008 8:21 am
by dimsum
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..
Re: how does JTable's store() works?
Posted: Sun Mar 16, 2008 3:45 am
by Bodom78
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"; }
Re: how does JTable's store() works?
Posted: Tue Mar 18, 2008 2:54 pm
by radiant_tech
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.
Re: how does JTable's store() works?
Posted: Wed Mar 19, 2008 11:03 pm
by Bodom78
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.
Re: how does JTable's store() works?
Posted: Thu Mar 20, 2008 4:50 pm
by dimsum
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