[Solved] Propel with CodeIgniter – Fatal error: Undefined class constant ‘Tablename::PEER’

When we have the name collision i.e. name of Propel generated and CodeIgniter Controller class are same then this error occurs.


Fatal error: Undefined class constant 'Users::PEER' in /media/htdocs/htdocs/project/application/libraries/propel/runtime/lib/query/ModelCriteria.php on line 68

The error occured to me because I had the same name for the Propel generated “Users” Model class and I also had the CI Controller named “Users”. Since propel generates the model at runtime looking at the request for the model usage. This collision can be found only at the runtime.

When you have this error you can navigate to the Base[Table-name]Peer.php > populateObjects function.

There you can see the code

[sourcecode language=’php’]public static function populateObjects(PDOStatement $stmt)
{
$results = array();

// set the class once to avoid overhead in the loop
$cls = UsersPeer::getOMClass();
// populate the object(s)
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$key = UsersPeer::getPrimaryKeyHashFromRow($row, 0);
if (null !== ($obj = UsersPeer::getInstanceFromPool($key))) {
// We no longer rehydrate the object, since this can cause data loss.
// See http://www.propelorm.org/ticket/509
// $obj->hydrate($row, 0, true); // rehydrate
$results[] = $obj;
} else {
$obj = new $cls();
$obj->hydrate($row);
$results[] = $obj;
UsersPeer::addInstanceToPool($obj, $key);
} // if key exists
}
$stmt->closeCursor();

return $results;
}

[/sourcecode]

You can see the line $obj = new $cls(); creates model class dynamically, at this stage when name collision occurs, instead of loading the Propel Model class, it loads the Code Igniter class which throws the error “Fatal error: Undefined class constant ‘Users::PEER'” which is correct because Controller class do not have that constant variable defined in it.

Sometimes this also errors in the redeclare class of Propel.php.

I was able to find this by using step by step debugging using Xdebug. Thanks to Xdebug made my day.

I wasted hours trying to find the exact error. Hope this helps you and saves your time.