Phalcon: Choose Model Objects or Array Rows for Read-Heavy Queries
Decide when to hydrate Phalcon ORM rows as model objects versus plain arrays, with version-safe validation steps.
11 Dec 2025, 08:53 UTC

Decision: match hydration to the query, not the application
Phalcon\u2019s ORM hydrates database rows into model objects by default. Hydration means converting a database row into an in-memory object. That gives every row accessors, relationship navigation, validation, and model events. It also constructs one object per row, which adds memory and garbage-collection pressure. For a read-only report exporting tens of thousands of rows, that overhead is usually wasted.
Useful takeaway: keep model objects for domain logic and writes, and switch to plain arrays for bulk read-only output. Make the choice per query rather than globally.
Constraints and assumptions
- Phalcon is installed as a compiled C extension for the target PHP version. Shared or managed hosting may block installation or upgrades.
- Hydration method names and constants differ across Phalcon 3.x, 4.x, and 5.x. Verify the installed extension before relying on a specific class or constant.
- This guide covers read queries. Writes and relationship traversal require model objects.
Supported options compared
| Option | Best for | Capabilities | Cost |
|---|---|---|---|
| Model objects (default) | Domain logic, forms, writes, related data | Accessors, lazy relationships, model events, behaviors | Higher per-row memory and garbage-collection pressure |
| Plain arrays | Reports, exports, large read-only listings | Array key access only | Lower peak memory, less object allocation |
| stdClass objects | Read-only data with property syntax, no ORM features | Property access, no relationships or events | Lower than models, higher than arrays |
Trade-offs
Model objects
Model objects let you write $robot->type and navigate $robot->parts. They trigger beforeSave, afterSave, and related-model hooks. The cost is object construction for every row and the PHP objects remaining in memory until the resultset is freed.
Plain arrays
Array rows avoid per-row object construction. They are appropriate when you only need column values, such as a CSV export or a dashboard summary. Code that expects $row->related on an array row will fail. Arrays also do not support lazy relationship loading or model events.
stdClass objects
Some Phalcon versions offer a middle ground: lightweight objects without ORM behavior. Verify availability with reflection before using this mode.
Implementation: switch hydration per resultset
Phalcon resultsets expose a hydration mode. The exact constant names vary by version, so confirm them on your installation before deploying.
<?php
use Phalcon\\Mvc\\Model\\Resultset;
// Default: model objects. Good when you need relationships.
$robots = Robots::find();
foreach ($robots as $robot) {
echo $robot->name; // object accessor
echo $robot->type->name; // relationship navigation (if loaded)
}
// Read-only export: plain arrays. No relationships, lower memory.
$robots->setHydrateMode(Resultset::HYDRATE_ARRAYS); // verify constant name
foreach ($robots as $row) {
echo $row['name']; // array access only
// $row->type is invalid here
}
Place setHydrateMode() immediately after the query returns the resultset and before iterating. If your installed Phalcon version uses different constant names, the reflection check below will list them.
Validation: measure before changing a hot path
Run these checks on a staging instance with production-like data volume. Do not assume gains from small local datasets.
1. Confirm the extension and PHP version
# Run in the application terminal. No special permissions required.
php -v
php -m | grep -i phalcon
Expected: phalcon appears in the module list. If it is missing, the ORM hydration examples will not run.
2. List hydration methods for the installed version
# Run in the application terminal.
php -r '
$class = new ReflectionClass("Phalcon\\\\Mvc\\\\Model\\\\Resultset");
foreach ($class->getConstants() as $name => $value) {
if (strpos($name, "HYDRATE") !== false) {
echo $name . " = " . $value . PHP_EOL;
}
}
foreach ($class->getMethods() as $method) {
if (stripos($method->getName(), "hydrate") !== false) {
echo $method->getName() . PHP_EOL;
}
}
'
Expected output: constants such as hydration modes and methods such as setHydrateMode. Treat the exact names as version-specific.
3. Compare memory and time across modes
<?php
// Run on staging with a large read-only table. Adjust the model and limit.
use Phalcon\\Mvc\\Model\\Resultset;
function measure(callable $fn): array {
$start = microtime(true);
$memStart = memory_get_peak_usage(true);
$fn();
return [
'seconds' => microtime(true) - $start,
'peakMemoryBytes' => memory_get_peak_usage(true) - $memStart,
];
}
// Model objects
$objects = measure(function () {
$rows = Robot::find();
foreach ($rows as $row) { _ = $row->name; }
});
// Plain arrays
$rows = Robot::find();
$rows->setHydrateMode(Resultset::HYDRATE_ARRAYS); // verify constant
$arrays = measure(function () use ($rows) {
foreach ($rows as $row) { _ = $row['name']; }
});
var_dump($objects, $arrays);
Expected check: array mode should show lower peak memory on large datasets. Execution time varies; measure rather than assume.
4. Confirm capability loss
<?php
$robot = Robot::findFirst(); // model object
echo $robot->related->name ?? 'no relationship'; // may work
$rows = Robot::find();
$rows->setHydrateMode(Resultset::HYDRATE_ARRAYS);
foreach ($rows as $row) {
// $row->related will raise an error; use $row['related_id'] if present
}
Limitations
- Array rows cannot traverse relationships lazily. If your template or service expects
$row->items, switch back to model objects or eager-load and flatten the data yourself. - Memory savings depend on dataset width and row count. A small resultset may show no meaningful difference.
- Changing hydration mode is a code change. Review templates and service layers that consume the resultset before deploying.
Practical check
After deploying, monitor peak memory on the affected endpoint and confirm that reports or exports still produce the expected columns. If a template breaks with “Cannot use object as array” or “Undefined property,” the consumer expects model objects; revert that query to the default hydration mode.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.