phalcon/cphalcon · error · Phalcon\Events\Exceptions\InvalidEventType

Invalid event type {eventType}

Error message

Invalid event type {eventType}

What it means

fireAll() splits the event type string on the first colon to derive the component type and the event name ('db' + 'beforeQuery'). Phalcon event names must follow the 'type:eventName' format; InvalidEventType is thrown when the string contains no colon at all, so the name cannot be decomposed.

Source

Thrown at phalcon/Events/Manager.zep:589

        }

        // Fast exit on a manager with no listeners. Mirrors fire().
        if empty this->events {
            if unlikely this->strict {
                throw new NoListenersForEvent(eventType);
            }

            return [];
        }

        if fetch cached, this->eventNameCache[eventType] {
            let type      = cached[0];
            let eventName = cached[1];
        } else {
            let colonPos = strpos(eventType, ":");

            if unlikely colonPos === false {
                throw new InvalidEventType(eventType);
            }

            let type      = substr(eventType, 0, colonPos);
            let eventName = substr(eventType, colonPos + 1);

            let this->eventNameCache[eventType] = [type, eventName];
        }

        let hasTypeQueue = isset this->events[type];
        let hasFullQueue = isset this->events[eventType];

        if !hasTypeQueue && !hasFullQueue {
            if unlikely this->strict {
                throw new NoListenersForEvent(eventType);
            }

            return [];
        }

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Use the full 'type:eventName' form, e.g. 'db:beforeQuery', 'models:beforeCreate'
  2. If the name is dynamic, validate it contains a colon before firing: if (strpos($eventType, ':') === false) { throw new InvalidArgumentException(...); }
  3. Centralize event names in class constants so typos are caught once

Example fix

// before
$em->fireAll('beforeQuery', $connection); // throws InvalidEventType

// after
$em->fireAll('db:beforeQuery', $connection);
Defensive patterns

Strategy: validation

Validate before calling

function validEventType(string $eventType): bool
{
    return strpos($eventType, ':') !== false && strpos($eventType, ':') > 0;
}

if (!validEventType($eventType)) {
    throw new InvalidArgumentException("Event type must be 'component:eventName', got '{$eventType}'");
}

Type guard

function assertEventType(string $eventType): string
{
    [$type, $name] = explode(':', $eventType, 2) + [null, null];
    if ($type === null || $name === null || $type === '' || $name === '') {
        throw new InvalidArgumentException("Event type must be 'component:eventName', got '{$eventType}'");
    }
    return $eventType;
}

Try / catch

use Phalcon\Events\Exceptions\InvalidEventType;
try {
    $em->fireAll($eventType, $source);
} catch (InvalidEventType $e) {
    // programmer error: fail fast, do not swallow
    throw new InvalidArgumentException('Bad event name: ' . $eventType, 0, $e);
}

Prevention

When it happens

Trigger: $eventsManager->fireAll('beforeQuery', $this) (missing component prefix); a dynamically built name whose prefix variable is empty: $em->fireAll($prefix . ':' . $name) with $prefix = '' still contains a colon, but $em->fireAll($name) alone does not; names copied from Symfony-style single-word events.

Common situations: Porting code from Symfony EventDispatcher (single names like kernel.request) to Phalcon; refactoring that drops the 'component:' prefix; configuration files listing bare event names that are passed straight to fireAll().

Related errors


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