Page 1 of 1

Zend_Db_Statement_Exception

Posted: Tue Sep 18, 2007 2:10 pm
by Luke
I've got some php to insert a vendor into my vendors database. It looks like this (I'm using the Zend Framework)...

Code: Select all

// snip

        if ($clean = $this->processInput($input))
        {
            require_once 'models/Vendors.php';
            require_once 'Zend/Db/Expr.php';
            $vendor = new Bag_Vendors();
            
            $clean['created'] = new Zend_Db_Expr('NOW()');
            
            try {
                $vendor->insert($clean);
                $this->_redirect('vendors/view');
            }
            catch (Zend_Db_Statement_Exception $e) {
                if (strstr($e->getMessage(), '1062')) //@todo: find a better way to do this
                {
                    $this->view->formSetError('code', 'This code already exists');
                }
            }
        }

//snip
See the todo in there? Is there a better way to find out if I am getting mysql error code 1062 with this? I know I could do mysql_errno(), but it seems like the Exception would provide this code or at least its own code for that. Do I have to subclass it? Should I just shut up and use mysql_errno()?

Posted: Tue Sep 18, 2007 2:30 pm
by Begby
I don't think you should ever rely on exceptions for doing data validation, which looks like what you might be doing.

Secondly this is not very portable. What if you have to switch the backend to SQL Server? The error numbers are going to be different.

It might be better to do a query before the insert to see if the vendor information is valid, or have the insert method catch the exception and return false.

Posted: Tue Sep 18, 2007 3:17 pm
by Luke
Alright, thanks. I just added a custom validation rule that checks (Bag_NotDuplicateCode). I like this much better... thank you.

Code: Select all

$filter = new MC2_Input_Filter_Set();
        $filter->addFilter(new MC2_Input_Filter_StringTrim());
        $filter->addFilter(new MC2_Input_Filter_Digits(), array('zip', 'phone', 'fax'));
        
        $validate = new MC2_Input_Validate_Set();
        $code = new MC2_Input_Validate_Wrapper_Zend(new Zend_Validate_Regex('/^[a-zA-Z0-9_-]{2,6}$/'));
        $validate->addValidator($code, 'code', 'Manufacturer code may only contain numbers, letters, dashes, and underscores. It must also be between 2 and 6 characters.');
        $validate->addValidator(new MC2_Input_Validate_Email(), array('email_1', 'email_2'), 'You must enter a valid email address');
        $validate->addValidator(new MC2_Input_Validate_USState(), 'state');
        $validate->addValidator(new MC2_Input_Validate_USZip(), 'zip');
        $validate->addValidator(new MC2_Input_Validate_USPhone(), array('phone', 'fax'));
        $validate->addValidator(new Bag_NotDuplicateCode($this->db), 'code');
        
        $input->addFilterSet($filter);
        $input->addValidatorSet($validate);
        
        if ($clean = $this->processInput($input))
        {
            require_once 'models/Vendors.php';
            require_once 'Zend/Db/Expr.php';
            $vendor = new Bag_Vendors();
            
            $clean['created'] = new Zend_Db_Expr('NOW()');
            
            try {
                $vendor->insert($clean);
                $this->_redirect('vendors/view');
            }
            catch (Zend_Db_Statement_Exception $e) {
                if (strstr($e->getMessage(), '1062')) //@todo: find a better way to do this
                {
                    $this->view->formSetError('code', 'This code already exists');
                }
            }
        }