使用zf3和doctrine将数据插入数据库

时间:2017-06-09 19:48:28

标签: doctrine-orm zend-framework3

我正在尝试将数据插入数据库,但提交的表单不执行任何操作。

这是我的服务经理:

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']);
    $currentDate = date('Y-m-d H:i:s');
    $autos->setDateCreated($currentDate);        

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

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

这是我的控制器addAction

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
    ]);
}

我可以从数据库检索数据到索引页面,但我无法添加。希望找到解决方案。

这是我的Autos Entity

namespace Retrieve\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Entity(repositoryClass="\Retrieve\Repository\AutosRepository")
* @ORM\Table(name="auto")
*/
class Autos 
{

/**
 * @ORM\Id
 * @ORM\Column(name="id")
 * @ORM\GeneratedValue
 */
protected $id;

/** 
 * @ORM\Column(name="title")  
 */
protected $title;

/** 
 * @ORM\Column(name="description")  
 */
protected $description;

/** 
 * @ORM\Column(name="featured")  
 */
protected $featured;

/**
 * @ORM\Column(name="date_created")  
 */
protected $dateCreated;


/**
 * Returns ID of this post.
 * @return integer
 */
public function getId() 
{
    return $this->id;
}

/**
 * Sets ID of this post.
 * @param int $id
 */
public function setId($id) 
{
    $this->id = $id;
}

/**
 * Returns title.
 * @return string
 */
public function getTitle() 
{
    return $this->title;
}

/**
 * Sets title.
 * @param string $title
 */
public function setTitle($title) 
{
    $this->title = $title;
}

/**
 * Returns featured.
 * @return integer
 */
public function getFeatured() 
{
    return $this->featured;
}

/**
 * Sets featured.
 * @param integer $featured
 */
public function setFeatured($featured) 
{
    $this->featured = $featured;
}   

/**
 * Returns post description.
 */
public function getDescription() 
{
   return $this->description; 
}

/**
 * Sets post description.
 * @param type $description
 */
public function setDescription($description) 
{
    $this->description = $description;
}

/**
 * Returns the date when this post was created.
 * @return string
 */
public function getDateCreated() 
{
    return $this->dateCreated;
}

/**
 * Sets the date when this post was created.
 * @param string $dateCreated
 */
public function setDateCreated($dateCreated) 
{
    $this->dateCreated = $dateCreated;
}

}

希望这有助于找到解决方案。

1 个答案:

答案 0 :(得分:0)

我发现了问题:它是一个我没有使用的输入过滤元素,它在表单中进行身份验证。但解决方案只会让我遇到另一个问题:

  

注意:未定义的索引:第38行的C:\ xampp \ htdocs \ ameyaw \ module \ BusinessGhana \ src \ Service \ AutosManager.php中的标题

     

注意:未定义的索引:第39行的C:\ xampp \ htdocs \ ameyaw \ module \ BusinessGhana \ src \ Service \ AutosManager.php中的描述

     

注意:未定义的索引:在第58行的C:\ xampp \ htdocs \ ameyaw \ module \ BusinessGhana \ src \ Service \ AutosManager.php中有特色

     

消息:

     

使用params执行'INSERT INTO auto(title,description,featured,date_created)VALUES(?,?,?,?)'时发生异常[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',
        ],
    ]);
}
}

我知道补水可以解决这个问题,但我还不知道如何使用它。