2017-06-16 183 views
0

我在提交表單時出現此錯誤消息。注意:未定義索引

說明:未定義指數:標題中 C:\ XAMPP \ htdocs中\ ameyaw \模塊\ BusinessGhana \ SRC \服務\ AutosManager.php 在線38

說明:未定義指數:描述在 C:\ XAMPP \ htdocs中\ ameyaw \模塊\ BusinessGhana \ SRC \服務\ AutosManager.php 第39行上

說明:未定義指數:特色在 C:\ XAMPP \ htdocs中\ ameyaw \模塊\ BusinessGhana \ SRC \ Service \ AutosManager.php on line 58

消息:

發生異常而執行 'INSERT INTO汽車(標題, 描述,特色,DATE_CREATED)VALUES(,,,????)' 使用參數 [NULL,NULL,NULL, 「2017年6月15日5時04分44秒」]:

SQLSTATE [23000]:完整性約束違規:1048列 '標題' 不能爲空

這是我的形式和字段集

use Zend\Form\Fieldset; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Persistence\ObjectManagerAwareInterface; 
use BusinessGhana\Entity\Autos; 

class AddFieldset extends Fieldset 
{ 

protected $objectManager; 

public function init() 
{ 

$this->add([   
    'type' => 'text', 
    'name' => 'title', 
    'attributes' => [ 
     'id' => 'autoTitle' 
    ], 
    'options' => [ 
     'label' => 'Title', 
     'display_empty_item' => true, 
     'empty_item_label' => 'Maximum of 60 characters', 
    ], 
]); 

$this->add([    
    'type' => 'textarea', 
    'name' => 'description', 
    'attributes' => [ 
     'id' => 'autoDescription' 
    ], 
    'options' => [ 
     'label' => 'Description', 
     'display_empty_item' => true, 
     'empty_item_label' => 'description', 
    ], 
]);    
$this->add([   
    'type' => 'radio', 
    'name' => 'featured', 
    'attributes' => [ 
     'id' => 'autoFeatured' 
    ], 
    'options' => array(
     'label' => 'Featured', 
     'value_options' => array(
      array('value' => '0', 
       'label' => 'No', 
       'selected' => true, 
       'label_attributes' => array(
        'class' => 'col-sm-2 btn btn-default', 
       ), 
      ), 
      array(
       'value' => '1', 
       'label' => 'Yes', 
       'label_attributes' => array(
        'class' => 'col-sm-2 btn btn-danger', 
       ), 
      ), 
     ), 
     'column-size' => 'sm-12', 
     'label_attributes' => array(
      'class' => 'col-sm-2', 
     ), 
    ), 
]); 

} 

}

use Zend\Form\Form; 
//use Zend\InputFilter\InputFilter; 


class AddForm extends Form 
{ 
public function init() 
{ 
$this->add([ 
    'name' => 'dependentForm', 
    'type' => AddFieldset::class, 

]); 

$this->add([ 
    'type' => 'submit', 
    'name' => 'submit', 
    'attributes' => [ 
     'value' => 'Submit', 
    ], 
]); 
} 
} 

這是我的控制器操作

public function addAction() 
{ 
// Create the form. 
$form = new PostForm(); 

if ($this->getRequest()->isPost()) { 

    // Get POST data. 
    $data = $this->params()->fromPost(); 

    // Fill form with data. 
    $form->setData($data); 
    if ($form->isValid()) { 

     // Get validated form data. 
     $data = $form->getData(); 
     $this->AutosManager->addNewAutos($data); 
     return $this->redirect()->toRoute('retrieve'); 
    } 
} 
return new ViewModel([ 
    'form' => $form 
]); 
} 

我知道水化可以解決這個問題,但我不知道如何使用它。 感謝您的任何幫助。

這是我autosManager

class AutosManager 
{ 
/** 
* Entity manager. 
* @var Doctrine\ORM\EntityManager; 
*/ 
private $entityManager; 

/** 
* Constructor. 
*/ 
public function __construct($entityManager) 
{ 
    $this->entityManager = $entityManager; 
} 

public function addNewAutos($data) 
{ 
    $autos = new Autos(); 
    $autos->setTitle($data['title']); 
    $autos->setDescription($data['description']); 
    $autos->setFeatured($data['featured']); 
    $currentDate = date('Y-m-d H:i:s'); 
    $autos->setDateCreated($currentDate);   

    $this->entityManager->persist($autos); 

    $this->entityManager->flush(); 
    } 
} 

我可以從數據庫中檢索數據。

這是我使我的形式:

  $form = $this->form; 
     $fieldset = $form->get('dependentForm'); 

     $title = $fieldset->get('title'); 
     $title->setAttribute('class', 'form-control'); 
     $title->setAttribute('placeholder', 'Maximum of 60 characters'); 
     $title->setAttribute('id', 'autoTitle'); 

     $description = $fieldset->get('description'); 
     $description->setAttribute('class', 'form-control'); 
     $description->setAttribute('placeholder', 'Description here...'); 
     $description->setAttribute('id', 'autoDescription'); 

     $featured = $fieldset->get('featured'); 
     $featured->setAttribute('id', 'autoRadio'); 

    $form->get('submit')->setAttributes(['class'=>'btn btn-primary']); 
     $form->prepare(); 
     echo $this->form()->openTag($form); 
     ?> 

     <fieldset> 

     <div class="form-group"> 
      <?= $this->formLabel($title) ?> 
      <?= $this->formElement($title) ?> 
<?= $this->formElementErrors()->render($title,['class'=>'help-block'])?> 
     </div> 

     <div class="form-group"> 
      <?= $this->formLabel($description) ?> 
      <?= $this->formElement($description) ?> 
<?=$this->formElementErrors()->render($description, 
    ['class'=>'help-block'])?> 
     </div> 

     <div class="form-group"> 
      <?= $this->formLabel($featured) ?> 
      <?= $this->formElement($featured) ?> 
    <?= $this->formElementErrors()->render($featured, 
    ['class' =>'help-block']) ?> 
     </div> 
     </div></div><div class="row"> 
      <div class="col-md-4"> 

     </fieldset> 
     <?= $this->formElement($form->get('submit')); ?> 
+0

我沒有看到任何輸入過濾器。顯然,如果你不添加特定的驗證,你的表單將是有效的。看看如何做到這一點教程:https://docs.zendframework.com/tutorials/getting-started/forms-and-actions/ – xtreamwayz

+0

你的錯誤是在你的文件'AutosManager.php':顯示它。 –

+0

@AlFoиceѫ我添加了我的自動管理器 – dino

回答

0

在你的函數addNewAutos($data):你的$ data變量沒有titledescriptionfeatured領域。所以PHP認爲這些爲null,並且您的$this->entityManager->flush()將嘗試在數據庫中寫入空值,但是您至少有一個約束,即title不能爲空。

所以檢查$data數組包含titledescriptionfeatured領域之前,啓動對Auto對象...