cakephp 与 extjs 通过在数据库中插入数据
我创建了 js 文件,并在单击电影控制器文件中的网格中添加一个 blnak 行时添加 tbar 添加按钮,
我编写
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;
}
}
}
如何传递此 js 数据?
如何向数据库中插入数据?
在控制器文件中
function create() {
$newData = json_decode($this->params['form'], true); // turn the incomin json into an array
$this->data = array(
'Movie' => array(
'date_' => $newData['date_'],
'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');
}
,然后在 js 文件中
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' }
}
});
,然后在网格中添加行,
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);
}
}]
这有什么问题。它不是在数据库中插入数据。
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_'],
'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的是使用 ExtJS 添加到网格中。附加到您的网格的商店(如果您遵循我对上一个问题的回答)将处理与服务器的通信。
在 ExtJS 中,工具栏中用于向网格添加行的按钮应该有一个处理程序。
调用 add 后(如果您的商店中的 autosave 设置为 true),它将自动调用您在代理 api 中“create”下设置的 cakephp 应用程序的 url。它将将此新记录的数据发送到该操作。
因此,如果您将创建代理设置为指向
/movies/create
而不是在 MoviesController 内部,则需要设置create()
操作。在
create
操作中,您需要检查$this->params['form']
是否有来自 ExtJS 的传入数据。ExtJs 向 PHP 发送帖子后,它期望返回一个 json 对象,该对象的根中带有“成功”键,值为 true 或 false。您需要 json 格式的值,因此您不能简单地使用
$this->set
并将其发送到您的视图。在本例中,我返回 json_encoding 字符串。实际上,您应该做的是将
Js
帮助器包含在您的app_controller
中。然后创建一个名为 ajaxreturn 的元素。/views/elements/ajaxreturn.ctp
将包含一行。Object 负责将
$data
转换为 json 对象。它用来代替 json_encode,因为 PHP4 不支持 json_encode。现在你有了这个元素,在你的控制器中你可以像这样重写它......
你想要返回 json 字符串并且只返回 json 字符串。没有布局,没有 html,只有字符串,它会抛出错误。
执行此操作后,您的商店将知道调用是否成功,如果成功,它将在您的网格中粘贴一行。如果没有,它将删除临时文件。将其放入网格中。
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.
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 acreate()
action.Inside of the
create
action, you'll want to check$this->params['form']
for the incoming data from ExtJS.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 yourapp_controller
. Then create an element named ajaxreturn./views/elements/ajaxreturn.ctp
would contain one line.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...
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.
我不确定我是否理解你的要求。
RequestHandler 是 cake 处理 javascript/ajax 请求的方式:
http://book.cakephp.org/view/1291/Request-Handling
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