proper mysql object format

36 views
Skip to first unread message

Troy Hall

unread,
May 20, 2016, 4:46:00 PM5/20/16
to joomla-dev-general

I'm writing a block of several entries to the database ( could be 1000 at a time )

And I see two different ways.. my current method doesn't display the error from mysql on duplicate entry inside a system message.  instead it throws a mysql error on white page like you would see in phpmyadmin.

I notice on the one with the catch ( the left ) he's creating the dbo but on mine i'm not.. which is right?  Why is mine working?  I'm only interested in if $line is a duplicate.. with 90k+ records its not practical do to a query to check.

Bear

Michael Babker

unread,
May 20, 2016, 4:48:44 PM5/20/16
to joomla-de...@googlegroups.com
$db->insertObject() is designed for inserting a new record

$db->updateObject is designed for updating an existing record

The two should not be cross-purposed

--
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.

Troy Hall

unread,
May 20, 2016, 7:29:39 PM5/20/16
to joomla-de...@googlegroups.com

that makes sense... but is the way he's showing the try/catch correct?  Everything works except when throwing a error the error response from mysql is not put into the system message box.

Bear

Michael Babker

unread,
May 20, 2016, 8:25:13 PM5/20/16
to joomla-de...@googlegroups.com
Thrown Exceptions do not automatically get added to error logs or the system message queue, it's up to the implementation that's catching the Exception to decide what to do with it (whether that be check the Exception for certain conditions and re-throw it if those aren't met, to try a different code path (which is what your example is doing), or just completely ignore it because the error condition really doesn't make a difference and move on with life).  If you want the Exception's message to go into the message queue you need to add it yourself, though in that specific scenario it's not something it looks like is intended to render a message because it's trying something else to avoid an error.

Troy Hall

unread,
May 20, 2016, 10:31:57 PM5/20/16
to joomla-de...@googlegroups.com
what I need the message for, is I set the `Entry` column to be unique.  So, if they happen to attempt a double post, i need to show them an error stating so, and which record entry(s) it was.
Right now when that happens you get white page with mysql error.
Bear

Michael Babker

unread,
May 21, 2016, 2:40:32 AM5/21/16
to joomla-de...@googlegroups.com
Then you need to catch an Exception like your demo does and load it into the message queue or let the Exception go uncaught and bubble up to the global error handler.  If you're getting a white screen, you're getting a different type of error and need to turn on error reporting to get its information.

sovainfo

unread,
May 21, 2016, 6:58:11 AM5/21/16
to Joomla! General Development
Yours is right. It makes no sense to get the DBO and create the $query and not using it. The insertObject () is passing the information. $query is not used to create the statement and then passed to $db.

Your solution doesn't have error handling, so it should be covered by a try/catch. The try/catch on the left is incorrect: it treats any error as an insert duplicate error, which is wrong. On top of that it will create the same blank page when it is not an duplicate error. You can't update something when there is a problem with the connection!

So, the catch should contain code that logs the errors. Don't consider it appropriate to put it in the message queue. You don't know how many to expect.

Troy Hall

unread,
May 21, 2016, 9:54:36 PM5/21/16
to joomla-de...@googlegroups.com
I'm doing something dreadfully wrong... I tried adding the try/catch and instead of throwing an error, it takes the valid code and displays it using the invalid code routine I wrote earlier, saying the good lines are bad.
its a totally wrong error...   It SHOULD show the mysql error ( or more ideally JError::raiseError("Your entry " . $line . " on row " . $i . "is a duplicate");
The code is here https://github.com/N6REJ/Joomla_Astronomer/blob/master/article-form-reponse.php
The white space error I was referring to earlier is..

Thanks again...
Bear
try{
// Insert the object into the user profile table.
$result = JFactory::getDbo()->insertObject('#__aso_astrometry', $saveData);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
$i++;
/* continue to next line */

Troy Hall

unread,
May 21, 2016, 11:13:25 PM5/21/16
to joomla-de...@googlegroups.com
I got to thinking what I really need is instead of INSERT, i need INSERT IGNORE for the "Entry" field
Bear


On 5/21/2016 05:58, sovainfo wrote:

Viper

unread,
May 22, 2016, 2:38:02 PM5/22/16
to Joomla! General Development
Yeah! Really? :) Why not :)
You got an error because an 'Entry' field is not an unique field in you data. You have a logical error in your app and you try to make them work by using crutches.

PS! Call JFactory::getDbo() in cycle is bad idea.


On Sunday, May 22, 2016 at 6:13:25 AM UTC+3, Bear wrote:
I got to thinking what I really need is instead of INSERT, i need INSERT IGNORE for the "Entry" field
Bear

On 5/21/2016 05:58, sovainfo wrote:
Yours is right. It makes no sense to get the DBO and create the $query and not using it. The insertObject () is passing the information. $query is not used to create the statement and then passed to $db.

Your solution doesn't have error handling, so it should be covered by a try/catch. The try/catch on the left is incorrect: it treats any error as an insert duplicate error, which is wrong. On top of that it will create the same blank page when it is not an duplicate error. You can't update something when there is a problem with the connection!

So, the catch should contain code that logs the errors. Don't consider it appropriate to put it in the message queue. You don't know how many to expect.
--
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-general+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages