Search code examples
phpsymfonyincludepropel

Propel & Sympfony 2 Autoloader


I am currently using Propel 1.6 and Symfony 2 autoloader class.

My problem is that I can access the first class but it somehow doesnt access the other classes The error:

Fatal error: Class 'model\om\BaseUser' not found in C:\xampp\htdocs\gym\build\classes\model\User.php on line 20

The XML

<?xml version="1.0" encoding="utf-8"?>
<database name="gym" namespace="model" defaultIdMethod="native">

The build.properties

# Database driver
propel.database = mysql

# Project name
propel.project = gym
propel.namespace.autoPackage = true
propel.database.url = mysql:host=localhost;dbname=test
propel.database.user = root
propel.database.password = 

The PHP

// Include the main Propel script
require_once '/propel/Propel.php';
// Initialize Propel with the runtime configuration
//Propel::init("/build/conf/gym-conf.php");

require_once realpath( dirname( __FILE__ ) ) . "/ClassLoader/UniversalClassLoader.php";
use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();

$loader->registerNamespaces( array ( 
                                    "build\classes\model" => realpath( dirname( __FILE__ )  ),
                                    "Symfony\Component" => realpath( dirname( __FILE__ ) ),
                                    "s" => __DIR__
                        ));


$loader->register();

use \build\classes\model\User;
$a = new User;

Solution

  • It's a classic autoloading issue... register your namespace with the autoloader correctly.

    Make sure you have registered model\om with it's real path in your autoloader if it uses a different path than the other namespaces.

    $loader->registerNamespaces( array ( 
        // ... namespaces here 
        "model\om" =>  'path_here',
    ));
    

    If that's not the solution you might have a missing/wrong use-statement in your \build\classes\model\User...

    ... or you are trying to construct an inexistant class like this.

    // this tries to load BaseUser build\classes\model\model\om\BaseUser
    // ... if used in User.php
    $baseUser = new model\om\BaseUser;  
    

    ... when it should instead read

    $baseuser = new \model\om\BaseUser;
    

    ... or better

    use model\om\BaseUser;
    
    // ...
    
    $baseUser = new BaseUser();
    

    ... or your BaseUser class violates PSR naming conventions in some way.