PDA

View Full Version : crud Pattern (php)



makcs
22 Dec 2009, 4:03 AM
<?php //crud.php
//---------------------------------------------------------------------------------
class dsCRUD
{
protected $table;
// directFn:
// function getAll()
// {
// $result = w2u( Mapper::find( $this->table ) );
// return array( 'total' => count( $result ), 'root' => $result ) ;
// }
//-----------------------------------------------------------------------------
function create( $data = NULL )
{
$fields = array();
if ( !is_null( $data ) )
{
foreach ( $data->root as $field => $value );
$fields[ $field ] = u2w( $value );
}

$obj = DomObj::factory( $this->table, $fields );
Mapper::save();

$result[ 'id' ] = $obj->id;
// $result[ 'root' ] = w2u( $obj );
$result[ 'success' ] = true;

return $result;
}
//-----------------------------------------------------------------------------
function read( $data = NULL )
{
if ( isset( $data->sort ) )
$result[ 'root' ] = w2u( Mapper::find( $this->table, '1=1', $data->sort . ' ' . $data->dir ) );
else
$result[ 'root' ] = w2u( Mapper::find( $this->table ) );

$result[ 'total' ] = count( $result );
$result[ 'success' ] = true;
return $result;
}
//-----------------------------------------------------------------------------
function update( $data = NULL, $param = NULL )
{
$upd = DomObj::factory( $this->table, array( 'id' => intval( $data->root->id ) ) );

foreach ( $data->root as $field => $value )
$upd->set( $field, u2w( $value ) );

Mapper::save();
// return array( 'successProperty' => true, 'success' => true );
// $result[ 'successProperty' ] = true;
$result[ 'success' ] = true;
return $result;
}
//-----------------------------------------------------------------------------
function destroy( $data )
{
Mapper::ffind( $this->table, 'id = ' . intval( $data->root ) )->free();
Mapper::save();

// $result[ 'root' ] = $data;
$result[ 'success' ] = true;
return $result;
}
//-----------------------------------------------------------------------------
}

?>

Place into Direct Classes folder this


<?php
//---------------------------------------------------------------------------------

require_once( '../crud.php' );

//---------------------------------------------------------------------------------
class dsBrands extends dsCRUD
{
protected $table = 'Brands';
// function getAll()
// {
// return parent::getAll();
// }
// //-----------------------------------------------------------------------------
// function create( $data = NULL )
// {
// return parent::create( $data );
// }
// //-----------------------------------------------------------------------------
// function read( $data = NULL )
// {
// return parent::read( $data );
// }
// //-----------------------------------------------------------------------------
// function update( $data = NULL, $param = NULL )
// {
// return parent::update( $data, $param );
// }
// //-----------------------------------------------------------------------------
// function destroy( $data )
// {
// return parent::destroy( $data );
// }
//-----------------------------------------------------------------------------
}

?>
uncomment methods for override if you need...

and with http://www.extjs.com/forum/showthread.php?t=88179

and you can use Direct NOW! :D


//---------------------------------------------------------------------------------------------
Ext.ns('dsTemp');
dsTemp = Ext.extend(Ext.data.DirectStore, {
constructor:function(config) {
config = config || {};
config.listeners = config.listeners || {};
Ext.applyIf(config.listeners, {
load: function(){
tr( 'load' );
},
save: function(s){
tr( 'save' );
s.commitChanges();
},
remove: function(){
tr( 'remove' );
},
update: function(s,r){
tr( 'update' );
// if ( r.isValid() )
// s.commitChanges();
// else
// s.rejectChanges();
},
write: function(store, action, result, res, rs) {
tr( 'write' );
},
exception: function(proxy, type, action, options, res, arg) {
tr( 'exception' );
}
});
dsTemp.superclass.constructor.call(this, config);
} // eo function constructor
,initComponent:function() {
var config = {
autoLoad: true,
autoSave: true
}; // eo config object
Ext.apply(this, Ext.apply(this.initialConfig, config));
dsTemp.superclass.initComponent.apply(this, arguments);
} // eo function initComponent
,onRender:function() {
dsTemp.superclass.onRender.apply(this, arguments);
} // eo function onRender
}); // eo extend
Ext.reg('dsTemp', dsTemp);

//---------------------------------------------------------------------------------------------

function dsFactory( id, ds, fields, sortfld ){
return dsBrandsStore = new dsTemp({
id: id
,api: {
create: ds.create,
read: ds.read,
update: ds.update,
destroy: ds.destroy
}
,reader: new Ext.data.JsonReader({
root: 'root'
,fields: fields
})
,writer: new Ext.data.JsonWriter({ encode: false })
,sortInfo: {
field: sortfld
// ,direction: 'ASC' // or 'DESC' (case sensitive for local sorting)
}
});
}

//---------------------------------------------------------------------------------------------

var dsBrandsStore = null;

function initDataStore(){

dsBrandsStore = dsFactory( 'dsBrands', dsBrands, [{name:'name', type: 'string'}], 'name' );
...

}
//---------------------------------------------------------------------------------------------

mask_hot
3 Feb 2010, 5:30 AM
Hi,

In your crud.php, what is the Mapper object?

and in your javascript, if we don't want to use a factory, how do we do?