I'm building a rudamentary backend system which provides methods to
upload files and store references to them (along with a few other
infos) in the database.  The admin tool needs to be able to reorder the
list which is displayed from the db data, so I added a field 'index' to
use for sort order (ASC/DESC).  There are arrows on the interface which
SHOULD effectively swap on row for another by copying they db info into
two separate variables, deleting the current records and resaving the
rows (only whith their 'index' values swaped).  This works 85% of the
time, the other 15% the rows get reinserted but the index values ar the
same...

the controller code (in submovies_controller):

function admin_moveUp($id,$parent_id){
                $this->autorender=false;
                
$currRec=$this->Submovie->find(array('id'=>$id),null,null,false);
                $currRec=$currRec['Submovie'];
                $currIndex=$currRec['index'];
                if($currIndex>1){
                        
$prevRec=$this->Submovie->find(array('index'=>($currIndex-1)),null,null,false);
                        $prevRec=$prevRec['Submovie'];
                        //
                        $saveTarg1= array( 'id'=>$currRec['id'], 
'title'=>$currRec['title'],
'url'=>$currRec['url'],
'subsection_id'=>$currRec['subsection_id'],'index'=>$prevRec['index']);
                        //
                        $saveTarg2= array( 
'id'=>$prevRec['id'],'title'=>$prevRec['title'],
'url'=>$prevRec['url'],
'subsection_id'=>$prevRec['subsection_id'],'index'=>$currRec['index']);
                        
if($saveTarg2['id']!=null&&$saveTarg1['index']!=$saveTarg2['index']){
                                if($this->Submovie->del($currRec['id'])){
                                        
if($this->Submovie->del($prevRec['id'])){
                                                
if($this->Submovie->save($saveTarg1)){
                                                        
if($this->Submovie->save($saveTarg2)){
                                                                
$this->redirect('/admin/submovies/listchildrenof/'.$parent_id);
                                                        }
                                                }
                                        }
                                }
                        }else{
                                
$this->redirect('/admin/submovies/listchildrenof/'.$parent_id);
                        }
                }

        }

$id - the id(autoincrement) value of the record to be moved up
$parent_id - in another table is a list of 'parent' categories, we keep
track of who belongs to who with this
DB contains fields id,subsection_id(parent_id),index(for
sorting),title,url

something to note is that data never disapears, and it's only the index
field that is ever 'mis-written'...My questions are: is there an easier
way to do this?(perhaps altering the single field 'index' in the DB
rather than delete/save of two entire rows) Is it worth my trial to
attempt a more direct SQL method? Where am I going wrong here?

any help is much appreciated...thanks in advance


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to