phalcon/cphalcon · error · Phalcon\Paginator\Exceptions\BuilderModelNotDefined

Model not defined in builder

Error message

Model not defined in builder

What it means

Thrown in Paginator\Adapter\QueryBuilder::getPaginate when computing the total for a HAVING/multi-GROUP query: builder->getModels() returns null, so the native count query cannot be constructed against a known model. The input at fault is a query builder created without from()/setModels.

Solutions

  1. Set the model on the builder: $builder->from('App\Models\X') before pagination.
  2. Avoid having()/multi-group pagination for builders without a model, or compute totals manually.
  3. Verify custom builder factories always call from() with a model.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Paginator/Adapter/QueryBuilder.zep:275 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at phalcon/Paginator/Adapter/QueryBuilder.zep:275

         * Remove the 'ORDER BY' clause, PostgreSQL requires this
         */
        totalBuilder->orderBy(null);

        /**
         * Obtain the PHQL for the total query
         */
        let totalQuery = totalBuilder->getQuery();

        /**
         * Obtain the result of the total query
         * If we have having perform native count on temp table
         */
        if hasHaving || hasMultipleGroups {
            let sql = totalQuery->getSql(),
                modelClass = builder->getModels();

            if unlikely modelClass === null {
                throw new BuilderModelNotDefined();
            }

            if typeof modelClass == "array" {
                let modelClass = array_values(modelClass)[0];
            }

            let model     = create_instance(modelClass),
                dbService = model->getReadConnectionService(),
                db        = totalBuilder->getDI()->get(dbService);

            let row = db->fetchOne(
                "SELECT COUNT(*) as \"rowcount\" FROM (" .  sql["sql"] . ") as T1",
                Enum::FETCH_ASSOC,
                sql["bind"]
            );

            let rowcount = row ? intval(row["rowcount"]) : 0,
                totalPages = intval(ceil(rowcount / limit));

View on GitHub (pinned to b7419de9cd)