2017-06-09 9 views
0

私はデータベースにデータを挿入しようとしていますが、送信されたフォームは何もしません。zf3とdoctrineを使ってデータベースにデータを挿入する

これは私のサービス・マネージャである:

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

です。ソリューションを見つけることを願っています。

これは私の自動車エンティティ

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

} 

であるが、これは解決策を見つけるのに役立ちます願っています。

+0

エラーログはありますか? 'Entity'、' AutosManager'、 'Controller'のような関連コードを与えることができますか? @DollyAswin。 –

+0

いいえ。エラーログはありません。 – dino

+0

エンティティに問題はないようです。 'AutosManager'に' EntityManager'を渡しましたか? –

答えて

0

私は問題を発見しました。これはフォームで認証していた入力フィルタ要素ではありませんでした。しかし、解決策は別の問題に私をもたらします:

お知らせ:未定義のインデックス:Cでのタイトル:\ xamppの\ htdocsに\ ameyaw \モジュールを\ BusinessGhana \ SRC \サービス\ AutosManager.phpライン38

注意:未定義のインデックス:C:¥xampp¥htdocs¥ameyaw¥module¥BusinessGhana¥src¥Service¥AutosManager.php 39行目

注意:未定義のインデックスはC:\ xampp \ htdocs \ ameyawに掲載されています。 \ module \ BusinessGhana \ src \ Service \ AutosManager.php on line 58

メッセージ:

params [null、null、null、 "2017-06-15 05:0]で 'INSERT INTO auto(タイトル、説明、機能、日付作成)VALUES(?、?、?、?)'を実行中に例外が発生しました。 4" 時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', 
     ], 
    ]); 
} 
} 

ある

nullにすることはできませんこの問題を解決するが、私はまだそれを使用する方法を知らない。

関連する問題