Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:21:56+00:00 2026-06-15T19:21:56+00:00

I have set up this method for updating a model via AJAX, and have

  • 0

I have set up this method for updating a model via AJAX, and have tried to extend it by also creating a new model if it doesn’t exist.

public function actionAjaxUpdate($id = null)
{
    if($id === null)
        $model = new MyObject;
    else
        $model = $this->loadModel($id);

    if(isset($_POST['MyObject']))
    {
        $model->attributes = $_POST['MyObject'];
        $model->save();
    }
}

Sample URLs sent when updating via AJAX, the digits at the end being the model ID:

/admin/myObject/ajaxUpdate/1
/admin/myObject/ajaxUpdate/2
/admin/myObject/ajaxUpdate/3
...

The problem here is that while updating works fine as an ID is provided, creating a new model doesn’t work. Because the ID does not exist yet for new items, the URL route will return a 404:

The requested URL /cmsadmin/myObject/ajaxUpdate was not found on this
server.

What would be a good approach to solving this problem?

I have tried overloading the same method without the $id parameter, but PHP does not support overloading, as discussed in this thread: Method overloading in a model of Yii framework

Please leave a comment if any additional information is required.

==== Additional Information ====

main.php

<?php
$backend = dirname(dirname(__FILE__));
$frontend = dirname($backend);
Yii::setPathOfAlias('admin', $backend);

return array(
    'basePath' => $frontend,
    'controllerPath' => $backend . '/controllers',
    'viewPath' => $backend . '/views',
    'runtimePath' => $backend . '/runtime',

    'components' => array(
        'urlManager' => array(
            'urlFormat' => 'path',
            'showScriptName' => false,
            'caseSensitive' => false,
            'appendParams' => true,
            'rules' => array(
                'admin/<controller:\w+>/<action:\w+>/<id:\d+>'=>'/<controller>/<action>',
                'admin/<controller:\w+>/<action:\w+>'=>'/<controller>/<action>',
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:21:58+00:00Added an answer on June 15, 2026 at 7:21 pm

    I had the same problem but I passed in the id=0 for insert and id=1 for update. Make sure the id field is set to auto-increment so it is not required.

    Now check if id is 1 then do insert else update the record.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button and onclick is set to this method which should display
I have this loop method to display a ViewPager and I set an OnClickListener
In rails when updating or creating, if i need to run another method this
I have the following problem when updating a for via AJAX after it is
I have this set of labels and radio buttons that look like this: Entertainment:
I have this set of code which was working fine about an hour or
I currently have this set up and working fine inside a users folder. RewriteEngine
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:
I have a set of data, with columns x and y. This set contains
The default in Ruby on Rails is to have this set to false (in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.