PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of vench  >  VS ActiveRecord  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example
Class: VS ActiveRecord
Map database records to objects with ActiveRecord
Author: By
Last change:
Date: 1 year ago
Size: 1,459 bytes
 

Contents

Class file image Download
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'VsActiveRecord.php';

class
Post extends VsActiveRecord {
    public function
__construct($id = NULL) {
       
$this->init('tbl_post', 'id', $id);
        if(!
is_null($id)) {
           
$this->load();
        }
    }
   
    public static function
collection($nameAR = __CLASS__) {
        return
parent::collection($nameAR);
    }
}


function
printObject(VsActiveRecord $obj) {
    foreach(
$obj->getColumns() as $k=>$v){
        echo
$k.' => '. $v.'<br/>';
    }
    echo
'<hr/>';
}

/**
 * Create new Post
 */
$u = new Post();
$u->title = 'Some title';
$u->content = 'Some text ';
$u->author_id = 1;
$u->update();
printObject($u);


/**
 * Create new Post
 */
 
$u = Post::collection()->create(array(
    
'title'=>'Some title 2',
    
'content'=>'Some text 2',
    
'author_id'=>2,
 ));
printObject($u);
 
/**
 * Find Post
 */
$posts = Post::collection()->find( 'author_id = 2', null, 2);
foreach(
$posts as $u) {
   
printObject($u);
}
echo (
'Size find rows: '.Post::collection()->getSizeLastQuery());
echo
'<hr/>';

/**
 *Update Posts
 */
$posts = Post::collection()->update(array('title'=>'new title'), 'author_id = 1');
foreach(
$posts as $u) {
   
printObject($u);
}

/**
 *Remove Posts
 */
Post::collection()->delete('author_id = 2');


 
?>