2009-06-23 7 views
0

J'ai une case à cocher nommée obligatoire, où si la case est cochée sa valeur est 1, sinon c'est 0. La valeur est correctement passée à partir du fichier de vue au contrôleur. Le fichier modèle obtient la valeur du contrôleur, mais la valeur n'est pas enregistrée.Problème de stockage de la valeur dans la base de données (CakePHP)

J'ai répercuté la valeur requise dans le modèle, pour vérifier si la valeur est reçue. La valeur est renvoyée en 1. Mais dans la base de données, elle n'est pas mise à jour.

Ceci est mon code:

function updateFieldEntries($data) 
{ 
    $this->data['Attribute']['id']=$this->Attribute->find('all', array(
            'fields' => array('Attribute.id'), 
            'order' => 'Attribute.id DESC')); 

    $this->data['Attribute']['id']=$this->data['Attribute']['id'][0]['Attribute']['id']; 

    $this->data['Attribute']['form_id'] = $this->find('all', array(
            'fields' => array('Form.id'), 
            'order' => 'Form.id DESC')); 
    $this->data['Attribute']['form_id']=$this->data['Attribute']['form_id'][0]['Form']['id']; 

    $this->data['Attribute']['instructions']=$data['Attribute']['instructions']; 

    $this->data['Attribute']['required']=$data['Attribute']['required']; 
    echo " required model ".$this->data['Attribute']['required']; 


    $this->data['Attribute']['sequence_no'] =$this->Attribute->find('all', array(
           'conditions' => array('Attribute.form_id' =>$this->data['Attribute']['form_id']), 
            'fields' => array('Attribute.sequence_no'), 
            'order' => 'Attribute.sequence_no DESC')); 
    $this->data['Attribute']['sequence_no']=$this->data['Attribute']['sequence_no'][0]['Attribute']['sequence_no']; 

    if($data['Attribute']['name']== ''){ 
     $this->data['Attribute']['label']=$this->Attribute->find('all', array(
           'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']), 
            'fields' => array('Attribute.label')                   )); 
     $this->data['Attribute']['label']=$this->data['Attribute']['label'][0]['Attribute']['label']; 
    } 
    else{ 
     $this->data['Attribute']['label']= $data['Attribute']['name']; 
    } 

    if($data['Attribute']['size']== ''){ 
     $this->data['Attribute']['size']=$this->Attribute->find('all', array(
           'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']), 
            'fields' => array('Attribute.size')                  )); 
     $this->data['Attribute']['size']=$this->data['Attribute']['size'][0]['Attribute']['size']; 
    } 
    else{ 
     $this->data['Attribute']['size']= $data['Attribute']['size']; 
    } 


    if($data['Attribute']['instructions']== ''){ 
      $this->data['Attribute']['instructions']=$this->Attribute->find('all', array(
           'conditions' => array('Attribute.id' =>$this->data['Attribute']['id']), 
            'fields' => array('Attribute.instructions')                  )); 
    $this->data['Attribute']['instructions']=$this->data['Attribute']['instructions'][0]['Attribute']['instructions']; 

      } 

    $this->Attribute->save($this->data); 

} 

EDIT

J'ai également vérifié si est en cours d'enregistrement la seule valeur requise en utilisant l'option saveField.

$this->Attribute->saveField('required',$this->data['Attribute']['required']); 

La valeur 1 a obtenu stockée dans une ligne distincte de la table pour cet attribut particulier. Alors, quel est le problème, pourquoi n'est-il pas enregistré avec les autres valeurs?

EDIT

Si j'enregistre une valeur entière directement, comme

 $this->data['Attribute']['required']='7'; 

il est stocké dans la base de données. Comment?? Quel est le problème alors ??

+0

L'une des autres valeurs est-elle enregistrée? Ou est-ce juste "requis"? –

+0

Toutes les autres valeurs sont enregistrées correctement, comme le nom, la taille sont toutes mises à jour correctement. Seule la valeur requise n'est pas sauvegardée.J'ai ajouté cette propriété requise récemment.Auparavant, j'avais seulement le nom, le type de taille, les instructions et la séquence non – Angeline

+0

N'était-ce pas résolu avec votre question précédente? http://stackoverflow.com/questions/1030639/problem-in-storing-value-in-database –

Répondre

1

Je commencerai par refactoring:

function updateFieldEntries($data) 
{ 
    $tempAttr = $this->Attribute->find 
     (
      'first', 
      array 
      (
       'order' => 'Attribute.id DESC', 
       'fields' => array('Attribute.id') 
      ) 
     ); 

    $this->data['Attribute']['id'] = $tempAttr['Attribute']['id']; 

    $tempAttr = $this->find 
     (
      'first', 
      array 
      (
       'order' => 'Form.id DESC', 
       'fields' => array('Form.id') 
      ) 
     ); 

    $this->data['Attribute']['form_id'] = $tempAttr['Form']['id']; 

    $this->data['Attribute']['instructions'] = $data['Attribute']['instructions']; 
    $this->data['Attribute']['required'] = $data['Attribute']['required']; 

    $tempAttr = $this->Attribute->find 
     (
      'first', 
      array 
      (
       'order' => 'Attribute.sequence_no DESC', 
       'fields' => array('Attribute.sequence_no'), 
       'conditions' => array('Attribute.form_id' => $this->data['Attribute']['form_id']) 
      ) 
     ); 

    $this->data['Attribute']['sequence_no'] = $tempAttr['Attribute']['sequence_no']; 

    $tempAttr = $this->Attribute->find 
     (
      'first', 
      array 
      (
       'conditions' => array('Attribute.id' => $this->data['Attribute']['id']), 
       'fields' => array('Attribute.label', 'Attribute.size', 'Attribute.instructions') 
      ) 
     ); 

    if (empty($data['Attribute']['name'])) 
    { 
     $this->data['Attribute']['label'] = $tempAttr['Attribute']['label']; 
    } 
    else 
    { 
     $this->data['Attribute']['label'] = $data['Attribute']['name']; 
    } 

    if (empty($data['Attribute']['size'])) 
    { 
     $this->data['Attribute']['size'] = $tempAttr['Attribute']['size']; 
    } 
    else 
    { 
     $this->data['Attribute']['size'] = $data['Attribute']['size']; 
    } 

    if (empty($data['Attribute']['instructions'])) 
    { 
     $this->data['Attribute']['instructions'] = $tempAttr['Attribute']['instructions']; 
    } 
    else 
    { 
     $this->data['Attribute']['instructions'] = $data['Attribute']['instructions']; 
    } 

    $this->Attribute->save($this->data); 
} 

Alors je vais essayer la recommandation de Travis LELEU de débogage réellement votre code. Je commencerais par vérifier la valeur de $this->Attribute->id avant de sauvegarder.

Questions connexes