Внимание |
Zend Optimizer: due to a bug in the optimizer, you will have to either reduce the optimization level, or define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 otherwise PHP may segfault Pass by Reference, due to a unfixable bug in PHP4, you can not use overload with pass-by-reference arguments (It works OK in PHP5), If you need pass-by-reference, define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 |
DB_DataObject is a SQL Builder and Data Modeling Layer built on top of PEAR::DB. Its main purpose is to
Build SQL and execute statements based on the objects variables.
Group source code around the data that they relate to.
Provide a simple consistent API to access and manipulate that data.
So what does that mean in english? Well, if you look around at some of the better written PHP applications and frameworks out there, you will notice a common approach to using classes to wrap access to database tables or groups. The prime example of this is the person object, which would frequently look something like this.
Пример 34-1. A Classic Data Object or Container
|
The key benifit of this approach is that you are grouping similar actions on a single table in one place, and are more likely to spot duplicated code (eg. two methods that do similar things). You will also notice the global $db variable used here - the fact is that most of the time you will use a common database connection for all your classes, so how should this be dealt with?
The next step on this road is to use the objects variables as a storage mechanism.
Пример 34-2. A Classic Data Object or Container
|
As you can see, the current row of data is now stored in the Data Container, and additional methods can be added to manipulate the data in the object or even call other related objects (eg. tables relationships in databases)
As a next step, why not utilize the member variables to perform searches or gets on the database.
Пример 34-3. A Classic Data Object or Container
|
As you can see, by assigning values to the object before the find method is called, you can set conditions for the query. DB_DataObjects behaves in a similar way to this, however, you can also add more conditions with the whereAdd() method, or restrict the selection using the selectAdd() method.
Obviously you can carry on down this road and create lots of little containers for each table in your database, and all the code will nicely relate to each table. However, you can see from the examples above that all classes are likely to end up with a common set of methods.
to fetch data, like get, find, fetch
to update, insert and delete from the data store
to automate fetching related objects
A common simple configuration method (for setting database connections)
A fast and simple store for database descriptions, so primary keys can be used to locate data quickly
a debugger that enables you to see what exactly it is doing.
basic data validation - so the strings and integers can be checked.
Posibility to build complex joins or get related data by secondary calls (links) .
Ability to create and update your Table specific classes with the current database table variables (see autobuilding)
Simple to integrate with other packages, with setFrom() and toArray() methods
So what do my classes look like?
Пример 34-4. At last some real DataObject Code..
|
The above example illustrates the components of the DB_DataObject, by setting the options, all the core objects will be able to auto load the data definitions from a generated ini file, and know how to access the database. (multiple databases are supported - see section on configuration)
The class definition illustrates how you only need to define the data specific code in your class, ignoring all the common methods, along with showing one of the methods for retrieveing multiple rows of data.
The later half illustrates how to query and get the result for a single row. The $person->get() would connect to the database, perform the query, fetch the result and assign the objects variables to the data returned from the query.
Пример 34-5. In the above example, this query would be performed.
|
Пример 34-6. To make a change to the Database you would just change the value of the objects variables and call the update method.
|
As a general rule of thumb method names are usually the same as the SQL statement they relate to.
Пред. | Начало | След. |
DB_DataObject | Уровень выше | Configuration Options |
HIVE: All information for read only. Please respect copyright! |