phalcon/cphalcon · error · Phalcon\Mvc\Model\MetaData\Exceptions\InvalidContainer

The dependency injector is invalid

Error message

The dependency injector is invalid

What it means

The annotations-based metadata strategy (Phalcon\Mvc\Model\MetaData\Strategy\Annotations) resolves column maps by fetching the 'annotations' service from the DI container passed into getColumnMaps(). If that container is not an object (null or scalar), the strategy cannot resolve its dependencies and throws InvalidContainer before any parsing happens.

Source

Thrown at phalcon/Mvc/Model/MetaData/Strategy/Annotations.zep:34

use Phalcon\Mvc\Model\MetaData\Exceptions\InvalidContainer;
use Phalcon\Mvc\Model\MetaData\Exceptions\NoAnnotationsForClass;
use Phalcon\Mvc\Model\MetaData\Exceptions\NoPropertyAnnotationsForClass;
use Phalcon\Mvc\ModelInterface;

class Annotations implements StrategyInterface
{
    /**
     * Read the model's column map, this can't be inferred
     */
    final public function getColumnMaps(<ModelInterface> model, <DiInterface> container) -> array
    {
        var annotations, className, reflection, propertiesAnnotations, property,
            propAnnotations, columnAnnotation, columnName;
        array orderedColumnMap, reversedColumnMap;
        bool hasReversedColumn;

        if unlikely typeof container != "object" {
            throw new InvalidContainer();
        }

        let annotations = container->get("annotations");

        let className = get_class(model),
            reflection = annotations->get(className);

        if unlikely typeof reflection != "object" {
            throw new NoAnnotationsForClass(className);
        }

        /**
         * Get the properties defined in
         */
        let propertiesAnnotations = reflection->getPropertiesAnnotations();

        if unlikely empty propertiesAnnotations {
            throw new NoPropertyAnnotationsForClass(className);

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Pass a valid Phalcon\Di\DiInterface instance — normally the one obtained from $metaData->getDI().
  2. Set the container on the MetaData component ($metaData->setDI($di)) and let initializeColumnMap() hand the strategy the container itself.
  3. In tests, build a minimal FactoryDefault that registers an 'annotations' service before invoking the strategy.

Example fix

// before
$strategy = new Phalcon\Mvc\Model\MetaData\Strategy\Annotations();
$maps = $strategy->getColumnMaps(new Invoices(), null); // InvalidContainer

// after
$di = new Phalcon\Di\FactoryDefault(); // includes 'annotations' service
$maps = $strategy->getColumnMaps(new Invoices(), $di);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!$container instanceof \Phalcon\Di\DiInterface || !$container->has('annotations')) {
    throw new RuntimeException('Annotations strategy needs a DI container with an "annotations" service');
}

Type guard

use Phalcon\Di\DiInterface;

function hasUsableContainer(mixed $container): bool
{
    return $container instanceof DiInterface;
}

Try / catch

use Phalcon\Mvc\Model\Exceptions\InvalidContainer;

try {
    $maps = $strategy->getColumnMaps($model, $container);
} catch (InvalidContainer $e) {
    $maps = $strategy->getColumnMaps($model, Phalcon\Di\Di::getDefault());
}

Prevention

When it happens

Trigger: Calling $strategy->getColumnMaps($model, $container) or getMetaData() directly with null or a non-object container — custom strategies or test code invoking the strategy outside the metadata pipeline; a custom modelsMetadata implementation that forwards an unset container variable to the strategy.

Common situations: Unit tests exercising the Annotations strategy directly; custom StrategyInterface implementations reusing Annotations::getColumnMaps(); refactors that drop the container argument along the call chain.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/c62cc0e413ea0f0f. Report an issue: GitHub.