PHPackages                             somnambulist/cte-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. somnambulist/cte-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

somnambulist/cte-builder
========================

Library for programmatically making CTE SQL queries (WITH queries)

3.4.0(2y ago)2744.9k↓54.4%2[1 issues](https://github.com/somnambulist-tech/cte-builder/issues)MITPHPPHP &gt;=8.1CI failing

Since Aug 15Pushed 2y ago5 watchersCompare

[ Source](https://github.com/somnambulist-tech/cte-builder)[ Packagist](https://packagist.org/packages/somnambulist/cte-builder)[ RSS](/packages/somnambulist-cte-builder/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (8)Versions (10)Used By (0)

Common Table Expression Builder
===============================

[](#common-table-expression-builder)

[![GitHub Actions Build Status](https://camo.githubusercontent.com/e5eb926911f912182d7f83688b301db8be9bc589cd025b1a8c4875fbaaf5f12d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f6d6e616d62756c6973742d746563682f6374652d6275696c6465722f74657374732e796d6c3f6c6f676f3d676974687562266272616e63683d6d6173746572)](https://github.com/somnambulist-tech/cte-builder/actions?query=workflow%3Atests)[![Issues](https://camo.githubusercontent.com/a7663111b35d199585c757cc52e16a3a8cc20962be18b2e5be3e5aaba062416f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f736f6d6e616d62756c6973742d746563682f6374652d6275696c6465723f6c6f676f3d676974687562)](https://github.com/somnambulist-tech/cte-builder/issues)[![License](https://camo.githubusercontent.com/97065a1adbb91a1f512805981185c4415fd7b4a96217adf20634fab242a17203/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736f6d6e616d62756c6973742d746563682f6374652d6275696c6465723f6c6f676f3d676974687562)](https://github.com/somnambulist-tech/cte-builder/blob/master/LICENSE)[![PHP Version](https://camo.githubusercontent.com/53539839aafc629b113f22f15a1e2a9f03e83af3201d744c0598a3849671276b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736f6d6e616d62756c6973742f6374652d6275696c6465723f6c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/somnambulist/cte-builder)[![Current Version](https://camo.githubusercontent.com/41f986a4017b58d240d52fdf01e8bc7b3058d39fa291391e98170d18ee7f9629/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6d6e616d62756c6973742f6374652d6275696c6465723f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/somnambulist/cte-builder)

Provides a programmatic interface via Doctrine DBAL Query Builder for creating Common Table Expressions (WITH clauses). Includes support for CTE dependencies and debugging. A CTE allows extracting a sub-select or derived query that can be executed separately to the main query, but then referenced again. Depending on the database server being used, there may be significant performance advantages to this approach, for example: in Postgres a CTE is evaluated once and the same result used no matter how many times it is referenced.

CTEs can also be used to pre-generate content that is needed multiple times, ensuring that any significant computational cost is only incurred once.

Be sure to read up on CTEs and WITH clauses for your chosen database server.

Requirements
------------

[](#requirements)

- PHP 8.0+
- doctrine/dbal
- somnambulist/collection

Installation
------------

[](#installation)

Install using composer, or checkout / pull the files from github.com.

- composer require somnambulist/cte-builder

**Note:** 3.2.0 changes the return type on the main execute method for compatibility with doctrine/dbal. This method now returns the `Doctrine\DBAL\Result` object and not a `Statement` object.

Usage
-----

[](#usage)

CTE Builder consists of the ExpressionBuilder and the Expression. Expressions are created either directly and bound to the builder, or via the builder. The builder requires:

- DBAL Connection

If using Symfony the default configured connection can be used.

```
