I can't insert data to database #5985

Closed
opened 2026-01-22 15:24:03 +01:00 by admin · 11 comments
Owner

Originally created by @ghost on GitHub (Jun 11, 2018).

Originally assigned to: @Ocramius on GitHub.

version 2.6.1

first time I'm using doctrine and there is a problem about insterting data. I created entity class then created table with console command. But it comes to insert something, I can't insert anything..

it says;

t's a requirement to specify a Metadata Driver and pass it to Doctrine\ORM\Configuration::setMetadataDriverImpl().

I googled it and all solutions about symfony configuration but I don't use symfony..

public function store(RequestInterface $request) 
    { 
        $post = new Post(); 
        $post->setTitle($request->get("title")); 
        $post->setContent($request->get("content")); 

        $db = new Bootstrap(); 
        $db->conn()->persist($post); 
        $db->conn()->flush(); 

        return redirect("admin"); 
    }  

and bootstrap class

<?php 

namespace Core\Database; 

use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Tools\Setup; 

class Bootstrap 
{ 
    private $conn = [ 
        'driver' => 'pdo_mysql', 
        'host' => 'localhost', 
        'dbname' => 'cms', 
        'user' => 'root', 
        'password' => '123456' 
    ]; 

    public function conn() 
    { 
        return EntityManager::create($this->conn, $this->config()); 
    } 

    private function config() 
    { 
        return Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src"), true, null, null, true); 
    } 
}
Originally created by @ghost on GitHub (Jun 11, 2018). Originally assigned to: @Ocramius on GitHub. version 2.6.1 first time I'm using doctrine and there is a problem about insterting data. I created entity class then created table with console command. But it comes to insert something, I can't insert anything.. it says; > t's a requirement to specify a Metadata Driver and pass it to Doctrine\ORM\Configuration::setMetadataDriverImpl(). I googled it and all solutions about symfony configuration but I don't use symfony.. ```php public function store(RequestInterface $request) { $post = new Post(); $post->setTitle($request->get("title")); $post->setContent($request->get("content")); $db = new Bootstrap(); $db->conn()->persist($post); $db->conn()->flush(); return redirect("admin"); } ``` and bootstrap class ```php <?php namespace Core\Database; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; class Bootstrap { private $conn = [ 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'cms', 'user' => 'root', 'password' => '123456' ]; public function conn() { return EntityManager::create($this->conn, $this->config()); } private function config() { return Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src"), true, null, null, true); } } ```
admin added the Question label 2026-01-22 15:24:03 +01:00
admin closed this issue 2026-01-22 15:24:04 +01:00
Author
Owner

@Ocramius commented on GitHub (Jun 11, 2018):

What is the full stack trace for that error?

@Ocramius commented on GitHub (Jun 11, 2018): What is the full stack trace for that error?
Author
Owner

@ghost commented on GitHub (Jun 13, 2018):

@Ocramius nothing actually.. I tried that and I got nothing..

try {
            $post = new Post;
            $post->setTitle($request->get("title"));
            $post->setContent($request->get("content"));
            $orm = new Bootstrap;
            $orm->conn()->persist($post);
            $orm->conn()->flush();
        } catch (\Exception $e) {
            echo $e->getTrace();
        }
@ghost commented on GitHub (Jun 13, 2018): @Ocramius nothing actually.. I tried that and I got nothing.. ```php try { $post = new Post; $post->setTitle($request->get("title")); $post->setContent($request->get("content")); $orm = new Bootstrap; $orm->conn()->persist($post); $orm->conn()->flush(); } catch (\Exception $e) { echo $e->getTrace(); } ```
Author
Owner

@Ocramius commented on GitHub (Jun 13, 2018):

You don't need to add any code - just display the full exception as it is rendered when having ini_set('display_errors', '1');

@Ocramius commented on GitHub (Jun 13, 2018): You don't need to add any code - just display the full exception as it is rendered when having `ini_set('display_errors', '1');`
Author
Owner

@ghost commented on GitHub (Jun 13, 2018):

@Ocramius I'm working with localhost, I'm already displaying errors but there is no error. I check with different methods to get any error like php-doctrine error, or pdo error but I got nothing.

@ghost commented on GitHub (Jun 13, 2018): @Ocramius I'm working with localhost, I'm already displaying errors but there is no error. I check with different methods to get any error like php-doctrine error, or pdo error but I got nothing.
Author
Owner

@Ocramius commented on GitHub (Jun 14, 2018):

I'm already displaying errors but there is no error.

Where did you get this one from?

t's a requirement to specify a Metadata Driver and pass it to Doctrine\ORM\Configuration::setMetadataDriverImpl().

@Ocramius commented on GitHub (Jun 14, 2018): > I'm already displaying errors but there is no error. Where did you get this one from? > > t's a requirement to specify a Metadata Driver and pass it to Doctrine\ORM\Configuration::setMetadataDriverImpl().
Author
Owner

@ghost commented on GitHub (Jun 14, 2018):

@Ocramius I passed metadata driver, and still it doesn't persist without any error

@ghost commented on GitHub (Jun 14, 2018): @Ocramius I passed metadata driver, and still it doesn't persist without any error
Author
Owner

@Ocramius commented on GitHub (Jun 14, 2018):

@hsntngr55 hmm, your bootstrapper is creating a new EntityManager every time, so it's obviously empty when attempting to flush it.

Closing as resolved here :-)

@Ocramius commented on GitHub (Jun 14, 2018): @hsntngr55 hmm, your bootstrapper is creating a new `EntityManager` every time, so it's obviously empty when attempting to flush it. Closing as `resolved` here :-)
Author
Owner

@ghost commented on GitHub (Jun 14, 2018):

so, if I use as static, it will work ?

@ghost commented on GitHub (Jun 14, 2018): so, if I use as static, it will work ?
Author
Owner

@Ocramius commented on GitHub (Jun 14, 2018):

Please don't do that: use dependency injection instead :-)

<?php 

namespace Core\Database; 

use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\Tools\Setup; 

class Bootstrap 
{ 
    private $conn = [ 
        'driver' => 'pdo_mysql', 
        'host' => 'localhost', 
        'dbname' => 'cms', 
        'user' => 'root', 
        'password' => '123456' 
    ]; 

    public function myAction() : MyAction
    {
        // explicitly passing the entity manager to `MyAction` - this is dependency injection:
        return new MyAction($this->conn());
    }

    private function conn() 
    { 
        return EntityManager::create($this->conn, $this->config()); 
    } 

    private function config() 
    { 
        return Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src"), true, null, null, true); 
    } 
}
final class MyAction
{
    private $em;
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    public function store(RequestInterface $request) 
    { 
        $post = new Post(); 
        $post->setTitle($request->get("title")); 
        $post->setContent($request->get("content")); 

        $this->em->persist($post); 
        $this->em->flush(); 

        return redirect("admin"); 
    }  
@Ocramius commented on GitHub (Jun 14, 2018): Please don't do that: use dependency injection instead :-) ```php <?php namespace Core\Database; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; class Bootstrap { private $conn = [ 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'cms', 'user' => 'root', 'password' => '123456' ]; public function myAction() : MyAction { // explicitly passing the entity manager to `MyAction` - this is dependency injection: return new MyAction($this->conn()); } private function conn() { return EntityManager::create($this->conn, $this->config()); } private function config() { return Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src"), true, null, null, true); } } ``` ```php final class MyAction { private $em; public function __construct(EntityManager $em) { $this->em = $em; } public function store(RequestInterface $request) { $post = new Post(); $post->setTitle($request->get("title")); $post->setContent($request->get("content")); $this->em->persist($post); $this->em->flush(); return redirect("admin"); } ```
Author
Owner

@ghost commented on GitHub (Jun 14, 2018):

@Ocramius well, it works like a charm, thank you a lot :) but I didin't get it logic behind this. why class is empty ? I'm sending a $post entity as a parameter ?

@ghost commented on GitHub (Jun 14, 2018): @Ocramius well, it works like a charm, thank you a lot :) but I didin't get it logic behind this. why class is empty ? I'm sending a $post entity as a parameter ?
Author
Owner

@Ocramius commented on GitHub (Jun 14, 2018):

@hsntngr55 in your example above:

            $orm = new Bootstrap;
            $orm->conn()->persist($post);
            $orm->conn()->flush();

$orm->conn() leads to a new EntityManager being produced at each call, which means that the third line is calling ->flush() on an empty EntityManager

@Ocramius commented on GitHub (Jun 14, 2018): @hsntngr55 in your example above: ```php $orm = new Bootstrap; $orm->conn()->persist($post); $orm->conn()->flush(); ``` `$orm->conn()` leads to a new `EntityManager` being produced at each call, which means that the third line is calling `->flush()` on an empty `EntityManager`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5985