Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Doctrine setting null to variables
    primarykey
    data
    text
    <p>This is my first time working with OO PHP, MVC and Doctrine, so I'm probably doing something really wrong!! I'm trying to save an object into the database using Doctrine, the problem is that the object is completaly null</p> <p>this is the error that PHP returns me:</p> <pre><code>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-&gt;execute(NULL) #1 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php(278): Doctrine\DBAL\Statement-&gt;execute() #2 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(929): Doctrine\ORM\Persisters\BasicEntityPersister-&gt;executeInserts() #3 C:\Users\Alexandre\apache\workspace\projetoPeso\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(318): Doctrine\ORM\UnitOfWork-&gt;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 </code></pre> <p>I know that I did the mapping correctly, because if I change the name of the column on my model it says my column doesn't exists</p> <p>this is my model:</p> <pre><code>&lt;?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-&gt;idTipo = $idTipo; } public function getIdTipo() { return $this-&gt;idTipo; } public function setNmTipo($NmTipo) { $this-&gt;NmTipo = $NmTipo; } public function getNmTipo() { return $this-&gt;NmTipo; } } ?&gt; </code></pre> <p>my controller:</p> <pre><code>&lt;?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-&gt;setNmTipo($_POST['nmTipoSuplemento']); $entityManager-&gt;persist($tpSuplemento); $entityManager-&gt;flush(); } } ?&gt; </code></pre> <p>my view.php</p> <pre><code>&lt;?php require("../util/header.php"); require("../util/footer.php"); require("../controller/ctrlTipoSuplemento.php"); getHeader("Manter Tipo de Suplemento", "Tipo de Suplemento"); montarFormulario(); ?&gt; &lt;form action="" method="post"&gt; &lt;input type="text" name="nmTipoSuplemento" required="required" placeholder="Tipo do Suplemento"&gt;&lt;br&gt; &lt;button type="submit"&gt;Salvar&lt;/button&gt; &lt;/form&gt; &lt;?php /* * Salvar os dados do formulário acima */ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $ctrl = new ctrlTipoSuplemento(); $ctrl-&gt;salvarTipoSuplemento(); } terminarFomulario(); ?&gt; &lt;?php getFooter();?&gt; </code></pre> <p>and bootstrap.php</p> <pre><code>&lt;?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' =&gt; 'pdo_mysql', 'user' =&gt; 'root', 'password' =&gt; '', 'dbname' =&gt; 'suplementos', ); $entityManager = EntityManager::create($conn, $config); </code></pre> <p>my table:</p> <pre><code>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; </code></pre> <p>Thank you so much!</p> <p><strong>EDIT 1:</strong> Added view.php<br> <strong>EDIT 2:</strong> Is there something wrong with getters and setters? If I replace <code>private</code> by <code>public</code>, and set the variable value like this <code>$tpSuplemento-&gt;nmTipo = $_POST['nmTipoSuplemento'];</code> it works</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload