Forums

Three tables, many to many relationship

jbudd 16 Dec, 2009
Hi,

I am using Chronoforms to create records in three tables eg:

mydatatable (id, published, text) I want published to = 1
mytagstable (id, published, tag) I want published to = 0
mydatatags (dataid, tagid)

I have mydatatable and mytagstable in DBConnection so the autogenerated code correctly creates a record in each of these tables

I changed the processing order to Plugins, Autogenerated, OnSubmit so that OnSubmit Code can retrieve the IDs of the two new records and write them to mydatatags:
$db =& JFactory::getDBO();
$itemid = $MyForm->tablerow["mydatatable"]->id;
$tagid = $MyForm->tablerow["mytagstable"]->id;
$query = "INSERT INTO `mydatatags` (tagid, itemid) VALUES ($tagid, $itemid)";
$db->setQuery($query);
$db->query();


The question is: How can I store different values in the two 'published' fields given that the RunOrder means I cant use the OnSubmit code?
nml375 16 Dec, 2009
Hi,
Since the $MyForm->tablerow[..] references to a JTable object representing the newly added row, something like this should do the trick:
$MyForm->tablerow["mydatatable"]->published = 1;
$MyForm->tablerow["mydatatable"]->save();
$MyForm->tablerow["mytagstable"]->published = 0;
$MyForm->tablerow["mytagstable"]->save();


/Fredrik
jbudd 16 Dec, 2009
That gives me an error
PHP Fatal error: Call to undefined method stdClass::save()

jb
nml375 16 Dec, 2009
Hi jb,
Hmm... try using store() instead of save()

/Fredrik
jbudd 16 Dec, 2009
Yes that works! Thanks Fredrik!

I tried a seperate update query too:
$query = "UPDATE `mytagstable` SET `published` = 1 WHERE `id` = $tagid";
$db->setQuery($query);
$db->query();

I guess that your suggestion must also involve a new SQL request/transaction/query (whichever is the relevant term) since the new record creation has already committed?

jb
This topic is locked and no more replies can be posted.