Doctrine 2 - problems with "Getting Started XML-Edition" - generating database schema - php

Having never touched Doctrine before (either 1 or 2), I am following this tutorial for Doctrine 2.
I'm at the point where I use the command line to generate the database schema. This is the cli-config.php file, as per the tutorial:
<?php
$cliConfig = new Doctrine\Common\Cli\Configuration();
$cliConfig->setAttribute('em', $entityManager);
When I run it though, I just get an error:
Fatal error: require(): Failed opening required 'Doctrine\Common\Cli\Configuration.php'
Because that class referenced by the cli-config.php file doesn't exist. I've also tried blanking the cli-config.php file, which of course doesn't work either - says that "The helper "em" is not defined."
I'm using version 2.0.0BETA3. I know that this is a beta version, so they could have changed some files around, but I can't find that class anywhere.
Any ideas on how to get it working?

The docs in the XML Getting Started are outdated in this regard. Please see the Tools section in the manual on how to configure the CLI Tool:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/tools.html
All the rest still works as described. I will update this part asap.

Assuming you installed Doctrine using pear
$ sudo pear install pear.doctrine-project.org/doctrineORM
which will install the three 'Doctrine 2' packages: DoctrineCommon, DoctrineDBAL, and DoctrineORM. On Ubuntu, these packages will be located in /usr/share/php/Doctrine, and the doctrine command line utility, will be installed into /usr/bin.
With this setup, this is a version of cli-config.php you can use (note: DIR should have two underscores before and after it. For some reason they didn't display).
<?php
require ‘Doctrine/ORM/Tools/Setup.php’;
// Setup Autoloader (1)
Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();
require_once 'Doctrine/Common/ClassLoader.php';
$classLoader = new Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__."/Entities"));
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'bugs',
'user' => 'bugs',
'password' => 'xyzabc',
'host' => 'localhost' );
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

Related

PDO Library not detected (classes not found) PHP

I'm doing my own website and i'm using a PDO library here. I have somes library working fine but this one for any reason cannot be "detected"
Files in the folder:
Installed lib using composer => /main/assets/
File where i want to use the lib => /main/user/index.php
Lib path => /main/assets/vendor/faapz/pdo/...
Reproduce this:
1: In a folder (main), create another folder named "assets", then execute the command "composer require faapz/pdo" (this require an installation of composer)
2: In the folder (main), create folder named "user" and create new php file.
3: Use this code
<?php
// => Class 'FaaPz\PDO\Database' not found in ...\main\user\file.php on line 10
$parent_dir = dirname(__DIR__);
require $parent_dir . '/assets/vendor/autoload.php';
$db = new \PDO('mysql:dbname=your_db_name;host=your_db_host;charset=utf8mb4', 'root', ''); // Working lib
$query = 'mysql:host=your_db_host;dbname=your_db_name;charset=utf8';
$user = 'root';
$pass = '';
$database = new FaaPz\PDO\Database($query, $user, $pass); // Not working lib => Class 'FaaPz\PDO\Database' not found in ...\main\user\file.php on line 10
$auth = new \Delight\Auth\Auth($db); // Working lib
?>
I found the package "ext-pdo" work for my problem. Just install it using composer with the following command => composer require ext-pdo

PAMI can't find class

I'm trying to install PAMI library
I installed it via pear:
# pear channel-discover pear.marcelog.name
# pear install marcelog/PAMI
and trying to use example.php
$pamiClientOptions = array(
'host' => '127.0.0.1',
'scheme' => 'tcp://',
'port' => 9999,
'username' => 'admin',
'secret' => 'mysecret',
'connect_timeout' => 10000,
'read_timeout' => 10000
);
use PAMI\Client\Impl\ClientImpl as PamiClient;
$pamiClient = new PamiClient($pamiClientOptions);
// Open the connection
$pamiClient->open();
// Close the connection
$pamiClient->close();
when I try to use this script I receive error:
Class 'PAMI\Client\Impl\ClientImpl' not found
It's first time, I see that classes are included like this (use).
I'm using debian Linux also. Please, help.
UPDATE
Also it's installed in /usr/share/php/PAMI/
Solved. I've found solution in "in_depth explanation"
You had to do this after pear installation
require_once '/usr/share/php/PAMI/Autoloader/Autoloader.php';
PAMI\Autoloader\Autoloader::register();
I put this two strings at top of my script and it works now. But it also receives strange
PHP Fatal error: Class 'Logger' not found in
I solve this by installing log4php:
$ pear channel-discover pear.apache.org/log4php
$ pear install pear.apache.org/log4php/Apache_log4php-2.1.0
And also you should put before first require_once -
require_once '/usr/share/php/log4php/Logger.php';

#!/usr/bin/env php running orm:schema-tool:create

I've installed Doctrine using composer, and when I try to create the database I get the error:
#!/usr/bin/env php
Why am I seeing this error - no database is created. I obtain the same error if I run update --force. I've also tried orm:schema-tool:create, and I've also tried running the doctrine php file directly php ./vendor/bin/doctrine.php orm:schema:create with the same error.
I have an bootstrap file and a cli-config file as such
// bootstrap.php
<?php
require_once 'vendor/autoload.php';
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__."/"), $isDevMode);
// database configuration parameters
$conn = array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
);
// obtaining the entity manager
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
// cli-config.php
<?php
require_once "bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
After much debugging, the path in this line is horribly wrong:
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__."/"), $isDevMode);
Fixing it so it points to where the class files with mapping data is stored fixed it.

How to generate data base with Doctrine 2?

I start with doctrine and want to create a db or php class from table with doctrine.
First i try create db. My bootstrap.php:
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
require_once "src/BugRepository.php";
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
$conn = array(
'driver' => 'pdo_pgsql',
'user' => 'postgres',
'password' => '123456',
'host' => 'localhost',
'dbname' =>'testdata',
);
$entityManager = EntityManager::create($conn, $config);
and i do:
php vendor/bin/doctrine doctrine:database:create
my output:
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../doctrine/orm/bin"
BIN_TARGET="`pwd`/doctrine"
cd "$SRC_DIR"
"$BIN_TARGET" "$#"
But db testdatanot created.
Same with creating php class from table:
php vendor/bin/doctrine doctrine:mapping:import YourAppBundle yml --filter="bugs"
But yml file not created.
Whats can be wrong?
There are also a bin folder in vendor/doctrine/orm/bin/ you can use this one like this
php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create
make sure you have root folder and a cli-config.php file is present in root folder.
https://groups.google.com/forum/#!msg/doctrine-user/_ph183Kh-5o/_P_coljB-dcJ

Why is symfony2 not copying my bundle over?

My appKernel.php looks like this:
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
new Blogger\BlogBundle\BloggerBlogBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
My autoload.php looks like:
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
'Sensio' => __DIR__.'/../vendor/bundles',
'JMS' => __DIR__.'/../vendor/bundles',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
'Doctrine\\DBAL' => __DIR__.'/../vendor/doctrine-dbal/lib',
'Doctrine' => __DIR__.'/../vendor/doctrine/lib',
'Monolog' => __DIR__.'/../vendor/monolog/src',
'Assetic' => __DIR__.'/../vendor/assetic/src',
'Metadata' => __DIR__.'/../vendor/metadata/src',
));
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
'Twig_' => __DIR__.'/../vendor/twig/lib',
));
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->registerPrefixFallbacks(array(__DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs'));
}
$loader->registerNamespaceFallbacks(array(
__DIR__.'/../src',
));
$loader->register();
AnnotationRegistry::registerLoader(function($class) use ($loader) {
$loader->loadClass($class);
return class_exists($class, false);
});
AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload(__DIR__.'/../vendor/swiftmailer/lib/swift_init.php');
When I do:
php app/console assets:install web
I get the following:
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Symfony\Bundle\WebProfilerBundle into web/bundles/webprofiler
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
Why is my BloggerBlogBundle not getting copied over?
Note: As OP's answer is short and doesn't really go into too much detail, here's a more in-depth answer for future readers, so hopefully you don't have to do as much digging as I did! :)
It seems that we're following the exact same tutorial and have the exact same issue, and as the answer above isn't very descriptive and I had to do a little digging myself, here's exactly what's wrong.
Step 1
Your src/Blogger/BlogBundle needs to have, in the /resources folder, a public/css folder. So you need to place your CSS files as follows:
src/Blogger/BlogBundle/resources/public/css/<stylesheet>.css
Create the /public/css folder if it doesn't exist (it didn't exist for me).
Step 2
You need to run the assetic management tool to have a symlink created in /web automatically for you so these can be accessed.
php app/console assets:install web
You now will notice that if you open (using windows explorer / nautilus on linux) your dev directory and find the /web folder, that /web/bundles/bloggerblog/css/<stylesheet>.css has been created automatically for you!
You can now access your CSS files in your code using:
<link href="{{ asset('/bundles/bloggerblog/css/<stylesheet>.css') }}" type="text/css" rel="stylesheet" />
Note: '.css' is replaced with screen 'screen.css' for this tutorial
...and that should be it!
I have the the exact same problem with the original question, where assets are not copied over from Resources folder of bundles to web/bundles after running 'php app/console assets:install web' so what i did was running this command 'php app/console cache:clear --env=prod' and that fixed it. As you can see that you are dealing with prod assets or installing assets to prod environment in web directory, so clear cach for prod is reasonable. So do this:
1) $ php app/console cache:clear --env=prod
2) $ php app/console assets:install web
OR $ php app/console assets:install web --symlink
3) $ php app/console assetic:dump --env=prod (If you use built in Assetic, it will dump/combine css to one big one in /web/css as opposed to /web/bundles)
everything goes in place after that. you get all assets in your /web/bundles and assetic dump in /web/css and all of these are for prod env.
Just had this problem, ensure that the BlahBundle.php is located in the same directory as the Resources directory
I've had this issue today with Symfony3.3.13 and the problem was the public directory was actually capitalised Public, which seems to break the install script. Changing the name and it worked immediately.

Categories