Skip to content

php class for making database objects using a pdo handle. acts like ruby on rails active record. i use it to create models in zend framework

Notifications You must be signed in to change notification settings

myneid/DBObject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

* class DBObject will act very similar to pear db dataobjects but using PDO and php5 only
 * it will do dynamic object creation based on a passed table name.
 * it will do table linking if you have a field named car_id it will link to the table car on teh id field
 * examples are at the bottom of this file
 * i had this idea to do this and found online somebody that had already done something similar using pear db which i modified completely and added a bunch of new stuff into it to make it usable. you can find the original at
 * http://www-128.ibm.com/developerworks/opensource/library/os-php-flexobj/
* but this has grown way outside of that

$book = new DBObject( 'library','book', array( 'id', 'author',
'title', 'publisher' ) );
$book->delete_all();
$book->setTitle( "PHP Hacks" );
$book->setAuthor( "Jack Herrington" );
$book->setPublisher( "O'Reilly" );
$id = $book->insert();

echo ( "New book id = $id\n" );

$book->setTitle( "Podcasting Hacks" );
$book->update();

$book2 = new DBObject( 'library','book', array( 'author',
'title', 'publisher' ) );
$book2->get( $id );
echo( "Title = ".$book2->getTitle()."\n" );
$book2->delete( );

$book3 = DBObject::factory('library','book', $dbh);
$book->delete_all();
$book->setTitle( "PHP Hacks" );
$book->setAuthor( "Jack Herrington" );
$book->setPublisher( "O'Reilly" );
$id = $book->insert();

echo ( "New book id = $id\n" );

$book->setTitle( "Podcasting Hacks" );
$book->update();


$book = DBObject::factory('library','book', $dbh);
$book->setTitle('PHP hacks');
$book->find(1);
echo( "Title = ".$book->getTitle()."\n" );

$book = DBObject('library','book');
$book->setAuthor('Mark Twain');
$book->find();
while($book->fetch())
{
    echo "title: " . $book->getTitle();
}

//linked objects with field name of tablename_id get put under linked_object
$book = new DBObject( 'library','book', array( 'id', 'author_id',
'title', 'publisher' ) );
//author_id links to the author table
$book->get(12);
echo "the authors name is " . $book->linked_objects['author']->getName();



to create a model that extends DBObject

<?php
class Category extends DBObject
{
	public function __construct()
	{
		$this->__constructFromDb('library', 'category', $dbh);
	}
}

then
$category = new Category()
$category->find()
while($category->fetch())
{
}

other things you can do is
$myobj->limit(10);
$myobj->where('book is not null')
$myobj->setThistime('NOW()', true);

About

php class for making database objects using a pdo handle. acts like ruby on rails active record. i use it to create models in zend framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages