2013-07-27 58 views
0

串連領域我使用的是百夫長Zend框架,我有我的方式出現問題。我有字段num_ordrecode,它們都是主鍵,我的表中有列名爲conca的列,它是兩個字段num_ordrecode的並置。和Zend_Form

我的問題是,在我的方法後,我想測試num_ordrecode concatanation是否已經存在於我的數據庫中;但問題是如何發佈的前幾天到字段值。

這是我的代碼

public function postAction(){ 

    $this->_helper->viewRenderer->setNoRender(TRUE); 
    $user = new Param_Model_DbTable_Verification(); 

    $form= $this->_getForm(); 
    $form->getElement('Num_ordre')->addValidator(new Zend_Validate_Db_NoRecordExists('verifications','Num_ordre')); 
    $form->getElement('Num_ordre')->setRequired(true); 
    $posts = $this->_request->getPost(); 
    if ($this->getRequest()->isPost()) { 

     $formData = $this->getRequest()->getPost(); 
     if ($form->isValid($formData)) { 
      $row=$user->createRow(); 
      $row->code=$this->_getParam('code'); 
      $row->Num_ordre=$this->_getParam('Num_ordre'); 
      $row->Libelle_champ=$this->_getParam('Libelle_champ'); 
      $row->comparaison=$this->_getParam('comparaison'); 
      $row->formule=$this->_getParam('formule'); 
      $row->obligatoire=$this->_getParam('obligatoire'); 
      $row->Req_traduction=$this->_getParam('Req_traduction'); 
      $row->tolerance_erreur=$this->_getParam('tolerance_erreur'); 
      $row->Mess_erreur=$this->_getParam('Mess_erreur'); 
      $row->conca=$this->_getParam('Num_ordre').$this->_getParam('code'); 
      $row->save(); 
      if(isset ($posts['_addanother'])){ 
       $_form = $this->_getForm(); 
       $_form->removeElement('id'); 
       $this->_helper->redirector('new','admin-verification'); 
      } 
      else 
       $this->_helper->redirector(array('controller'=>'Admin-verification')); 

      }else{ 
       parent::postAction(); 
      } 
      }} 

回答

0

你怎麼樣只是檢查它這樣的嗎?

public function postAction(){ 

    $this->_helper->viewRenderer->setNoRender(TRUE); 
    $user = new Param_Model_DbTable_Verification(); 

    $form= $this->_getForm(); 
    $form->getElement('Num_ordre')->addValidator(new Zend_Validate_Db_NoRecordExists('verifications','Num_ordre')); 
    $form->getElement('Num_ordre')->setRequired(true); 
    $posts = $this->_request->getPost(); 
    if ($this->getRequest()->isPost()) { 

     $formData = $this->getRequest()->getPost(); 
     $mdl = new Model_Something(); //Call your model so you can test it 
     //Add a condition here 
     if ($form->isValid($formData) && $mdl->uniqueConcatenated($this->_getParam('num_ordre'), $this->_getParam('code')) { 
      $row=$user->createRow(); 
      /**truncated, keep your existing code here**/ 
     } 
    } 
} 

然後在模型Model_Something

public function uniqueConcatenated($numOrder, $code) { 
    $concatenated = $numOrder.$code; 
    //Check for the existence of a row with the concatenated field values 
    $select = $this->select(); 
    $select->where('concatenatedField = '.$concatenated); 
    $row = $this->fetchRow($select); 
    return $row; 
} 

希望這有助於

+0

是它的好,請,如果價值觀不existe在數據庫中如何設置郵件的好,但如果在數據庫值existe他告訴我泰國人值已經在數據庫中 – zineb

+0

thak你了哥哥,但我想如果值有消息existe在數據庫Telle公司我該值爲; lready在數據庫中選擇其他 – zineb

+0

那你應該看看驗證db_record_exists:http://framework.zend.com/manual/1.12/en/zend.validate.set.html# zend.validate.Db –

0

你可以手動調用的驗證isValid

$formData = $this->getRequest()->getPost(); 
if ($form->isValid($formData)) { 
    $formValues = $form->getValues(); 
    $uniqueValidator = new Zend_Validate_Db_NoRecordExists('verifications','conca'); 
    if ($uniqueValidator->isValid($formValues['Num_ordre'] . $formValues['Num_ordre'])) { 
     // valid 
    } else { 
     // not unique 
    } 
} 

未經測試的代碼

+0

非常感謝你,但NUM-公共秩序不necesary我只是想測試孔卡和孔卡是在表誰擁有NUM-公共秩序和代碼的價值我想探微考如何採取NUM的值列公共秩序和試驗後的代碼和concatanate如果值concatanate在數據庫中存在或不 – zineb