Doctrine and Codeigniter: Entity not found - php

I'm using Codeigniter 3 and Doctrine 2, which is loaded by a library class.
Doctrine itself is loaded via composer autoload.
On localhost (windows with php 5.6.0) I'm using the php built in server and everything is working.
When I'm uploading the project to my webserver (Plesk with nginx and proxied apache, php 5.6.15), i get the following error:
An uncaught Exception was encountered
Type: Doctrine\Common\Persistence\Mapping\MappingException
Message: Class 'Entity\User' does not exist
Filename: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php
Line Number: 96
Backtrace:
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php
Line: 41
Function: nonExistingClass
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 282
Function: getParentClasses
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 313
Function: getParentClasses
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Line: 78
Function: loadMetadata
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
Line: 216
Function: loadMetadata
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
Line: 281
Function: getMetadataFor
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php
Line: 44
Function: getClassMetadata
File: /var/www/vhosts/example.org/project.example.org/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php
Line: 698
Function: getRepository
File: /var/www/vhosts/example.org/project.example.org/application/controllers/User.php
Line: 18
Function: getRepository
File: /var/www/vhosts/example.org/project.example.org/index.php
Line: 301
Function: require_once
The controller is as follows:
....
public function show($id)
{
$user = $this->doctrine->em->getRepository('Entity\User')->findOneBy(array('id' => $id));
}
....
and the doctrine library:
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine {
public $em;
public function __construct()
{
// Load the database configuration from CodeIgniter
require APPPATH . 'config/database.php';
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/proxies';
$metadata_paths = array(APPPATH . 'models/entity');
// Set $dev_mode to TRUE to disable caching while you develop
$dev_mode = true;
// If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
// to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
The library is loaded via the autoload.php:
$autoload['libraries'] = array('OAuth2', 'session', 'doctrine');
the entity is located in models\Entity\
<?php
namespace Entity;
use Doctrine\ORM\Mapping\Entity;
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User Model
*
* #Entity
* #Table(name="user")
*/
class User {
/**
* #Id
* #Column(type="integer", nullable=false)
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
....
The strange thing is that it works locally.
Any ideas?

Found the problem: Case-sensitivity of linux:
The folder of the entities should be capitalized according to the namespace. Since the directory was models\entity\ and Windows' file system is case-insensitiv, it worked on locally. But linux is case-sensitiv and expects models\Entity.
Renamed the folder to models\Entity and it's working.

Related

Gas ORM - Auto Create Table show error message

In use Gas ORM for CodeIgniter.
Like what said in : http://gasorm-doc.taufanaditya.com/configuration.html
Gas ORM support auto-creation of tables. This mean you can convert your existing Gas models into a database. For security reasons, this option is disabled by default. To enable :
$config['auto_create_tables'] = TRUE;
And then i enable migration in migration.php and then create 2 class in models folder called user.php and blog.php. The code look like :
User class :
<?php
namespace Model;
use \Gas\Core;
use \Gas\ORM;
class User extends ORM {
public $primary_key = 'id';
function _init()
{
self::$relationships = array (
'blog' => ORM::has_many('\\Model\\Blog');
);
self::$fields = array(
'id' => ORM::field('auto[10]'),
'username' => ORM::field('char[64]'),
'password' => ORM::field('char[255]'),
'email' => ORM::field('char[255]'),
);
}
}
Blogclass:
<?php namespace Model;
use \Gas\Core;
use \Gas\ORM;
class Blog extends ORM {
public $primary_key = 'id';
function _init()
{
self::$relationships = array (
'user' => ORM::belongs_to('\\Model\\User')
);
self::$fields = array(
'id' => ORM::field('auto[10]'),
'title' => ORM::field('char[255]', array('required','max_length[255]')),
'body' => ORM::field('string'),
'modified_at' => ORM::field('datetime'),
'created_at' => ORM::field('datetime'),
);
$this->ts_fields = array('modified_at','[created_at]');
}
}
When i refresh the pages, the page showing error:
A PHP Error was encountered
Severity: Runtime Notice
Message: Only variables should be passed by reference
Filename: classes/core.php
Line Number: 2460
Backtrace:
File: /application/third_party/gas/classes/core.php
Line: 2460
Function: _error_handler
File: /application/third_party/gas/classes/core.php
Line: 320
Function: _generate_tables
File: /application/third_party/gas/classes/core.php
Line: 360
Function: __construct
File: /application/third_party/gas/bootstrap.php
Line: 229
Function: make
File: /application/libraries/Gas.php
Line: 111
Function: include_once
File: /application/controllers/Home_Controller.php
Line: 7
Function: __construct
File: /index.php
Line: 315
Function: require_once
I really stuck with this error. Can anyone help me to solve my problem?
I already trace my code, and the problem similar like this reference : Only variables should be passed by reference
The fact of this problem is maybe the code show error when running but actually the code completely its function. So i decide to turn the $config['auto_create_tables'] = TRUE; to $config['auto_create_tables'] = FALSE; after using this feature.

Symfony 2 Doctrine 2 EntityManager config

i'm trying to get my Doctrine CommandLine Tool working in Symfony 2 project on Windows 7 and I keep getting the same error message in console:
Fatal error: Call to protected Doctrine\ORM\EntityManager::__construct()
from invalid context in C:\wamp\www\firstSymfonyApp\cli-config.php on line 9
Call Stack:
0.0010 239440 1. {main}() C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:0
0.0090 621376 2. require('C:\wamp\www\firstSymfonyApp\cli-config.php') C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:48
Code of my cli-config.php file:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once 'app/bootstrap.php.cache';
$em = new \Doctrine\ORM\EntityManager();
return ConsoleRunner::createHelperSet($em);
Until today, I was only using doctrine on Linux where the installation was much more simple, please help me work this out.
Error message is very clear. EntityManager::__construct is protected method therefore you can't use it outside of the class.
Check out EntityManager::create.
Check this link for more information about how to start with Doctrine 2.
This is probably the snippet which should be important to you right now:
<?php
// bootstrap.php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entity-files");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'foo',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

How to get right ldap resources with Zend Ldap?

I started using this module from Zend (https://framework.zend.com/manual/2.3/en/modules/zend.ldap.introduction.html) for work with LDAP, and I want to implement it into my classes which works with LDAP. But when I want to use some php ldap function which are not implemented in Zend-Ldap I have to get the resource identifier but I always get resource(37) of type (Unknown) or error dap_mod_del(): 41 is not a valid ldap link resource (2). I think I use Zend module correctly you can see below.
Other zend-ldap features work perfect. But I cant get the right resources.
index.php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/api/AtaLdap.php';
require_once __DIR__ . '/api/classes/Users.php';
$ldap = new AtaLdap\AtaLdap();
var_dump($ldap->Zend());
AtaLdap.php
namespace AtaLdap;
use Zend;
class AtaLdap
{
const ldapServer = *********;
const ldapPort = **********;
const ldapLogin = *********;
const ldapPass = *********;
const ldapBaseDn = ********;
/**
* Connect to LDAP via Zend LDAP module
*
* #return Zend\Ldap\Ldap
* #throws Zend\Ldap\Exception\LdapException
*/
public static function Zend()
{
$ldapOptions = [
'host' => self::ldapServer,
'port' => self::ldapPort,
'password' => self::ldapPass,
'bindRequiresDn' => TRUE,
'baseDn' => self::ldapBaseDn,
'username' => self::ldapLogin
];
$ldap = new Zend\Ldap\Ldap($ldapOptions);
$ldap->bind();
return $ldap->getResource();
}
public static function ZendRes()
{
return self::Zend()->getResource();
}
public static function deleteEntry($fromDN, $nameEntry, $contentEntry)
{
$res = self::Zend()->getResource();
$entryToDelete["$nameEntry"] = $contentEntry;
$deleteEntry = ldap_mod_del($res, $fromDN, $entryToDelete);
return $deleteEntry ? TRUE : FALSE;
}

Slim 3: how to access settings?

Before the Slim 3 is released, codes below work fine:
settings.php,
return [
'settings' => [
'displayErrorDetails' => true,
'modules' => [
'core' => 'config/core/modules.php',
'local' => 'config/local/modules.php'
],
],
];
index.php
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
$MyClass = new MyClass($app);
MyClass.php
class MyClass
{
private $app;
public function __construct($app)
{
$this->app = $app;
$local = require $app->settings['modules']['local'];
}
But after the release, I get this error below:
Notice: Undefined property: Slim\App::$settings in /...
So I can't use $app->settings anymore? What should I use then?
You can get settings like this:
$container = $app->getContainer();
$settings = $container->get('settings');
You can access settings route callables via $this
$modulesSettings = $this->get('settings')['modules']['local'];
For more information read here
The address of the SLIM 3 configuration file is pro/src/settings.php,
and you can add additional settings; In any route you can access them like this:
var_dump($this->get('settings')['logger']);

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.

Categories