cakephp with extjs through insert data in database
I creter js file and add tbar add button when click one blnak row add in grid
in movies controller file i write
function ext_item($id = null) {
if(!empty($this->data)) {
if($this->Movie->save($this->data))
{
$this->set('success','true');
$this->data = array();
return;
}
else {
$this->set('success',"false");
return;
}
}
}
how to pass this js data ?
how to insert data in database?
in controller file
function create() {
$newData = json_decode($this->params['form'], true); // turn the incomin json into an array
$this->data = array(
'Movie' => array(
'date_' => $newData['date_'],
开发者_StackOverflow社区 'notes' => $newData['notes'],
'asset_id' => $newData['asset_id'],
'maint_picture' => $newData['maint_picture'],
'maint_condition1' => $newData['maint_condition1'],
'maint_condition2' => $newData['maint_condition2'],
'maint_condition3' => $newData['maint_condition3'],
'maint_condition4' => $newData['maint_condition4'],
)
);
if ($this->Movie->save($this->data))
{
$data['success'] = true;
} else {
$data['success'] = false;
}
$this->set('data', $data);
//$this->layout = 'ajax';
return $this->render(null, null, '/movies/ext_item');
}
then in js file
var proxy = new Ext.data.HttpProxy({
api: {
// these will map to cakephp controller actions
create: { url: 'movies_controller/create', method: 'POST' },
// read: { url: '/movies_controller/index', method: 'POST' },
//update: { url: '/movies_controller/update', method: 'POST' },
destroy: { url: 'movies_controller/destroy', method: 'POST' }
}
});
and for add row in grid
tbar: [{
text: 'Add Movie',
icon: 'images/table_add.png',
cls: 'x-btn-text-icon',
handler: function() {
grid.getStore().insert(0, new Movie({
id: 0,
notes: 'New Movie',
asset: ''
}));
rowEditor.startEditing(0, true);
}
}]
What wrong with this. it's not insert data in database.
What you want to do is add to the grid using ExtJS. The store that is attached to your grid (if you follow my answer to your last question) will handle talking to the server.
In ExtJS, the button in your toolbar to add a row to your grid should have a handler.
var toolbar = Ext.Toolbar({
// config options
handler: function() {
// in your handler you need to create a new record and insert it into your store
// if you followed my answer to your last question, you'll have setup a store with proxy, jsonreader, and jsonwriter.
// get the store component from Ext
var store = Ext.getCmp('idOfYourStore'),
NewRecord = Ext.data.Record.create(['name', 'genre', 'length']); // this array of column names should match the fields you specified for your JsonReader's fields
// now that you have your store component, and your new blank record. you can fill it in and add it to the store
var record = new NewRecord({
name: 'Name of Movie',
genre: 'Genre of Movie',
length: '1:25:22'
});
store.add(record);
store.commitChanges();
}
});
After calling add (if autosave is set to true on your store) it will automatically call the url to your cakephp application that you setup in your proxy's api under 'create'. It will send the data of this new record to that action.
So if you set up you're create proxy to point to /movies/create
than inside of your MoviesController you want to setup a create()
action.
Inside of the create
action, you'll want to check $this->params['form']
for the incoming data from ExtJS.
function create() {
$newData = json_decode($this->params['form'], true); // turn the incomin json into an array
$this->data = array(
'Movie' => array(
'name' => $newData['name'],
'genre' => $newData['genre'],
'length' => $newData['length']
)
);
if ($this->Movie->save($this->data)) {
$data['success'] = true;
} else {
$data['success'] = false;
}
return json_encode($data);
}
After ExtJs makes the post to PHP it expects a json object back with a 'success' key in the root of the object with true, or false. You need this in json, so you can't simply just use $this->set
and send it to your view. In this case I'm returning the json_encoding string.
In reality what you should do, is include the Js
helper in your app_controller
. Then create an element named ajaxreturn. /views/elements/ajaxreturn.ctp
would contain one line.
<?php echo $this->Js->object($data) ?>
Object is responsible for turn $data
into a json object. It's used instead of json_encode because PHP4 didn't have support for json_encode.
now that you have this element, in your controller you can rewrite it like so...
function create() {
$newData = json_decode($this->params['form'], true); // turn the incomin json into an array
$this->data = array(
'Movie' => array(
'name' => $newData['name'],
'genre' => $newData['genre'],
'length' => $newData['length']
)
);
if ($this->Movie->save($this->data)) {
$data['success'] = true;
} else {
$data['success'] = false;
}
$this->set('data', $data);
$this->layout = 'ajax';
return $this->render(null, null, '/elements/ajaxreturn');
}
You want to return the json string and ONLY the json string. No layout, no html, nothing but the string it will throw an error.
Once you do this, your store will know whether the call was successful, if so it will stick a row in your grid. If not, it will delete the temp. row it put in your grid.
I'm not sure I understanding what you're asking for.
RequestHandler is how cake enables handling javascript/ajax requests: http://book.cakephp.org/view/1291/Request-Handling
精彩评论