2009年4月20日星期一

Re: [fw-db] Using an already existing database connection resource with Zend_DB

Hi Ralph, hi folks,

I tried to implement your solution, unfortunately TYPO3 is kinda quite
old - at least
what database connectivity belongs. So, the connection is no PDO, it's
just a plain
MySQL Connection Resource established via mysql_connect().

I tried to extend the Zend_Db_Adapter_Pdo_Mysql class for my
requirements, but it seems
I would need to write a whole new controller from scratch.

Are you aware of any other solution which may work?

Thanks very much,
Thasmo


PS: Sorry Ralph for double post, trying to figure out how to reply to
mailing lists. xD

This is my try:

class Hype_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql {

protected function _connect() {
if($this->_connection) {
return;
}
$connection = $GLOBALS['TYPO3_DB']->link;
if($connection) {
$this->_connection = $connection;
return;
}
parent::_connect();
}
protected function _quote($value) {
if(is_int($value) || is_float($value)) {
return $value;
}
$this->_connect();
return mysql_escape_string($value);
}
}

2009/4/19 Ralph Schindler <ralph.schindler@zend.com>:
> Hey Thasmo,
>
> Yeah, this can be done, but you will have to extend the target adapter.  For
> the purposes of this example, I'll assume you are dealing with a Pdo mysql
> connection.
>
> In that case, I would extend the Zend_Db_Adapter_Pdo_Mysql adapter similarly
> to this (two options below):
>
> class My_DbAdapter_TYPO extends Zend_Db_Adapter_Pdo_Mysql
> {
>    // if you want to set the connection into the adapter:
>    protected $_typoDbConnection = null;
>
>    // and this
>    public function setTypoConnection(PDO $connection)
>    {
>        $this->_typoDbConnection = $connection;
>    }
>
>
>    // this is where the work is done though
>    protected function _connect()
>    {
>        if ($this->_connection) {
>            return;
>        }
>
>        if ($this->_typoDbConnection instanceof PDO) {
>            $this->_connection = $this->_typoDbConnection;
>            return;
>        }
>
>        // OR if you wanted to not set it into the adapter
>        // and retrieve it from a global typo place:
>        $typoconnection = TYPO::getGlobalDbConnection();
>        if ($typoconnection instanceof PDO) {
>            $this->_connection = $typoconnection;
>            return;
>        }
>
>        // all else fails:
>        parent::_connect()
>    }
>
> }
>
> you can then use the factory with this adapter which will create the above
> adapter that is not only able to use the typo connection, but also able to
> create one if need be:
>
> $adapter = Zend_Db::factory('TYPO', array(
>    'username' => 'foo',
>    ... // standard connection info here in case it needs to be created
>    'adapterNamespace' => 'My_DbAdapter'
>    ));
>
> That should give you enough to work with.. hope it helps,
>
> ralph
>
>
> Thasmo wrote:
>>
>> Hello folks,
>>
>> trying to use Zend_DB with TYPO3, I'm wondering if there's a way to use
>> the
>> already established MySQL connection from TYPO3, without needing to
>> establish a second connection with the Zend_DB factory or a database
>> adapter?
>>
>> Is there a simple solution to this, or do I need to write a wrapper class
>> or
>> something similar?
>>
>> Thanks for your help!
>>
>> Regards,
>> Thasmo
>

没有评论: