【问题标题】:cakephp with extjs through insert data in databasecakephp 与 extjs 通过在数据库中插入数据
【发布时间】:2011-11-09 03:12:25
【问题描述】:

我创建 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);
                }

            }]

这有什么问题。它不是在数据库中插入数据。

【问题讨论】:

  • Mayur,我发布了一个答案,但很明显您不了解 Ext 和 CakePHP 的一些基础知识。您需要掌握这些基础知识,然后才能像您尝试做的那样冒险进入高级的东西。在 StackOverflow 上获得答案只是意味着你永远不会真正学习它..

标签: php json cakephp extjs integration


【解决方案1】:

您要做的是使用 ExtJS 添加到网格中。附加到您的网格的商店(如果您按照我对上一个问题的回答)将处理与服务器的对话。

在 ExtJS 中,工具栏中用于向网格添加行的按钮应该有一个处理程序。

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();             
    }
});

在调用 add 之后(如果在您的商店中将 autosave 设置为 true),它会自动调用您在代理的 api 中“create”下设置的 cakephp 应用程序的 URL。它会将这条新记录的数据发送到该操作。

因此,如果您设置了指向 /movies/create 的代理,而不是在 MoviesController 内部设置 create() 操作。

create 操作中,您需要检查 $this->params['form'] 以获取来自 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);
}

在 ExtJs 向 PHP 发送帖子后,它期望返回一个 json 对象,该对象的根中带有一个“成功”键,值为 true 或 false。您需要在 json 中使用它,因此您不能简单地使用 $this->set 并将其发送到您的视图。在这种情况下,我将返回 json_encoding 字符串。

实际上您应该做的是将Js 助手包含在您的app_controller 中。然后创建一个名为 ajaxreturn 的元素。 /views/elements/ajaxreturn.ctp 将包含一行。

<?php echo $this->Js->object($data) ?>

Object负责将$data转成json对象。它被用来代替 json_encode,因为 PHP4 不支持 json_encode。

现在你有了这个元素,在你的控制器中你可以像这样重写它......

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');
}

您想返回 json 字符串并且只返回 json 字符串。没有布局,没有 html,只有它会抛出错误的字符串。

执行此操作后,您的商店将知道调用是否成功,如果成功,它将在您的网格中显示一行。如果没有,它将删除临时。行它放在你的网格中。

【讨论】:

  • 感谢您的回复。但我没有得到它完美。我按照你的步骤,但我没有成功地将数据保存在数据库中。
  • 其实如何在 js 中传递链接。表示如何调用控制器文件中的函数
  • @Mayur,我不确定你在问什么。从您的控制器中“调用” JS 是什么意思?你想做什么?
【解决方案2】:

我不确定我是否理解您的要求。

RequestHandler 是 cake 启用处理 javascript/ajax 请求的方式: http://book.cakephp.org/view/1291/Request-Handling

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 2021-11-08
    • 2015-08-16
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多