Pork.dbObject examples

First, we define the dbObjects that will be used on this page

 <?php    
class Blog extends dbObject
{
    function 
__construct($ID=false)
    {
        
$this->__setupDatabase('blogs'// database table
        
array('ID_Blog' => 'ID',    // database field => mapped object property
            
'strPost' => 'Story',    // as you can see, database field strPost is mapped to $this->Story
            
'datPosted' => 'Posted',
            
'strPoster' => 'Author',
            
'strTitle'  => 'Title'),
            
'ID_Blog',    // primary table key 
            
$ID);    // value of primary key to init with (can be false for new empty object / row)
        
$this->addRelation('Reaction'); // define a 1:many relation to Reaction 
        
$this->addRelation('Tag''BlogTag'); // define a many:many relation to Tag through BlogTag
    
}
}

class 
Reaction extends dbObject
{
    function 
__construct($ID=false)
    {
        
$this->__setupDatabase('reactions'// database table
        
array('ID_Reaction' => 'ID',    // database field => mapped object property
            
'ID_Blog' => 'ID_Blog',
            
'strPoster' => 'Poster',
            
'strEmail' => 'Email',
            
'strIp' => 'Ip',
            
'strReaction' => 'Reaction',
            
'datReply' => 'ReplyDate'),
            
'ID_Reaction'// primary table key 
            
$ID);    // value of primary key to init with (can be false for new empty object / row)
        
$this->addRelation('Blog'); // define a many:many relation to Blog through Reaction
    
}
}


class 
Tag extends dbObject
{
    function 
__construct($ID=false)
    {
        
$this->__setupDatabase('tags'// database table
        
array('ID_Tag' => 'ID',    // database field => mapped object property
            
'strTag' => 'Tag',),
            
'ID_Tag'// primary table key 
            
$ID);    // value of primary key to init with (can be false for new empty object / row)
        
$this->addRelation('Blog''BlogTags'); // define a many:many relation to Blog through BlogTags
    
}
}


class 
BlogTag extends dbObject
{
    function 
__construct($ID=false)
    {
        
$this->__setupDatabase('blogs_x_tags'// database table
        
array('ID_BlogTag' => 'ID',    // database field => mapped object property
            
'ID_Blog' => 'ID_Blog',
            
'ID_Tag' => 'ID_Tag'),
            
'ID_BlogTag'// primary table key 
            
$ID);    // value of primary key to init with (can be false for new empty object / row)
        
$this->addRelation('Blog');// define a 1:1 relation to Blog
        
$this->addRelation('Tag'); // define a 1:1 relation to Tags
    
}
}

To insert a new Blog into the database

 <?php    
$weblog 
= new Blog(); // create an empty object to work with. 
$weblog->Author 'SchizoDuckie'// mapped internally to strAuthor. 
$weblog->Title 'A test weblog';  
$weblog->Story 'This is a test weblog!'
$weblog->Posted date("Y-m-d H:i:s"); 
$weblog->Save(); // Checks for any changed values and inserts or updates into DB. 
echo ($weblog->ID// outputs: 1 

Create an existing object through ID and query some properties

 <?php    
$weblog 
= new Blog(1);
echo 
$weblog->Title;
echo 
$weblog->Contents;
Query the relations of the object  <?php    
print_r
($weblog->relations);
Outputs:
 

Array  
(
	[Reaction] => stdClass Object
		(
			[relationType] => RELATION_NOT_ANALYZED
		)

	[Tag] => stdClass Object
		(
			[relationType] => RELATION_NOT_ANALYZED
			[connectorClass] => BlogTag
		)

)

There are 5 types of relations defined:

RELATION_SINGLE
a 1:1 relation
RELATION_FOREIGN
a many:1 or 1:many relation
RELATION_MANY
a many:many relation
RELATION_NOT_RECOGNIZED
Not a recognized relation (primary keys don't match)
RELATION_NOT_ANALYZED
Relation type not analyzed yet because it's not neccessary. (e.g. for new, uninitialized objects) It'll analyze at the moment you call
$object->Save();

The Find() function

every dbObject inherits the Find() function which has the following syntax:  <?php    

$object
->Find(string className_to_find, [array mapped_filters [,array mapped_order_by_and group_by [, array  only_these_fields]]]);

Examples:  <?php    

$emptylog 
= new Weblog();
$bloglist $emptylog->Find('Weblog', array(), array('limit 0,5'));

Fetches 5 weblog objects from the table. (yes, you can also find the class you just created

 <?php    

$weblog 
= new Weblog(1);
$replies $weblog->Find('Reaction', array("ReplyDate < date_sub(current_date(), interval 21 day)"));

Fetches only the last 3 weeks of replies.

 <?php    

    $replies 
$weblog->Find('Reaction', array("ReplyDate < date_sub(current_date(), interval 21 day)"), array('Order by ReplyDate desc'),     array('ID_Reaction''strReaction'));


Fetches only the ID and Reaction properties last 3 weeks of replies. This is useful if you don't want to fetch large text fields or something.

Fetches all reactions from 'some random guy', ordered.

 <?php    

    $replies 
$weblog->Find('Reaction', array("Poster"=>"some random guy"), array('order by ReplyDate desc'));

you can also mix the array keys and non-keyed items to auto-form an AND query

 <?php    

$replies 
$weblog->Find('Reaction', array("Poster" => "some random guy""ReplyDate < current_date()"));
//will search ReplyDate < current_date() and Poster = "some random guy"

Once you've found what you're looking for:

 <?php    
if($replies != false// check if we actually have results
{
    for(
$i=0$i<sizeof($replies); $i++) 
    {
        echo 
$replies[$i]->Reaction.'<hr/>';
    }
}

To create a new weblog, change some values and create a reaction to it

 <?php    
$weblog 
= new Blog(); // create an empty object to work with. 
$weblog->Author 'SchizoDuckie'// mapped internally to strAuthor. 
$weblog->Title 'A test weblog';  
$weblog->Story 'This is a test weblog!'
$weblog->Posted date("Y-m-d H:i:s"); 
$weblog->Save(); // Checks for any changed values and inserts or updates into DB. 

$reply = new Reaction();
$reply->Author 'Some random guy';
$reply->Reply 'w000t';
$reply->ReplyDate date("Y-m-d H:i:s");
$reply->Ip '127.0.0.1';
$reply->Connect($weblog); // auto-saves $reply and connects it to $weblog->ID 

To create 2 tags, and connect them to an existing Blog

 <?php    

$tag1 
= new Tag();
$tag1->Tag "test";

$tag2 = new Tag();
$tag2->Tag "test2";

$weblog->Connect($tag); // auto-creates the BlogTag object to hook these 2 together
$weblog->Connect($tag2); // auto-creates the BlogTag object to hook these 2 together