Using Yii ActiveRecord in jquery ajax call

Hi

I am trying to use the jquery ui sortable serialize function in my Yii application as shown below

<script type="text/javascript" >

&#036;(document).ready(function(){


        &#036;(&quot;#menu-list&quot;).sortable({


          handle : '.handle',


          update : function () {


                  var order = &#036;('#menu-list').sortable('serialize');


                  &#036;.post(&quot;/sitemanager/protected/administrator/components/MenuSort.php?&quot;+order);


          }


        });


});

</script>

<p>Drag and drop to rearrange the main menu below</p>

<ul id="menu-list" class="no-list-style rearrange">

<?php foreach (Menu::model()->getEnabledMainMenuItems() as $menu): ?>

&lt;li id=&quot;listItem_&lt;?php echo &#036;menu-&gt;id; ?&gt;&quot; &gt;&lt;img src=&quot;images/arrow.png&quot; alt=&quot;move&quot; width=&quot;16&quot; height=&quot;16&quot; class=&quot;handle&quot; /&gt;&lt;strong&gt;&lt;?php echo &#036;menu-&gt;title; ?&gt;&lt;/strong&gt;&lt;/li&gt;

<?php endforeach; ?>

I had to remove the ‘deny from all’ line in the .htaccess file of the protected directory for now to let this ajax call work. This is MenuSort.php

<?php

updateMenuOrder();

function updateMenuOrder() {

foreach (&#036;_GET['listItem'] as &#036;position =&gt; &#036;item) {


    &#036;menu = Menu::model()-&gt;findByPk(&#036;item);


    &#036;menu-&gt;position = &#036;position;


    &#036;menu-&gt;save(false);


}

}

?>

For some reason the ajax call just fails without any discernible error at this line

$menu = Menu::model()->findByPk($item);

Is there something I have to import or otherwise do to allow this file to use a model like shown?

Thanks for any help in advance.


 print_R($_GET['listItem']);

and see what you get

Its is probably empty for some reason

EDIT: I missread the first time

I dug deeper into the php logs and see this error

PHP Fatal error: Class ‘Menu’ not found in C:\xampp\htdocs\sitemanager\protected\administrator\components\MenuSort.php on line 7

I just assumed by creating a file in the protected directory I would able to access all the models I have but this seems not to be the case.

Any ideas?

The class inside a file must be declared as its same name, for example

components/MyMenu.php contains class MyMeny

in your case your class must be MenuSort or the file must be named Menu.php

Hmm still getting class not found errors. Even if I add a simple Yii import at the top of the file Menu.php like this


<?php


Yii::import('zii.widgets.CPortlet');


class Menu

{

    

    function updateMenuOrder() 

    {

        foreach ($_GET['listItem'] as $position => $item) 

        {

            $menu = Menu::model()->findByPk($item);

            $menu->position = $position;

            $menu->save(false);

        }

    }


}


?>

I still get

PHP Fatal error: Class ‘Yii’ not found in C:\xampp\htdocs\sitemanager\protected\administrator\components\Menu.php on line 3

To generalize the problem, I’m using the jquery ui sortable component thats javascript update function gets called every time a user moves an item on the page


$("#menu-list").sortable({

              handle : '.handle',

              update : function () {

                      var order = $('#menu-list').sortable('serialize');

                      $.post("/sitemanager/protected/administrator/components/Menu.php?"+order);

              }

            });



The javascript update handler uses the jquery ‘post’ function as shown to send data to a php file. This php file then needs to update the database with the information sent. I’m having trouble writing the backend php file in that the file doesn’t seem to recognize any Yii classes or commands.

Is this possible with Yii? Or is there a better way to do this.

Thanks

Solved my problem. Instead of sending the ajax request to a separate file, I just sent it to a method in my controller




<script type="text/javascript" >

    $(document).ready(function(){

            $("#menu-list").sortable({

              handle : '.handle',

              update : function () {

                      var order = $('#menu-list').sortable('serialize');

                      $.post('?r=menu/updateOrder',order);

              }

            });

    });

</script>



and have this method in my controller




public function actionUpdateOrder()

{

      Menu::model()->updateMenuOrder($_POST['listItem']);

}