注意:未定义的索引

时间:2017-06-16 03:59:42

标签: doctrine zend-framework3

我提交表单时收到此错误消息。

  

注意:未定义的索引:标题为   C:\ XAMPP \ htdocs中\ ameyaw \模块\ BusinessGhana \ SRC \服务\ AutosManager.php   第38行

     

注意:未定义索引:描述中   C:\ XAMPP \ htdocs中\ ameyaw \模块\ BusinessGhana \ SRC \服务\ AutosManager.php   第39行

     

注意:未定义的索引:特色于   C:\ XAMPP \ htdocs中\ ameyaw \模块\ BusinessGhana \ SRC \服务\ AutosManager.php   在第58行

     

消息:

     

执行' INSERT INTO auto时发生异常(标题,   描述,特色,date_created)价值观(?,?,?,?)'与params   [null,null,null," 2017-06-15 05: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')); ?>

1 个答案:

答案 0 :(得分:0)

在您的函数addNewAutos($data)中:您的$ data变量没有titledescriptionfeatured字段。因此,php将这些视为null,并且您的$this->entityManager->flush()将尝试在数据库中写入空值,但您有一个约束,至少title不能为空。

因此,请检查您的$data数组是否包含titledescriptionfeatured字段,然后才能启动Auto对象...