PhpToolCase
Api Documentation Version 0.9.2
|
The Object Relational Mapping Component is a great tool for retrieving and manipulating database tables data with just a couple of lines of code. The class can be used with the PtcEvent component to add event listeners to queries. This class can also be used to call methods from the PtcQueryBuilder component directly. Retrieved rows can be converted to arrays or json arrays. The PtcMapper can also use shortcut methods to retrieve / update single column values.
Main Features:
To get started using this component, you just need to extend the class:
And that's about it. The class name will be used as the database table name in lowercase letters.
If you need to specify the table name, use the static property PtcMapper::$_table in your extended class:
To retrieve all records from the table, the method PtMapper:all( ) can be used:
To retrieve 1 record by it's id, the method PtcMapper:find( ) can be used:
The PtcMapper component can be used to call the query builder method directly.
Have a look at the following example to undestand how this can be done:
Adding new records is very simple, have a look at the following example:
To create a new record from an associative array use the PtcMapper:create( ) method:
To update records the method PtcMapper::save( ) can be used:
To delete a record, the method PtcMapper::delete( ) can be used:
The query builder can also be used with a where clause to delete records:
Refer to the Query Builder Component (PtcQueryBuilder) user manual for detailed usage over the PtcQueryBuilder.
The returned values from the queries, can be converted to array or json. The following paragraphs will demostrate how:
If you need to convert a record values to an array, the method PtcMapper::toArray( ) can be used:
To convert records to json array, use the PtcMapper::toJson( ) method:
There are shortcuts to retrieve single column values with the component, by using the prefix "get_"
followed by the column name:
Updating 1 single value based on the record id, can be done quickly by using the prefix "set_"
followed by the column name that you want to update:
To map field names, use the property PtcMapper::$_map like shown in the following example:
The PtcMapper component can be used with the PtcEvent class, to add event observers to the queries.
Here follows a list with all event observers and possible arguments, when they are fired:
&$values
), called before row insert &$values
, $result
), called after row insert &$values
), called before row update &$values
, $result
), called after row update &$values
), called before row insert and update &$values
, $result
), called after row insert and update &$values
), called before row delete &$values
, $result
), called after row delete "$result"
parameter.To use these observer events, add them as static methods to your class:
Observers can be registered with the PtcMapper::observe( ) method:
To add options on on initliazation, use the boot( )
method, like shown in the next example:
By default the PtcMapper component uses the "default"
connection from the PtcDb component.
To specify a different connection, use the PtcMapper::$_connectionName property to specify the connection to use:
By default the PtcMapper uses the string "id"
to identify the primary key for the table.
To specify a different column name, use the PtcMapper::$_uniqueKey property:
To specify a different name for the event class, use the static property PtcMapper::$_eventClass, inside your extend class:
To specify a different name for the event class, use the static property PtcMapper::$_connectionManager, inside your extend class:
To add options on initialization, add the method boot( )
statically to your extended class: