22 public function reset( ) { $this->_fields = array( ); }
27 public function remove( $key ) { unset( $this->_fields[ $key ] ); }
35 public function toJson( ){
return json_encode( $this->_fields ); }
41 public static function create( $array )
43 $class = get_called_class( );
44 $record =
new $class( );
45 foreach ( $array as $k => $v ){ $record->$k = $v; }
52 public function delete( )
54 $storage = static::$_storage[ get_called_class( ) ];
55 static::_fireEvent(
'deleting' , array(
56 &$this->_fields[ static::$_uniqueKey ] , &$this->_fields ) );
57 $result = static::_getQB( )->table( $storage[
'table' ] )
58 ->delete( $this->_fields[ static::$_uniqueKey ] )
60 static::_fireEvent(
'deleted' ,
61 array( &$this->_fields[ static::$_uniqueKey ] , &$this->_fields , &$result ) );
71 $storage = static::$_storage[ get_called_class( ) ];
72 if ( empty( $this->_fields ) )
74 trigger_error(
'Nothing to save in table' . static::$storage[
'table' ] .
75 '!' , E_USER_WARNING );
78 static::_mapFields( );
80 static::_fireEvent(
'saving' , array( &$values ) );
81 if ( array_key_exists( static::$_uniqueKey , $this->_fields ) )
83 static::_fireEvent(
'updating' , array( &$values ) );
84 unset( $values[ static::$_uniqueKey ] );
85 $result = static::_getQB( )->table( $storage[
'table' ] )
86 ->update( $values , $this->_fields[ static::$_uniqueKey ] )
88 static::_fireEvent(
'updated' , array( &$values , $result ) );
92 static::_fireEvent(
'inserting' , array( &$values ) );
93 $result = static::_getQB( )->table( $storage[
'table' ] )
94 ->insert( $this->_fields )->run( );
95 static::_fireEvent(
'inserted' , array( &$values , $result ) );
97 static::_fireEvent(
'saved' , array( &$values , $result ) );
106 public static function find( $id )
108 $class = static::_initialize( );
109 static::_getQB( )->setFetchMode( PDO::FETCH_CLASS |
110 PDO::FETCH_PROPS_LATE , $class );
111 return $result = static::_getQB( )->table( static::$_storage[ $class ][
'table' ] )
112 ->where(
'id' ,
'=' , $id )
119 public static function all( )
121 $class = static::_initialize( );
122 static::_getQB( )->setFetchMode( PDO::FETCH_CLASS |
123 PDO::FETCH_PROPS_LATE , $class );
124 return $result = static::_getQB( )->table( static::$_storage[ $class ][
'table' ] )
133 $class = static::_initialize( );
134 if ( array_key_exists(
'columns' , static::$_storage[ $class ] ) )
136 return static::$_storage[ $class ][
'columns' ];
139 static::_getQB( )->setFetchMode( PDO::FETCH_ASSOC );
140 $columns = static::_getQB( )->run(
'SHOW COLUMNS FROM ' .
141 static::_getQB( )->sanitize( static::$_storage[ $class ][
'table' ] ) );
142 foreach ( $columns as $name ){ $cols[ $name[
'Field' ] ] = $name[
'Field' ]; }
143 return static::$_storage[ $class ][
'columns' ] = $cols;
149 public static function observe( $class = null )
151 if ( !class_exists( $events_class = static::$_eventClass ) )
153 trigger_error( $events_class .
' NOT FOUND!' , E_USER_ERROR );
156 $class = ( $class ) ? $class : get_called_class( );
157 $methods = get_class_methods( $class );
158 foreach ( static::$_events as $event )
160 if ( in_array( $event , $methods ) )
162 $cls = strtolower( $class );
163 $events_class::listen( $cls .
'.' . $event , $class .
'::' . $event );
164 static::$_observers[ get_called_class( ) ][ $cls .
'.' . $event ] = $event;
173 public function __set( $key , $value )
175 if ( !static::_checkColumn( $key ) ){
return false; }
176 return $this->_fields[ $key ] = $value;
184 if ( !static::_checkColumn( $key ) ){
return false; }
185 return $this->_fields[ $key ];
196 $class = static::_initialize( );
197 if ( strpos( $method ,
'get_' ) === 0 )
199 $meth = explode(
'get_' , $method );
200 if ( !static::_checkColumn( $meth[ 1 ] ) ){
return false; }
201 $column = ( !array_key_exists( 1 , $args ) ) ? static::$_uniqueKey : $args[ 0 ];
202 $value = ( !array_key_exists( 1 , $args ) ) ? $args[ 0 ] : $args[ 1 ];
203 return static::_getQB( )->table( static::$_storage[ $class ][
'table' ] )
204 ->where( $column ,
'=' , $value )
207 else if ( strpos( $method ,
'set_' ) === 0 )
209 $meth = explode(
'set_' , $method );
210 if ( !static::_checkColumn( $meth[ 1 ] ) ){
return false; }
211 static::_fireEvent( array(
'saving' ,
'updating' ) , array( &$meth , &$args ) );
212 $result = static::_getQB( )->table( static::$_storage[ $class ][
'table' ] )
213 ->where( static::$_uniqueKey ,
'=' , $args[ 1 ] )
214 ->update( array( $meth[ 1 ] => $args[ 0 ] ) )
216 static::_fireEvent( array(
'updated' ,
'saved' ) , array( &$meth , &$args , &$result ) );
219 $qb = static::_getQB( )->table( static::$_storage[ $class ][
'table' ] );
220 $qb->setFetchMode( PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE , $class );
221 return call_user_func_array( array( $qb , $method ), $args );
252 'inserting' ,
'inserted' ,
'updating' ,
'updated' ,
253 'deleting' ,
'deleted' ,
'saving' ,
'saved'
274 $storage = static::$_storage[ get_called_class( ) ];
275 if ( !array_key_exists( $column , $storage[
'columns' ] ) &&
276 !in_array( $column , static::$_map ) )
278 trigger_error(
'Column ' . $column .
' does not exists in table ' .
279 $storage[
'table' ] .
'!' , E_USER_ERROR );
291 $event = ( is_array( $event ) ) ? $event : array( $event );
292 $event_class = static::$_eventClass;
293 if ( array_key_exists( $class = get_called_class( ) , static::$_observers ) )
295 foreach ( static::$_observers[ $class ] as $k => $v )
297 foreach ( $event as $ev )
299 if ( $v === $ev ){ $event_class::fire( $k , $data ); }
309 $manager = static::$_connectionManager;
310 return call_user_func( $manager .
'::getQB' , static::$_connectionName );
319 $db = static::_getQB( );
320 if ( !array_key_exists( $class = get_called_class( ) , static::$_storage ) )
322 static::$_storage[ $class ] = array( );
323 if ( static::$_table ){ static::$_storage[ $class ][
'table' ] = static::$_table; }
326 static::$_storage[ $class ][
'table' ] = strpos( $class ,
'\\' ) ?
327 strtolower( end( explode(
'\\' . $class ) ) ) : strtolower( $class );
329 $db->run(
'SHOW TABLES LIKE ?' , array( static::$_storage[ $class ][
'table' ] ) );
330 if ( !$db->countRows( ) )
332 trigger_error(
'Table ' . static::$_storage[ $class ][
'table' ] .
333 ' does not exists, quitting now!' , E_USER_ERROR );
336 static::$_storage[ $class ][
'columns' ] = static::getColumns( );
337 if ( method_exists( $class ,
'boot' ) ){ static::boot( ); }
346 if ( !empty( static::$_map ) )
348 foreach ( static::$_map as $k => $v )
350 if ( array_key_exists( $v , $this->_fields ) )
352 $this->_fields[ $k ] = $this->_fields[ $v ];
353 unset( $this->_fields[ $v ] );
363 static::_initialize( );
364 return static::_getQB( )->lastId( );
save()
Inserts a new record in table.
static $_uniqueKey
Unique row identifier column name.
_mapFields()
Replaces column names with values in the PtcMapper::$_map property.
static _initialize()
Initializes the class, adding columns and table name to the PtcMapper::$_storage property.
__set($key, $value)
Sets values.
__construct()
Retrives the query builder from the connection manager and table column names.
static __callStatic($method, $args)
Calls shortcut methods for getting / setting single values, or the QueryBuilder methods directly...
static getColumns()
Retrieves column names from table.
static $_storage
Column and table names property.
toJson()
Returns values as a json array.
toArray()
Returns values as an associative array.
static find($id)
Retrieves single record from the table based on id.
static $_eventClass
Event class name property.
__get($key)
Retrieves values.
static _fireEvent($event, $data)
Fires events if methods are present in observers classes.
$_fields
Array of created values property.
static $_events
Possible observer events array.
static $_table
Database table property.
static $_connectionName
Connection name to be used property.
reset()
Resets previously set values.
static _getQB()
Retrieve the query builder object if present.
static $_observers
Property that holds the observer classes.
static $_map
Maps column names to fields, if "as" is used.
static $_connectionManager
Connection Manager class name property.
static create($array)
Sets values based on associative array.
static all()
Gets all records.
static _checkColumn($column)
Checks if a given column name exists in table.
static observe($class=null)
Adds observers to the class to use event listeners with the queries.
static lastId()
Retrieves last inserted id.