Zend DB MYSQL Wrapper - php

All,
I have a PHP application written in Zend Framework with MVC style. I plan to use Zend_DB to connect to the MySQL database and process queries. I am looking for a wrapper class which makes it easy to use Zend_DB class. This wrapper class will have a constructor that connects to the Mysql db using Zend_DB. It will also have a method to return a singleton instance for each and every db connection made.
Something like:
$pptDB = PPTDB::getInstance();
$pptDB->setFetchMode(PPTDB::FETCH_OBJ);
$result = $pptDB->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
echo $result->bug_description;
Where class PPTDB extends Zend_DB
Is this something feasible to have? If not, how ls would you use Zend_DB in a major application?
Thanks,

AFAIK it is possible to use Zend_DB as a standalone (well, all classes in that package should also be available of course), but if you're not using the models there is little to gain from is. If you're only interested in the database abstraction I'd recommend PDO, if you want a Zend_DB wrapper Reflection can give you a nice list of properties / functions you can choose to override or not.
http://nl2.php.net/reflection

You should create adapter using Zend_Db::factory() and than use it in Zend_Db_Table_Abstract::setDefaultAdapter(). Then you can get your adapter wherever you want using Zend_Db_Table::getDefaultAdapter() or $table->getAdapter() :)

Related

Is it possible to use PDO instead of Doctrine in Symfony 4?

I'm doing a simple MVC web app that is supposed to do different queries to a SQL database. I'm using Symfony 4 as a framework, mostly to take advantage of its routing features.
I know I could take advantage from Doctrine but I was wondering: if someone wants to use PDO instead of Doctrine is it possible to do so and how should it be implemented. Should I make a Database class with PDO that I could pass on to another class?
Yes, you are not required to use doctrine at all. You can just define your own service with required functionality.
But maybe you could use DBAL which is lower layer used by doctrine to handle creating queries (but doesn't include any ORM functionality).

How to access a connection handle from one class in another class in php

I´m building a MVC application. My PDO connection is in /Controller/includes/Conexao and queries are in /model/DAO
As I need a prepared statement I need to use the connection handle like that:
$stmt = $db->prepare($query);
Problem is that $db is a variable in my class Conexão and I am not able to use it in DAO class. So how could I proceed?
Using old fashioned php code I could only call mysql_query but it is about to be discontinued so that´s not an option.
I would avoid using globals unless there is a VERY good reason for doing so. Look into dependency injection and singletons. There is a very nice light-weight container called Pimple that can be used for this purpose:
http://pimple.sensiolabs.org/
Another uglier solution would be using mysqli_query (only the old mysql is deprecated).

Terminology and explanation of Doctrine PDO

I have noticed an interesting technique Doctrine is using for for it's Connection and Statement interfaces.
They have a class called PDOConnection, which naturally extends PDO, but also implements Connection, an interface created by Doctrine.
The Connection interface defines copies of several PDO methods, and although Doctrines PDOConnection class doesn't implement all of them, the fact it extends PDO means it does.
My best guess why they did that is they wanted to design their connection drivers around PDO's methodology, but what I really what to know is what this pattern is called and if it regularly gets used.
My best guess why they did that is they wanted to design their connection drivers around PDO's methodology
Yep, that's it. There is no special pattern for this.

OOP CMS object design for accessing the database

I'm trying to make a small cms in php and mysql, and want it to be object-oriented
I have a class DbConnector that handles all sql queries. Then I have some other classes that needs to alter the database through the DbConnector.
As I see i I have to either send the DbConnector instance as a parameter in every class initialization, or I can make a new DbConnector instance for each class, which to me seems pretty unnecessary.
What is the preferred way to do this or is it all wrong?
The preferred way of doing this would be Dependency Injection, the lazy / easy / OK for small scale way could be a Singleton database object.

Looking for a simple mySQLi class example with descriptions

I am looking for an example of a simple, but functional example of a class extending the mySQLi class in PHP5. I am interested in stored procedures also. Basically what I need is an example of a practical and usable class. I learn best by example and unfortunately I have been unable to find any site or book that seems to have a really solid yet simple example. All the examples I see are way to complicated so I am unable to understand or they are so simple that I might as well just instantiate the class right then and there inline. It would be great if somebody could supply me with an example like this and explain whats going on however well they can. My long term goal is to have a class that I can instantiate that deals with authentication of the database and then takes my SQL statements and supplies the output in variables.
If anyone is willing to take the time to help me out it would be much appreciated!
,Thanks
Did you check PHP PEAR MDB2 ?
http://pear.php.net/package/MDB2
Its not so simple, its the best DAL in php IMHO...
About mysqli, you can take a look at those articles :
ext/mysqli: Part I - Overview and Prepared Statements
Using ext/mysqli: Part II - Extending mysqli
(Quite old, but mysqli extension has not changed that much for a couple of years, so those should still be OK)
You could also have a look at PDO, included in PHP since version 5.1 ; it's an object-oriented API to access multiple DB engines (you don't use mysqli_query, pg_query, oci8_query or stuff like that ; only $pdo->query -- well, your SQL has to be compatible, but that's another problem ;-( )
Another approach would be using an ORM, like, for instance, Doctrine.
It's not a class that extends MySQLi, but a full Framework to abstract the DB engine - with that, you write a schema to describe you DB's structure, and you then use a "pseudo-SQL" language to send queries ; or just (for most cases) the object-oriented API that abstracts SQL.
It's a bit difficult to use, at first, but really powerful and useful (and not bad for productivity, I think, for many kind of projects)
give a try to DALMP: http://code.google.com/p/dalmp/ is support prepared statements and many cache backends
Little bit off topic but why MySQLi? PDO is much better and considered a defacto PHP standard these days. I wrote a simple PDO class a year or two ago, I can give you link if interested.
Here is a simple example of adding built in query timing to the mysqli class.
This assumes you have defined a "MyTimer" class implementing the iTimer interface. iTimer class, in it's simplest form, would record the start and stop times for the specified $key (using microtime() or getrusage() or whatever).
<?php
interface iTimer
{
public function start($key);
public function stop($key);
}
class mysqlp extends mysqli
{
function query($query, $resultmode)
{
$timer = MyTimer::getInstance();
$timer->start('mysqli::query');
$result = parent::query($query, $resultmode);
$timer->stop('mysqli::query');
return $result;
}
}
#usage
$mysqli = new mysqlp("localhost", "my_user", "my_password", "world");
$results = $mysqli->query("SELECT id FROM foo");

Categories