Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Db Laravel Package

zendframework/zend-db

Zend\Db is a database abstraction layer for PHP, offering SQL builders, adapters, platform-specific quoting, and result set utilities. It supports multiple drivers and helps you write portable, secure queries while keeping low-level control when you need it.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require zendframework/zend-db. Next, configure an Adapter—typically by providing a configuration array (or using Zend\Db\Adapter\Adapter::factory() with a DSN, credentials, and driver options). For immediate value, build a simple SELECT query using Zend\Db\Sql\Sql and execute it:

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

$adapter = new Adapter([
    'driver' => 'Pdo_Mysql',
    'hostname' => 'localhost',
    'database' => 'test',
    'username' => 'root',
    'password' => '',
]);

$sql = new Sql($adapter);
$select = $sql->select()->from('users')->where(['id' => 1]);
$stmt = $select->prepareStatement();
$result = $stmt->execute()->current();

Consult the docs/ folder (if included) or the archived ZF2 docs—they remain accurate for usage despite the package’s archived status. The first real-world use case is often replacing raw PDO/MySQLi calls with portable query builders.

Implementation Patterns

  • Query Builder + Hydration: Use Sql::select() to build portable queries, then hydrate results into DTOs via Zend\Db\ResultSet\HydratingResultSet and a Zend\Hydrator (e.g., ArraySerializable or custom). This avoids magic arrays and ensures type safety.
  • Component-Level Abstraction: Wrap Adapter in a repository/service class to centralize DB access—e.g., getUserById($id) returns a hydrated user object, hiding SQL construction.
  • Platform-Aware SQL: Use Zend\Db\Sql\Platform\PlatformInterface (e.g., Zend\Db\Sql\Platform\MySQLPlatform) to inject vendor-specific behavior—like handling LIMIT/OFFSET in MySQL vs PostgreSQL—without conditional branching.
  • Metadata & Schema Tools: Use Zend\Db\Metadata\Metadata to introspect tables/columns at runtime (e.g., for admin panels or migration scaffolding).
  • Async/Deferred Execution: Combine with event managers (via Adapter::getDriver()->getConnection()->EVENT_AFTER_EXECUTE) to log queries or inject profiling hooks.

Gotchas and Tips

  • No Active Maintenance: This package is archived (as of 2023), and its successor is part of Laminas (formerly ZF). For new projects, prefer laminas/laminas-db instead—it’s a drop-in replacement with identical API.
  • Parameter Binding Pitfalls: While where(['col' => $val]) auto-binds safely, avoid concatenating values (e.g., where("col = '$val'"))—it bypasses protection. Use where->expression() only with positional placeholders and bound params.
  • Driver-Specific SQL: The platform layer handles common divergences, but complex queries (e.g., recursive CTEs, JSON operators) may need RawExpression and careful vendor detection. Test across target DBs.
  • Connection Reuse: Reuse a single Adapter instance per request—Adapter handles internal connection pooling and state. Avoid re-instantiating it in loops.
  • Debugging: Enable Adapter::getDriver()->getConnection()->getDriverOptions()['profiling'] = true or use Zend\Db\Adapter\Platform\getSqlStringForQueryObject() to print final SQL strings.
  • Extensibility: Extend Zend\Db\Sql\Sql to add custom clauses (e.g., withCte()) or override Zend\Db\Sql\PreparableSqlEngine for non-standard query formatting.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4