Wednesday, November 2, 2011

CakePHP2.0.1 - Problem with saving HABTM

Hi all! Thanks in advance for replys! :P
I have the following model:

class EpisodeRelease extends AppModel{
    public $cacheQueries = true;
    public $name = 'EpisodeRelease';
    public $primaryKey = 'id';
    public $table = 'episode_releases';
    public $hasMany = array(
        'EpisodeReleaseMirror' => array (
            'className' => 'EpisodeReleaseMirror',
            'foreignKey' => 'episode_release_id',
            'conditions' => array('EpisodeReleaseMirror.approved_by >' => 0),
            'order' => 'EpisodeReleaseMirror.download_count DESC'
        )
    );
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ),
        'EpisodeType' => array(
            'className' => 'EpisodeType',
            'foreignKey' => 'episode_type_id'
        ),
        'Anime' => array(
            'className' => 'Anime',
            'foreignKey' => 'anime_id'
        ),
        'Language' => array(
          'className' => 'Language',
          'foreignKey' => 'language_id'
        )
    );
    public $hasAndBelongsToMany = array(
        'Fansub' => array(
            'className' => 'Fansub',
            'joinTable' => 'Fansub2EpisodeRelease',
            'foreignKey' => 'episode_release_id',
            'associationForeignKey' => 'fansub_id',
            'unique' => false
        ),
        'VideoCodec' => array(
            'className' => 'VideoCodec',
            'joinTable' => 'VideoCodec2EpisodeRelease',
            'foreignKey' => 'episode_release_id',
            'associationForeignKey' => 'video_codec_id',
            'order' => 'VideoCodec.order DESC',
            'unique' => false
        )
    );
}
?>

and the following code in the controller...
    function add(){
        if($this->_canAdd()){
            $this->set('animes', $this->EpisodeRelease->Anime->find('list',array('fields'=>'name')));
            $this->set('languages', $this->EpisodeRelease->Language->find('list',array('fields'=>'name')));
            $this->set('episodeTypes', $this->EpisodeRelease->EpisodeType->find('list',array('fields'=>'name')));
            $this->set('fansubs', $this->EpisodeRelease->Fansub->find('list',array('fields'=>'name','order'=>'Fansub.name ASC')));
            $this->set('videoCodecs', $this->EpisodeRelease->VideoCodec->find('list',array('fields'=>'name','order' => 'VideoCodec.order DESC')));
            
            if($this->request->is('post')){
                $this->request->data['EpisodeRelease']['user_id'] = $this->Auth->User('user_id');
                $this->EpisodeRelease->set($this->request->data);
                if($this->EpisodeRelease->saveAll($this->request->data)){
                    $this->Session->setFlash('Release aggiunta con successo. Sarà visualizzabile non appena un amministratore lo avrà approvato.');
                    //$this->redirect(array('action'=>'index'));
                    pr($this->request->data);
                }else{
                    pr($this->request->data);
                    $this->Session->setFlash('ERRORE! Compila tutti i campi!');
                    $this->set('similar_releases',array());
                }
            }else{
                $this->set('similar_releases',array());
            }
        }else{
            $this->Session->setFlash('Non hai i permessi necessari per eseguire questa azione.');
            $this->redirect(array('action'=>'index'));
        }
    }
When i press Submit the array sent to saveAll is the following: 
Array (     [EpisodeRelease] => Array         (             [language_id] => 1             [anime_id] => 1             [episode_type_id] => 1             [number] => 2             [name] => episode             [user_id] => 2         )      [Fansub] => Array         (             [Fansub] => Array                 (                     [0] => 1                 )          )      [VideoCodec] => Array         (             [VideoCodec] => Array                 (                     [0] => 8                 )          )  )
But it seems that HABTM relations are'nt saved.... why?
Thanks again! ^^
Overall rating
 

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: