require_once missing doctrine zend framework - php

I'm integrating doctrine with Zend Framework. I've hit an error thrown from cli. It seems Zend_Application_Bootstrap_Bootstrap does not have a require_once for Zend_Application_Bootstrap_BootstrapAbstract. Has anyone hit this?
my cli-config.php
<?php
$classLoader = new \Doctrine\Common\ClassLoader('App', __DIR__ . "/../application/models");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Cms', __DIR__ . "/../application/modules/cms-modules/models");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__ . "/../application/models");
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = $config->newDefaultAnnotationDriver(array(
__DIR__."/../application/models/App",
__DIR__."/../application/modules/cms-modules/models/Cms"
));
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'bella',
'user' => 'username',
'password' => 'password',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
$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)
));

Bootstrap class should extends the Bootstrap Abstract class.
class Bootstrap extends Zend_Application_Module_Bootstrap {
//.....
}

Zend_Application does not use require_once. It is one of the first packages in ZF 1.* that requires the Zend Autoloader.

Yep replacing the doctrine class loader with Zend's auto loader did the trick. I had to add the path to the namespaces directly to the php path using set_include_path. Is there a nicer way to do this? I see Doctrine's class loader allows you to specify both the path and namespace. Thanks for your help beberlei and Alex

Related

Add Doctrine DBAL into own php project

Trying to add Doctrine DBAL into my own project to use it to access my db etc. I don't have composer and i never used it. This is what i am trying to do according to the docu:
use Doctrine\Common\ClassLoader;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
require_once "doctrine/Common/ClassLoader.php";
$classLoader = new ClassLoader('Doctrine', 'doctrine');
$classLoader->register();
$config = new Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => "",
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
}
This is taken from here:
-http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
and:
- http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/introduction.html
I have the Common and DBAL folder added into my project
My folder structure looks like this:
root
doctrine
DBAL
Common
php stuff
index.php (where connection.php) is executed
So what happens is that i either get "Cannot find class XY" or something similar, based upon what i change on the code. I never am able to execute it as it should following the tutorial.
What am i doing wrong here?
I just want to have the connection object, where i can start doing my stuff like useing the query builder etc...
I am completely lost here...
UPDATE: Installed composer as requested and have this Code now:
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Which is the 2nd code example in my 1st link. Tells me " Class 'Doctrine\DBAL\Configuration' not found ". Funny thing is, that IntelliJ can perfectly autocomplete the path (suggests me Configuration when finishing DBAL in the path) but PHP doesn't find it. If i remove the new Configuration PHP just tells me, that it doesn't find the DriverManager...
I installed it correctly via composer though, at least composer tells me it is installed correctly now (Where does composer save the libs?)
You now need to require composers autoload file.
require __DIR__.'/vendor/autoload.php';
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Please note, depending on your directory structure, the autoload file might be somewhere else, but usually this should work.
Pay attention to the use of namespaces: if the Doctrine namespace for its loader is Doctrine\Common\ClassLoader, you have to put the files inside the Doctrine\Common folder ("Doctrine" with a capital "D").
See the code snippet shown inside the Introduction chapter of the Doctrine DBAL documentations.

Fatal error HelperSet not found when setting up Doctrine Console (with CodeIgniter)

I've got a problem setting up Doctrine with CodeIgniter. When running the following error is coming up:
Fatal error: Class 'Symfony\Component\Console\Helper\HelperSet' not found in /Applications/MAMP/htdocs/CodeIgniter-2.2.1/application/doctrine.php on line 21
The folder structure looks like this
/application/
/application/doctrine.php
/application/libraries/
/application/libraries/Doctrine/
/application/libraries/Doctrine/Common
/application/libraries/Doctrine/DBAL
/application/libraries/Doctrine/ORM
/application/libraries/Doctrine/Symfony
/application/libraries/Doctrine/Doctrine.php
/application/libraries/Doctrine/index.html
This is the line 21
$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)
));
Cannot find out what is the problem..
Followed this tutorial: http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/
update This is my doctrine.php in the application folder
<?php
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
chdir(APPPATH);
require __DIR__ . '/libraries/Doctrine.php';
foreach ($GLOBALS as $helperSetCandidate) {
if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
$helperSet = $helperSetCandidate;
break;
}
}
$doctrine = new Doctrine;
$em = $doctrine->em;
$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)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Just downloaded github version and without any extra settings everything seems to be ok.
Check if the HelperSet.php file exists in Doctrine/Symfony/Component/Console/Helper directory, and add these lines to Doctrine.php in libraries directory (you have to register Symfony classes):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'your_path_to/Doctrine');
$symfonyClassLoader->register();
This GitHub repository is for CodeIgniter 2, don't use it.
Try this tutorial, it works fine for me, even for CI 3.

Stuck in Tutorial Doctrine2: "class Product is not a valid entry or mapped super class"

Trying to do the tutorial: https://github.com/doctrine/doctrine2/blob/master/docs/en/tutorials/getting-started.rst#id3
when executing $ php create_product.php ORM I get the error message:
class Product is not a valid entry or mapped super class
This is create_product.php
<?php
// create_product.php
require_once "bootstrap.php";
$newProductName = $argv[1];
$product = new Product();
$product->setName($newProductName);
$entityManager->persist($product);
$entityManager->flush();
echo "Created Product with ID " . $product->getId() . "\n";
And the bootstrap.php is
<?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 = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// database configuration parameters
$conn = array(
'driver' => 'pdo_mysql',
'dsn' => 'mysql:dbname=doctrine2;host=any.where.nl',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
)
// 'driver' => 'pdo_sqlite',
// 'path' => __DIR__ . '/db.sqlite',
);
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
My Products.php is an exact copy as in the tutorial and located in /src/Project.php
Any idea why the error-message says that Product.php is not valid entity? And how to solve it?
Best regards,
Tim van Steenbergen
You probably didn't put #Entity annotation, the example is missing it. Try this:
/**
* #Entity
* #Table(name="product")
*/
/**
* #ORM\Entity
* #ORM\Table(name="bugs")
*/
in the context of Doctrine 2 manual. Mentioned manual contain wrong code in the entity Bug, not in the entity Product now.

Symfony autoloader component

I'm getting:
Fatal error: Class 'Twig_Loader_Filesystem'
<?php
require_once "library/Symfony/Component/ClassLoader/UniversalClassLoader.php";
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace("Symfony\Component", "library/Symfony/Component");
$loader->registerPrefix("Twig_", "library/Twig");
$loader->register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => '',
));
?>
The Twig folder is in library folder. Have I missunderstood on how to use the component?
Are you sure your path is correct?
Try to use __DIR__ . '/library'
Try to use DebugUniversalClassLoader to pin down the problem - you can catch a RuntimeException and see which file it actually tries to load.
EDIT:
Correct solution: If you try to load PEAR-style classes with prefix Twig_ from '/library/Twig', you should point it to '/library', because Twig_ prefix itself will be used as a directory name inside /library

Zend Framework 1.11 with Doctrine 2 and namespaces

I've been using Doctrine in my project without explicitly namespacing any of my classes. This led to some problems with trying to organise my code into separate sub directories (or at least it seemed to). As such I've tried to implement namespaces in my code but I'm struggling and having tried the numerous solutions on here to no avail, I need to ask.
I've got the standard project structure:
application/
--models/
--services/
--controllers/
..etc
In my Bootstrap I've got the following (without namespaces in my code which works fine):
/**
* Initialize Doctrine
* #return Doctrine_Manager
*/
public function _initDoctrine() {
// include and register Doctrine's class loader
require_once('doctrine/Doctrine/Common/ClassLoader.php');
$autoloader = \Zend_Loader_Autoloader::getInstance();
require_once('doctrine/Doctrine/Common/ClassLoader.php');
$commonLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', 'doctrine');
$autoloader->pushAutoloader(array($commonLoader, 'loadClass'), 'Doctrine\Common');
$dbalLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', 'doctrine');
$autoloader->pushAutoloader(array($dbalLoader, 'loadClass'), 'Doctrine\DBAL');
$ormLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', 'doctrine');
$autoloader->pushAutoloader(array($ormLoader, 'loadClass'), 'Doctrine\ORM');
$modelLoader = new \Doctrine\Common\ClassLoader(NULL, APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), '');
// create the Doctrine configuration
$config = new \Doctrine\ORM\Configuration();
// setting the cache ( to ArrayCache. Take a look at
// the Doctrine manual for different options ! )
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// choosing the driver for our database schema
// we'll use annotations
$driver = $config->newDefaultAnnotationDriver(
APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl($driver);
// set the proxy dir and set some options
$config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
$config->setAutoGenerateProxyClasses(true);
//$config->setAutoGenerateProxyClasses(false);
$config->setProxyNamespace('App\Proxies');
// now create the entity manager and use the connection
// settings we defined in our application.ini
$connectionSettings = $this->getOption('doctrine');
$conn = array(
'driver' => $connectionSettings['conn']['driv'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['host']
);
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
// push the entity manager into our registry for later use
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $entityManager;
return $entityManager;
}
When I add
namespace models;
to each of my model classes and update the Bootstrap to be as follows I get an Exception "Class Application does not exist" (Application is one of my models):
$modelLoader = new \Doctrine\Common\ClassLoader('models', APPLICATION_PATH . "/models");
$autoloader->pushAutoloader(array($modelLoader, 'loadClass'), 'models');
Just for completeness, I reference that model in my controller as follows:
public function indexAction()
{
$this->_helper->layout()->title = "Your applications";
$this->_helper->layout()->description = "Create, edit and view all applications you have registered with us.";
$this->view->applicationList = $this->entityManager->getRepository("Application")->findAll();
}
What am I missing? I'm sure it's obvious but really am pulling out my hair now.
After much futher searching, I stumbled across this video (login required).
Basically, the way I solved (as per the webinar) was to namespace my entities and save them under the /library directory. Then whenever I need to use them from the actual app, I access via their namespace.

Categories