cakephp 与 extjs 通过在数据库中插入数据

发布于 2024-12-02 13:05:55 字数 2497 浏览 0 评论 0原文

我创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-12-09 13:05:55

您想要做的是使用 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,只有字符串,它会抛出错误。

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

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.

小耗子 2024-12-09 13:05:55

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文