Re: Catching MySQL errors

2008-03-31 Thread Sergio Pola

I catch the error with a Caveman's method in cake 1.1

if( $this-Model-save($data)){
if($id == -1) $id = $this-Model-getLastInsertID();
}
else {
$error_msg = mysql_error();
if(strpos($error_msg, 'Duplicate entry') != -1 ){
// do something
}
}

I think there is a better way using the errno number and, but this is
fast solution.
-Sergio


On Mar 28, 3:57 pm, MattC [EMAIL PROTECTED] wrote:
 I've tried to catch the duplicate entry error a bunch of times, but
 have never had any luck.  I would love to see a solution for this.  I
 realize I can check before trying the save, but if I'm importing some
 large amount of data it would cause an extra query for every record.

 As for using IsUnique I don't think it works real well just sticking
 it in as a rule in validation.  Otherwise won't it fail on updates?
 And if you set on = create won't that allow for duplicate names
 on update?  I always end up using a custom rule to make sure that 1)
 the username doesn't exist and 2) if it does it has the same id as the
 data being saved.  Maybe I'm missing some Cake magic on this one.

 -Mattwww.pseudocoder.com

 On Mar 28, 11:58 am, Greg Baker [EMAIL PROTECTED] wrote:

  How can I capture a MySQL error like the following:

  Warning (512): SQL Error: 1062: Duplicate entry
  'TestSlot-47e9547d-6570-4e3e-9834-16b88699522c' for key 2 [CORE/cake/
  libs/model/datasources/dbo_source.php, line 459]

  Basically I have a unique key set across two fields in a table so that
  if I add a 'Slot' which belongsTo a 'Conference' the slot name must be
  unique within that conference.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread Dardo Sordi Bogado

Wouldn't be better to validate the uniqueness before saving the data?

On Fri, Mar 28, 2008 at 12:58 PM, Greg Baker [EMAIL PROTECTED] wrote:

  How can I capture a MySQL error like the following:

  Warning (512): SQL Error: 1062: Duplicate entry
  'TestSlot-47e9547d-6570-4e3e-9834-16b88699522c' for key 2 [CORE/cake/
  libs/model/datasources/dbo_source.php, line 459]

  Basically I have a unique key set across two fields in a table so that
  if I add a 'Slot' which belongsTo a 'Conference' the slot name must be
  unique within that conference.
  


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread Chris Hartjes

On Fri, Mar 28, 2008 at 12:09 PM, Dardo Sordi Bogado
[EMAIL PROTECTED] wrote:

  Wouldn't be better to validate the uniqueness before saving the data?


Dardo beat me to it...I was going to suggest the same thing:  a custom
validation method to make sure you have a unique slot name

-- 
Chris Hartjes
Internet Loudmouth
Motto for 2008: Moving from herding elephants to handling snakes...
@TheKeyBoard: http://www.littlehart.net/atthekeyboard

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread Greg Baker

yah, okay..  I just figured that since I _could_ do it in the database
schema, I _should_ do it there.  I'll make a custom method.

thanks

On Mar 28, 2:12 pm, Chris Hartjes [EMAIL PROTECTED] wrote:
 On Fri, Mar 28, 2008 at 12:09 PM, Dardo Sordi Bogado

 [EMAIL PROTECTED] wrote:

   Wouldn't be better to validate the uniqueness before saving the data?

 Dardo beat me to it...I was going to suggest the same thing:  a custom
 validation method to make sure you have a unique slot name

 --
 Chris Hartjes
 Internet Loudmouth
 Motto for 2008: Moving from herding elephants to handling snakes...
 @TheKeyBoard:http://www.littlehart.net/atthekeyboard
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread jonknee

 Dardo beat me to it...I was going to suggest the same thing:  a custom
 validation method to make sure you have a unique slot name

No need to make it custom, it's built into 1.2 Model::isUnique() and
can be accessed as a validation rule:

'rule' = 'isUnique'

It works great, I'm using it for usernames/email addresses.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread Greg Baker

Thanks Jon, the unique field is actually a pairwise uniqueness.  So I
believe I do need a custom method.  I just did a $this-findCount() in
my model with the fields that need to be unique.  Works well.

On Mar 28, 2:36 pm, jonknee [EMAIL PROTECTED] wrote:
  Dardo beat me to it...I was going to suggest the same thing:  a custom
  validation method to make sure you have a unique slot name

 No need to make it custom, it's built into 1.2 Model::isUnique() and
 can be accessed as a validation rule:

 'rule' = 'isUnique'

 It works great, I'm using it for usernames/email addresses.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Catching MySQL errors

2008-03-28 Thread MattC

I've tried to catch the duplicate entry error a bunch of times, but
have never had any luck.  I would love to see a solution for this.  I
realize I can check before trying the save, but if I'm importing some
large amount of data it would cause an extra query for every record.

As for using IsUnique I don't think it works real well just sticking
it in as a rule in validation.  Otherwise won't it fail on updates?
And if you set on = create won't that allow for duplicate names
on update?  I always end up using a custom rule to make sure that 1)
the username doesn't exist and 2) if it does it has the same id as the
data being saved.  Maybe I'm missing some Cake magic on this one.


-Matt
www.pseudocoder.com


On Mar 28, 11:58 am, Greg Baker [EMAIL PROTECTED] wrote:
 How can I capture a MySQL error like the following:

 Warning (512): SQL Error: 1062: Duplicate entry
 'TestSlot-47e9547d-6570-4e3e-9834-16b88699522c' for key 2 [CORE/cake/
 libs/model/datasources/dbo_source.php, line 459]

 Basically I have a unique key set across two fields in a table so that
 if I add a 'Slot' which belongsTo a 'Conference' the slot name must be
 unique within that conference.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---