2010年8月30日星期一

[fw-db] Zend_Db_Table sequences and MySql problem

Hi,

I have a problem with Zend_Db_Table / Zend_Db_Table_Abstract and would like to ask for some advice.

In my current project I am trying to support both MySql and Oracle Databases.

I am using subtypes of Zend_Db_Table defining objects representing my database tables on the php side, a simple example:

 

[code]

class  Model_MailWorklist extends Zend_Db_Table

{

 

    protected $_schema   = 'ONLINEMARKETING';

    protected $_name     = 'BACKGROUND_MAIL_WORKLIST';

    protected $_primary  = 'ID';

    protected $_sequence = 'ONLINEMARKETING.COUNTER_BG_WORK';

 

}

[/code]

 

As one may have noticed I am using sequences with my Oracle database, so something like this just works fine:

 

[code]

 

 

$_mailWorkList = new Model_MailWorklist();

 

$_nextPrimaryKey = $_mailWorkList

     ->insert

       (

           array

           (

               'SUBSCRIBER_ID' => $_singleTransferRecipientId,

               'NEWSLETTER_ID' => $_newsletterDispatchNewsletterId,

               'DISPATCH_DATE' => $newsletterDispatchDateTime,

               'DISPATCHED'    => 0,

               'DISPATCH_TYPE' => 'main'

           )

       );

 

[/code]

 

Now I am tying to use the same code with my MySql Database, which rather uses [i] auto_increment [/i], instead of sequences.

Sine I need to define [i] protected $_sequence [/i] for handling my Oracle database, I hoped that for the MySql case Zend_Db_Table will just ignore the specified sequence and the [i]insert()[/i] function returns the next inserted primary key. But as long as [i] protected $_sequence [/i] is set to some string value in my Model_MailWorklist-Class, Zend will try to use it and the [i]insert()[/i] function just returns [i]FALSE[/i]. The responsible Framework code:

 

[code]

 

abstract class Zend_Db_Table_Abstract

{

  protected $_sequence = true;

 

  public function insert(array $data)

    {

    …

 

        if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {

            $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);

        }

  …

[/code]

 

:\ … Now I dislike the solution of adding a check for the database type currently active to every init method of subtypes of Zend_Db_Table and set [i]$this->_sequence [/i] if running on Oracle.

My current “brute force solution” is extending Zend_Db_Table, lests say ZendX_Db_Table:

 

[code]

 

class ZendX_Db_Table extends Zend_Db_Table

{

 

    public function __construct($config = array(), $definition = null)

    {

        parent::__construct($config, $definition);

 

        $this->set_transparent_sequence();

    }

 

    private function set_transparent_sequence()

    {

        if( $this->_db instanceof Zend_Db_Adapter_Pdo_Mysql )

        {

            $this->_sequence = true;

        }

    }

 

}

 

[/code]

 

The only problem, now I need to use ZendX_Db_Table instead of Zend_Db_Table in my whole application. My question is, is there maybe a more efficient way instead of introducing a whole new class level to my application, just to make Zend ignore sequences when working with MySql?

Is this even a Zend-Bug or does this behaviour make sense and there is a reason supporting sequences in MySql?

 

Regards,

Richard Tantius
Software Engineer



Gotenstr. 7-9
53175 Bonn
Tel.:        +49 (0)228 / 4 22 86 - 32
Fax.:       +49 (0)228 / 4 22 86 - 66

E-Mail:   r.tantius@binserv.de
Web:      www.binserv.de
               www.binforcepro.de

Geschäftsführer: Rüdiger Jakob
Registergericht: Siegburg HRB 6765
Hauptsitz der Gesellschaft.: Pfarrer-Wichert-Str. 35, 53639 Königswinter
Diese E-Mail einschließlich eventuell angehängter Dateien enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind und diese E-Mail irrtümlich erhalten haben, dürfen Sie weder den Inhalt dieser E-Mail nutzen noch dürfen Sie die eventuell angehängten Dateien öffnen und auch nichts kopieren oder weitergeben/verbreiten. Bitte verständigen Sie den Absender und löschen Sie diese E-Mail und eventuell angehängte Dateien umgehend. Vielen Dank!

 

 

 

没有评论: