1. #1
    Ext User makcs's Avatar
    Join Date
    Mar 2008
    Posts
    7
    Vote Rating
    0
    makcs is on a distinguished road

      0  

    Wink crud Pattern (php)

    crud Pattern (php)


    Code:
    <?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

    Code:
    <?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!
    Code:
    //---------------------------------------------------------------------------------------------
    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' );
    ...	
    
    }
    //---------------------------------------------------------------------------------------------
    A posse ad esse non vĂ¡let consequentia

  2. #2
    Sencha User
    Join Date
    Dec 2007
    Location
    Aubagne, France
    Posts
    347
    Vote Rating
    1
    mask_hot is on a distinguished road

      0  

    Default


    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?

Thread Participants: 1