PDA

View Full Version : How server side CRUD works ?



shensi
9 Apr 2012, 1:49 AM
How server side CRUD works ?
Hi, I am new to sencha and thanks to many examples ext-4.0.7-gpl contains, I managed to make it works on client side. I am wondering why I don't find much informations about the server side content :

>>app.php
>> remote
>> | -init.php
>> | -lib
>>>> | -----application_controller.php
>>>> | -----model.php
>>>> | -----request.php
>>>> | -----response.php
>>>> | -----session_db.php
>> | -app
>>>> | -----models
>>>> | -----controllers

That's the only one link I found about this subject (http://joekuan.wordpress.com/2011/01/10/how-to-use-ext-js-crud-php-example/)

I managed, with the sencha's documentation to get it on server side / client side to send data and receive them in a store. The only description is translation and storage in standard column/row through json,xml in order to make it easily in client side... Perhaps I didn't understand everything as I am a beginner :-)

So Could someone explain me how to use and personalize CRUD ?
How do I get my sent parameters on server side ?

I have a dataStore, wich displayed my static json's data from a file properly ! Cool
I would like to make a mysql select and filtered it with an ID sent as a parameter but it doesn't work and mysql return an error because of this ...

And when I set statically this ID on server Side, ajax request works perfectly

Here is the error message from firebug



<br /><b>Warning</b>: require(remote/app/controllers/.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in <b>/usr/local/domos/www/sencha/examples/feed-viewer/app.php</b> on line <b>10</b><br /><br /><b>Fatal error</b>: require() [<a href='function.require'>function.require</a>]: Failed opening required 'remote/app/controllers/.php' (include_path='.:/usr/share/php:/usr/share/pear') in <b>/usr/local/domos/www/sencha/examples/feed-viewer/app.php</b> on line <b>10</b><br />






Unable to parse the JSON returned by the server: You're trying to decode an invalid JSON String: <br /><b>Warning</b>: require(remote/app/controllers/.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in <b>/usr/local/domos/www/sencha/examples/feed-viewer/app.php</b> on line <b>10</b><br /><br /><b>Fatal error</b>: require() [<a href='function.require'>function.require</a>]: Failed opening required 'remote/app/controllers/.php' (include_path='.:/usr/share/php:/usr/share/pear') in <b>/usr/local/domos/www/sencha/examples/feed-viewer/app.php</b> on line <b>10</b><br />' when calling method: [nsIDOMEventListener::handleEvent]




FYI Here is my dataStore:
Ext.apply(this, {
cls: 'feed-grid',
store: Ext.create('Ext.data.Store', {
model: 'DeviceDetails',
sortInfo: {
property: 'datetime',
direction: 'DESC'
},
proxy: {
type: 'ajax',
//url: 'data/devices.json',
url: 'app.php/events/view',
reader: {
type: 'json',
root: 'data'
}
},
listeners: {
load: this.onLoad,
scope: this
}
}),
columns: [{
text: 'Source',
dataIndex: 'source',
flex: 1,
renderer: this.formatTitle
}, {
text: 'Type',
dataIndex: 'type',
//hidden: true,
//width: 200,
flex: 1,
renderer: this.formatTitle


}, {
text: 'Value',
dataIndex: 'value'
//hidden: true,
//width: 200


}, {
text: 'Date',
dataIndex: 'datetime',
renderer: this.formatDate,
width: 200
}]
});
this.callParent(arguments);
this.on('selectionchange', this.onSelect, this);
},


FYI Here is my method to load and fill my dataStore


loadDevice: function(dID){
console.log("DeviceGrid::loadDevice -> " + dID);
var store = this.store;
store.load({
params: {
dID: dID
}
});
},


And sorry for my bad english !
Regards,
Shensi

scottmartin
9 Apr 2012, 6:14 AM
It is difficult to say what the problem may be since we cannot see your entire code. It looks like your controllers/views may not be setup properly and 'views' //aka: 'requires' are not properly set.

Here is a good post from one of our users who recently posted about setting up a MVC application:
(with online demo)
http://ext4all.com/post/extjs-4-mvc-application-architecture

Regards,
Scott.

shensi
9 Apr 2012, 7:47 AM
Hi, thank you very much for your answer and for the link.
I checked the demo and the rar archive doesn't contain server side code (PHP and stuffs).

As I explained, the client side code is working well. When I click on my element, I can see the ajax request with firebug. I would like to make a sql request filtered with this ID (2)

33782

When I don't use a filter and i select everything in my sql table, the response is displayed properly in my grid. But I don't know how to get the parameter and how to use it from the server side...

In a old way (lol), I use to get variable like this in PHP: $_GET['id'] or $_POST['id'] or $_REQUEST['id']
But here, I am a bit lost because httpProxy object is declared with API on each CRUD operation


proxy: {
type: 'ajax',
//url: 'data/devices.json',
url: 'app.php/events/view',
//startParam: undefined,
/*api: {
create: '/user/add',
//read: '/user/list',
update: '/user/update',
destroy: '/user/delete'
},*/
reader: {
type: 'json',
//root: 'results',
root: 'data'
//successProperty: 'success'
}
},


The URL for read API is declared as app.php/events/view according to the code in remote/lib/request.php
But how can I :
1) Find the id I provided in my ajax request ?
2) Hack or bypass the CRUD to provide this id parameter to the good function ??

-------------

This is the code provided with some sencha examples. I put flags to redirect the output of function in a file but I don't this F**** ID !!!



>>app.php
>> remote
>> | -init.php
>> | -lib

>>>> | -----application_controller.php
>>>> | -----model.php
>>>> | -----request.php
>>>> | -----response.php
>>>> | -----session_db.php
>> | -app
>>>> | -----models
>>>> | -----controllers

app.php


<?php
require('remote/init.php');


// Get Request
$request = new Request(array('restful' => false));


//echo "<P>request: " . $request->to_string();


// Get Controller
require('remote/app/controllers/' . $request->controller . '.php');
$controller_name = ucfirst($request->controller);
$controller = new $controller_name;




// Dispatch request
echo $controller->dispatch($request);




init.php

<?php
session_start();

// base framework
require(dirname(__FILE__).'/lib/session_db_1.php');
require(dirname(__FILE__).'/lib/application_controller.php');
require(dirname(__FILE__).'/lib/model.php');
require(dirname(__FILE__).'/lib/request.php');
require(dirname(__FILE__).'/lib/response.php');


// require /models (Should iterate app/models and auto-include all files there)
require(dirname(__FILE__).'/app/models/event.php');


// Fake a database connection using _SESSION
$events_dbh = new SessionDB();


application_controller.php


<?php
/**
* @class ApplicationController
*/
class ApplicationController {
public $request, $id, $params;


/**
* dispatch
* Dispatch request to appropriate controller-action by convention according to the HTTP method.
*/
public function dispatch($request) {
$this->request = $request;
$this->id = $request->id;
$this->params = $request->params;

//exec("echo ApplicationController::dispatch::request - $this->request->method >> /tmp/sencha.txt");
exec("echo ApplicationController::dispatch::id - $this->id >> /tmp/sencha.txt");
exec("echo ApplicationController::dispatch::params - $this->params >> /tmp/sencha.txt");

if ($request->isRestful()) {
exec("echo ApplicationController::dispatch::isRestful=yes >> /tmp/sencha.txt");
return $this->dispatchRestful();
}
if ($request->action) {
$ret = $this->{$request->action}();
exec("echo ApplicationController::dispatch::action=yes :: $ret >> /tmp/sencha.txt");
return $this->{$request->action}();
}


// normal dispatch here. discover action
}


protected function dispatchRestful() {
shell_exec("echo ApplicationController::dispatchRestful : ".$this->request->method." >> /tmp/sencha.txt");
switch ($this->request->method) {
case 'GET':
//return $this->view();
return $this->deviceDetails();
break;
case 'POST':
return $this->create();
break;
case 'PUT':
return $this->update();
break;
case 'DELETE':
return $this->destroy();
break;
}
}
}

response.php

<?php
/**
* @class Response
* A simple JSON Response class.
*/
class Response {
public $success, $data, $message, $errors, $tid, $trace;


public function __construct($params = array()) {
$this->success = isset($params["success"]) ? $params["success"] : false;
$this->message = isset($params["message"]) ? $params["message"] : '';
$this->data = isset($params["data"]) ? $params["data"] : array();

exec("echo Response::__construct $this->success >> /tmp/sencha.txt");
exec("echo Response::__construct $this->message >> /tmp/sencha.txt");
exec("echo Response::__construct $this->data >> /tmp/sencha.txt");
}


public function to_json() {
exec("echo Response::to_json >> /tmp/sencha.txt");
return json_encode(array(
'success' => $this->success,
'message' => $this->message,
'data' => $this->data
));
}
}