这是我第一次使用 OO PHP、MVC 和 Doctrine,所以我可能做错了什么!我正在尝试使用 Doctrine 将对象保存到数据库中,问题是该对象完全为空
这是 PHP 返回给我的错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'NM_TIPO_SUPLEMENTO' cannot be null' in C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php:138 Stack trace: #0 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php(138): PDOStatement->execute(NULL) #1 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php(278): Doctrine\DBAL\Statement->execute() #2 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(929): Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts() #3 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(318): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata)) #4 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\E in C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php on line 47
我知道我正确地进行了映射,因为如果我更改模型上列的名称,它会说我的列不存在
这是我的模型:
<?php
/**
* @Entity @Table(name="tb_tipo_suplemento")
**/
class TipoSuplemento {
/** @Id @Column(type="integer", name="ID_TIPO_SUPLEMENTO") @GeneratedValue(strategy="IDENTITY") **/
protected $idTipo;
/** @Column(type="string", name="NM_TIPO_SUPLEMENTO", nullable=false) **/
protected $nmTipo;
public function setIdTipo($idTipo) {
$this->idTipo = $idTipo;
}
public function getIdTipo() {
return $this->idTipo;
}
public function setNmTipo($NmTipo) {
$this->NmTipo = $NmTipo;
}
public function getNmTipo() {
return $this->NmTipo;
}
}
?>
我的控制器:
<?php
class ctrlTipoSuplemento {
public function salvarTipoSuplemento() {
require("../model/TipoSuplemento.php");
require_once "../bootstrap.php";
//criar um objeto novo e o preencher
$tpSuplemento = new TipoSuplemento();
$tpSuplemento->setNmTipo($_POST['nmTipoSuplemento']);
$entityManager->persist($tpSuplemento);
$entityManager->flush();
}
}
?>
我的观点.php
<?php
require("../util/header.php");
require("../util/footer.php");
require("../controller/ctrlTipoSuplemento.php");
getHeader("Manter Tipo de Suplemento", "Tipo de Suplemento");
montarFormulario();
?>
<form action="" method="post">
<input type="text" name="nmTipoSuplemento" required="required" placeholder="Tipo do Suplemento"><br>
<button type="submit">Salvar</button>
</form>
<?php
/*
* Salvar os dados do formulário acima
*/
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ctrl = new ctrlTipoSuplemento();
$ctrl->salvarTipoSuplemento();
}
terminarFomulario();
?>
<?php getFooter();?>
和 bootstrap.php
<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/model"), $isDevMode);
// database configuration parameters
$conn = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'suplementos',
);
$entityManager = EntityManager::create($conn, $config);
我的桌子:
CREATE TABLE IF NOT EXISTS `tb_tipo_suplemento` (
`ID_TIPO_SUPLEMENTO` int(10) NOT NULL AUTO_INCREMENT,
`NM_TIPO_SUPLEMENTO` varchar(100) NOT NULL,
PRIMARY KEY (`ID_TIPO_SUPLEMENTO`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
太感谢了!
编辑 1:添加 view.php
编辑 2: getter 和 setter 有什么问题吗?如果我替换private
为public
, 并像这样设置变量值,$tpSuplemento->nmTipo = $_POST['nmTipoSuplemento'];
它可以工作