<?php
/*
Copyright (c) 2010 Samuel Stevens
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
define('INDEX',True);
function __autoload($class)
{
if (file_exists($class.".php"))
include($class.".php");
}
class Library extends AutoDB
{
private $descriptions;
function Library()
{
parent::AutoDB('Library');
$this->descriptions = new AutoDB('Descriptions');
}
function GetNewestBooks()
{
return $this->Select('Library.*, Descriptions.Desc as Desc')->JoinOn('Descriptions','ID','BookID')->OrderBy('Date Desc')->Limit(10)->Get();
}
function AddBook($name, $description="")
{
static $time = 0;
$time += 120;
$book = $this->Create();
$book->SetNoQuote('Date','UNIX_TIMESTAMP()+'.$time, False); //False to not reload record on save, since we dont need the result, Default=True
$book->Name = $name;
$id = $book->Save();
$desc = $this->descriptions->Create();
$desc->BookID = $id;
$desc->Desc = $description;
$desc->Save();
}
}
$config = array(
'Enabled' => True,
'PConnect' => False,//Optional, use a persistant connection, Default=False
'Handle' => Null, //Optional, use existing database connection, uses this instead of connecting again
'DieOnError'=> True, //Optional, throw exception on query error, Default=True
'Host' => 'localhost',
'Username' => 'test',
'Password' => '',
'DB' => 'test'
);
$db = new Database($config);
//Create our test tables
//THESE TABLES GET DROPPED AT THE END OF THE SCRIPT (just so you know)
$db->query("CREATE TABLE IF NOT EXISTS `Library` ( ".
"`ID` int(11) NOT NULL auto_increment, ".
"`Name` varchar(150) NOT NULL, ".
"`Date` int(11) NOT NULL, ".
"PRIMARY KEY (`ID`) ".
") DEFAULT CHARSET=utf8");
$db->query("CREATE TABLE IF NOT EXISTS `Descriptions` ( ".
"`ID` int(11) NOT NULL auto_increment, ".
"`BookID` int(11) NOT NULL, ".
"`Desc` text NOT NULL, ".
"PRIMARY KEY (`ID`) ".
") DEFAULT CHARSET=utf8");
AutoDB::SetDB($db);
$library = new Library();
header("Content-Type: text/plain");
$library->AddBook('MyBook', 'Is Awesome');
$library->AddBook('YourBook', 'Is Ok, sure, whatever');
$library->AddBook('TheirBook', 'Blah Blah');
$library->AddBook('iBook', 'Book about some job or another');
$library->AddBook('uBook', 'Popular book');
echo "Top Newest Books\n\n";
foreach($library->GetNewestBooks() as $book)
{
echo "New Book!: ".str_pad($book->Name,15);
echo "Released: ".date("d-m-Y H:i",$book->Date)."\n";
echo "Description: ".$book->Desc."\n\n";
}
$db->query("Drop Table `Library`, `Descriptions`");
|