在Cakephp中插入数据的问题

时间:2016-11-18 19:31:55

标签: cakephp

在执行add方法后的数据库中我只检索外键tarid_id,其他字段为空这里是我的Adherent Model的代码:谢谢你的回答

         <?php
           App::uses('AppModel', 'Model');
              /**
             * Adherant Model
           *
        * @property User $User
          * @property Tarif $Tarif
  */
   class Adherant extends AppModel {

       /**
           * Validation rules
  *
        * @var array
           */
         public $validate = array(
         'nom' => array(
          'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'prenom' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update'  operations
        ),
    ),
    'cartepaiment' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'cin' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'tarif_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

       //The Associations below have been created with all possible keys,   those   that are not needed can be removed

 /* *
        * belongsTo associations
  *
   * @var array
    */
    public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Tarif' => array(
        'className' => 'Tarif',
        'foreignKey' => 'tarif_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

}

我的add方法:

    public function add() {



    $this->loadModel('User');
    $this->loadModel('Tarif');
     //$tarif_id=this->Session->read('tarif_id');

    $this->Session->write('Person.addAdh',"");
    if ($this->request->is('post')) {


$this->Adherant->create();
        $login=$this->request->data['login'];
        $pass=$this->request->data['motpass'];
          $count=$this->User-          >find('count',array('conditions'=>array('User.login'=>$login)));
if($count==1){
$this->redirect(array('action' => 'add'));
$this->Session->write('Person.addAdh',"adherant existe déjà (user)");

 }else{
         $count1=$this->Adherant-   >find('count',array('conditions'=>array('Adherant.cin'=>$this->request-    >data['cin'])));
         if($count1==0){
     $this->User->save( 
            array(
                'login' => $login,
                'motpass' => $pass,

                )
            );
$first=$this->User- >find('first',array('conditions'=>array('User.login'=>$login)));
         $user=$first['User']['id'];
         if ($this->Adherant->saveAll($this->request->data, array('deep' =>   true)))
 {


       $this->Session->setFlash( __('The ADHERANT has been saved'),  'success');
              return $this->redirect(array('action' => 'index'));
          }
        }else{
        $this->redirect(array('action' => 'add'));
    $this->Session->write('Person.addAdh',"adherant existe déjà(cin)");
    }

   }}



        $users = $this->Adherant->User->find('list');

      $tarifs = $this->Adherant->Tarif-       >find('list',array('fields'=>array('Tarif.id','Tarif.designation')));
        $this->set(compact('users','tarifs'));





}

0 个答案:

没有答案