I have tried to follow the "steps" detailed on the website but i couldn't make it work:
http://docs.doctrine-project.org/en/latest/reference/configuration.html
I have downloaded the doctrine package.
Then i have installed it using PEAR.
Now, i create a file called "test.php" with the following content:
//loding Setup from the doctrine package...
require 'vendor/Doctrine/ORM/Tools/Setup.php';
Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entities-or-mapping-files");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'admin',
'password' => 'mypass',
'dbname' => 'test',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);
// or if you prefer yaml or xml
//$config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);
What else should i do to make it work?
I can not currently do something like:
print_r($em->getRepository("User"));
It shows me many warnings, for example:
Class User does not exist and could not be loaded in
Warning: array_reverse() expects parameter 1 to be array, boolean given in
Warning: Invalid argument supplied for foreach() in ..../Doctrine/ORM/Mapping/ClassMetadataFactory.php
etc. The first one i guess is the key.
Something is missing but i dont know what.
Thanks.
Related
I am following the tutorial for Doctrine: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html
I changed the bootstrap file to include my database to:
<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = false;
$paths = array(__DIR__."/src");
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
// database configuration parameters
$conn = array(
'host' => '*********',
'port' => '3306',
'user' => '********',
'password' => '****',
'dbname' => 'bugs',
'charset' => 'UTF8',
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
);
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
Then, I did the rest and create the tables, etc using the following commnad:
vendor/bin/doctrine orm:schema-tool:create
Later, I logged to my 'myphpadmin' and the database was not there.
Am I missing any step?
Thank you
That is because you are using the driver pdo_sqlite. It is creating an SQLite database and not a MySQL database. It should have created the file db.sqlite.
Change it to pdo_mysql to get the database to show up in phpmyadmin.
i've got my bootstrap
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
// Log Filename
define ('LOG_FILENAME', 'clicktocall-'.date("Ymd").'log');
$loader = require "vendor/autoload.php";
// Doctrine parser
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array("./src/acme"), $isDevMode);
// Database configuration parameters
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'xxx',
'password' => 'yyy',
'dbname' => 'zzz',
);
// Obtaining the entity manager
$em = EntityManager::create($dbParams, $config);
// Setting Logger Monolog
$Logger = new \Monolog\Logger('CTLogger');
$Logger->pushHandler(new \Monolog\Handler\StreamHandler('./log/'.LOG_FILENAME, \Monolog\Logger::INFO));
When i'm trying to execute
vendor\bin\doctrine orm:schema-tool:update --force
I obtain this message
Fatal error: Cannot redeclare class Doctrine\ORM\Mapping\Annotation in E:\www\acme\vendor\doctri
ne\orm\lib\Doctrine\ORM\Mapping\Annotation.php on line 22
I'm just using Doctrine bundle.
Any idea?
Thanks a lot
[Edit]
I've found that the problem is caused by
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
I use this row to parse the annotations for the symfony\validator bundle, without this the validation doesn't work...
I successfully generated entities from an existing mysql database, but I fail when trying to generate repository classes.
My doctrine.config.php
$frapi_config = array(
'database' => array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'root',
'dbname' => 'PizzaVan',
'host' => 'localhost:3306'
),
'doctrine' => array (
// Base path is /src/frapi/
'entitiesFolder' => "custom/Application/Model"
)
);
My bootstrap.php
//bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once dirname(__FILE__) . "/../vendor/autoload.php";
require_once "doctrine.config.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../" . $frapi_config["doctrine"]["entitiesFolder"]), $isDevMode, null, null, false);
// database configuration parameters
$conn = $frapi_config["database"];
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
My cli-config.php
require_once "bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
As said before, if I run 2 commands from the console
./vendor/bin/doctrine orm:convert-mapping --namespace='custom\\Application\\Model\\' --force --from-database annotation .
and
./vendor/bin/doctrine orm:generate-entities . --generate-annotations=true --generate-methods=true
I get Entities correctly created in the right folder. Now I would like to create custom RepositoryClass, but if I run
./vendor/bin/doctrine orm:generate-repositories .
I get this message : "No Repository classes were found to be processed".
Any solution? Thank you!
I am using ZF2 + Doctrine2 + PHPUNIT, when setting up Phpunit, it works fine for a basic test, however as soon as I try to run a test that invokes Doctrine2, I get:
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'#'localhost' (using password: YES)
But, I have never specified "username" nor "localhost" nor any sort of password. In-fact, my application runs perfectly fine, and the configuration I have specifies completely different settings.
So where is PHPUnit getting those settings and how to fix it?
My global.php:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'sbapp',
'password' => 'myp#ss',
'dbname' => 'root'
)
)
),
The Test:
class ApplicationControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
public function setUp()
{
$this->setApplicationConfig(
include '../../../config/application.config.php'
);
parent::setUp();
}
public function testAuthActionCanBeAccessed()
{
$postData = new \stdClass();
$postData->username = "someAppUser";
$postData->password = "12345";
$postData = json_decode(json_encode($postData),true);
$this->dispatch('/auth', 'POST', $postData);
$response = $this->getResponse();
$this->assertResponseStatusCode(200);
$this->assertActionName('auth');
}
}
The relative paths used within the config on setUp when done incorrectly can seriously affect the loading of entities and so on. So I was chasing around the right path.. when calling phpunit from root..or from vendor..or from within the test folder, etc..
Solution:
Call it from project root, and leave the routes exactly as the main project are.
I am learning Doctrine. I config doctrine 2.2.0 by Tarball Download. Now getting trouble when generating-the-database-schema. Can't use command-line tool with the code below:
<?php
// doctrine.php - Put in your application root
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\DBAL\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Helper\HelperSet;
$lib = "../DoctrineORM-2.2.0";
require $lib . '/Doctrine/ORM/Tools/Setup.php';
Setup::registerAutoloadDirectory($lib);
$paths = array("/path/to/entities-or-mapping-files");
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$dbParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => '',
'host' => '127.0.0.1',
'driver' => 'pdo_mysql'
);
$em = EntityManager::create($dbParams, $config);
$helperSet = new HelperSet(array(
'db' => new ConnectionHelper($em->getConnection()),
'em' => new EntityManagerHelper($em)
));
ConsoleRunner::run($helperSet);
The error here.
Fatal error: Class 'Doctrine\DBAL\Tools\Console\Helper\EntityManagerHelper' not found in E:\wamp\www\project\doctrine.php on line 30
and I can not find EntityManagerHelper.php under DoctrineORM-2.2.0\Doctrine\DBAL\Tools\Console\Helper .
Seems like EntityManagerHelper is under different namespace:
namespace Doctrine\ORM\Tools\Console\Helper;