Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
laminas/laminas-db
is a lightweight database abstraction layer used within Laminas (formerly Zend Framework). It provides tools to interact with SQL databases in a flexible and structured way without using a full ORM like Doctrine.
laminas-db
?
Feature |
Description |
---|---|
Lightweight |
Doesn’t come with the overhead of a full ORM. |
SQL Abstraction |
Helps you write platform-independent SQL. |
TableGateway, ResultSet, SQL |
Structured components for querying, inserting, updating, etc. |
Integration |
Easily integrates with Laminas MVC or standalone apps. |
Make sure you have Laminas DB component installed:
composer require laminas/laminas-db
Add database config
use Laminas\Db\Adapter\Adapter; use Laminas\Db\Adapter\AdapterServiceFactory; return [ 'db' => [ 'driver' => 'Pdo_Mysql', 'hostname' => '127.0.0.1', 'database' => 'zend', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', ], 'service_manager' => [ 'factories' => [ Adapter::class => AdapterServiceFactory::class, ], ], ];
Replace your_database_name
, your_db_user
, and your_db_password
with actual credentials.
'controllers' => [ 'factories' => [ Controller\YourController::class => function($container) { $dbAdapter = $container->get(Adapter::class); return new Controller\YourController($dbAdapter); }, ], ],
In your controller constructor:
use Laminas\Db\Adapter\Adapter; class YourController extends AbstractActionController { private $dbAdapter; public function __construct(Adapter $dbAdapter) { $this->dbAdapter = $dbAdapter; } }
You can use Adapter
directly to run raw SQL:
public function indexAction() { $sql = 'SELECT * FROM users WHERE id > ?'; $result = $this->dbAdapter->query($sql, [0]); foreach ($result as $row) { print_r($row); // Output row as array } return false; // To stop rendering }