2009年4月19日星期日

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

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

没有评论: