GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Cycle ORM
  • GitHub
  • Documentation
  • Wippy.ai
  • Spiral Framework
  • RoadRunner

This view

  • Original page
  • New address

GeezerVison is showing cycle-orm.dev — Cycle ORM: Query Builder - Basics

  • Table of Contents
  • About Cycle ORM
  • Installation
  • Integrations
  • Upgrade Guide
  • Contributing
  • Issue with Test Case
  • Versioning
  • LICENSE
  • Create, Update, Delete
  • Direct Database Queries
  • Select
  • Custom Repositories
  • Simple Relation
  • Without Annotations
  • Prerequisites
  • Entities
  • Embeddings
  • Relations
  • Has One
  • Has Many
  • Belongs To
  • Refers To
  • Many To Many
  • Embedding Entities
  • Morphed Relations
  • Collections
  • Bulk Loading
  • Basics
  • Extended Builder Usage
  • Querying Relations
  • Complex Queries
  • Security
  • Install
  • UUID
  • Auto Timestamps
  • Soft delete
  • Optimistic lock
  • Hooks
  • Event listener
  • Extensions
  • Events
  • Configuring Schema Builder
  • Manually defined Mapping Schema
  • Dynamic Mapping Schema
  • Column wrappers and typecast
  • DateTime properties
  • Composite PK
  • Installation and Configuration
  • Connect to Database
  • Access Database
  • Profiling and Logging
  • Database Isolation
  • Query Builders
  • Transactions
  • Schema Introspection
  • Schema Declaration
  • Migrations
  • Synchronizing Database Schema (Migrations)
  • Errata
  • About
  • Proxy Mapper
  • Classless Mapper
  • Std Mapper
  • Promise Mapper
  • Mapper extending
  • References
  • Typecasting
  • Customize Schema Defaults
  • Scopes
  • Single Table Inheritance
  • Joined Table Inheritance
  • Caching
  • Pagination
  • Chained Repository
  • Persist Repositories
  • Active Record
  • Schema
  • Entity, Heap, Node, State
  • Entity manager (Unit of Work)
  • Entity Iterator
  • Node Parser
  • Long-Running Applications
  • Commands
  • Testing
  • Limitations
  • Introduction
  • Installation
  • Active Entities
  • Active Query
  • Active Repository
Revision: Wed, 04 Nov 2026 16:26:00 GMT

Query Builder Basics

  • Accessing Query Builder
  • Simple conditions
  • Short Notation
  • Using Parameters
  • Sorting and pagination

The ORM provides control over generated SQL statements and functionality to set custom conditions and parameters.

#Accessing Query Builder

You can get access to the query builder manually by constructing an instance of the Select object, or by using the select() method of the default repository class.

php
$select = $orm->getRepository(User::class)->select();

// ...

The recommended approach is to declare select statements within an entity's repository:

php
use Cycle\ORM\Select;

class UserRepository extends Select\Repository
{
    public function findActive(): Select
    {
        return $this->select()->where('status', 'active');
    }
}

#Simple conditions

You can set any condition on the obtained query builder using the method where:

php
$select->where('status', 'active');

By default, such condition will generate statement like 'status' = "active" (value will be passed as part of the prepared statement).

To specify a custom operator call the function with 3 arguments:

php
$select->where('balance', '>', 100);

For between and not between conditions you can use notation with 4 arguments:

php
$select->where('id', 'between', 10, 20);

You can use orWhere and andWhere (identical to where) to declare more complex conditions:

php
$select->where('balance', '<', 100)
       ->orWhere('status', 'blocked');

Read more about complex conditions in the next section.

#Short Notation

You can also specify conditions using array notation:

php
$select->where([
    'id' => ['in' => new \Cycle\Database\Injection\Parameter([1, 2, 3])],
    'status' => ['like' => 'active']
]);

This declaration is identical to:

php
$select->where(function(\Cycle\ORM\Select\QueryBuilder $select) {
    $select->where('id', 'in', new \Cycle\Database\Injection\Parameter([1, 2, 3]));
    $select->where('status', 'like', 'active');
});

Array notation can be used to declare multiple conditions on one field:

php
$select->where([
    'id' => ['>' => 10, '<' => 100]
]);

#Using Parameters

By default, any passed value will be converted into Parameter objects internally. However, you must explicitly use Parameter while specifying array values:

php
$select->where('id', 'in', new \Cycle\Database\Injection\Parameter([1,2,3]));

Parameters can be used to specify a value after building the query:

php
$select->where('id', $id = new \Cycle\Database\Injection\Parameter(null));

$id->setValue(10);

print_r($select->fetchAll());

#Sorting and pagination

Use the methods offset, limit and orderBy to paginate or sort your entities:

php
$select->orderBy('id', 'DESC')->limit(1);
NEXT → Extended Builder Usage
2.x
2.x
1.x
Edit this page
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.