A PHP client driver for the RethinkDB query language (ReQL).

Last update: Jun 6, 2022

PHP-RQL

A PHP client driver for the RethinkDB query language (ReQL).

PHP-RQL is licensed under the terms of the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0

Continuous Integration

Master branch: master branch

Development branch: dev branch

To run the tests at the command line, issue composer install and then composer test at the package root. This requires composer to be available in $PATH.

Documentation

Read the PHP-RQL API documentation.

The official JavaScript driver documentation has more details on the available terms. Most examples for the JavaScript driver can be translated to PHP-RQL with few changes.

Example

tableCreate("tablePhpTest")->run($conn); // Insert a document $document = array('someKey' => 'someValue'); $result = r\table("tablePhpTest")->insert($document) ->run($conn); echo "Insert: $result\n"; // How many documents are in the table? $result = r\table("tablePhpTest")->count()->run($conn); echo "Count: $result\n"; // List the someKey values of the documents in the table // (using a mapping-function) $result = r\table("tablePhpTest")->map(function($x) { return $x('someKey'); })->run($conn); foreach ($result as $doc) { print_r($doc); } // Delete the test table r\db("test")->tableDrop("tablePhpTest")->run($conn); ?>">

    // Load the driver
    require_once("rdb/rdb.php");

    // Connect to localhost
    $conn = r\connect('localhost');

    // Create a test table
    r\db("test")->tableCreate("tablePhpTest")->run($conn);

    // Insert a document
    $document = array('someKey' => 'someValue');
    $result = r\table("tablePhpTest")->insert($document)
        ->run($conn);
    echo "Insert: $result\n";

    // How many documents are in the table?
    $result = r\table("tablePhpTest")->count()->run($conn);
    echo "Count: $result\n";

    // List the someKey values of the documents in the table
    // (using a mapping-function)
    $result = r\table("tablePhpTest")->map(function($x) {
            return $x('someKey');
        })->run($conn);

    foreach ($result as $doc) {
        print_r($doc);
    }

    // Delete the test table
    r\db("test")->tableDrop("tablePhpTest")->run($conn);
?>

Release Notes

...are available on the main website: http://php-rql.dnsalias.net

Attributions

GitHub

https://github.com/danielmewes/php-rql
Comments
  • 1. anonymous function in replace() fails with "Expected type OBJECT but found NULL"

    The following code seems to fail with:

    Runtime error: Expected type OBJECT but found NULL.

    r\table($table)->get($id)->replace( 
            function($doc) use ($id,$request) {
                return $doc->rDefault(array('id' => $id))->merge(array('test' => $request));
            }
        )->run($conn);
    

    Judging from the stack trace below, it seems to be because of the check in the return function. I'm unsure on how to debug this further.

    #0 /lib/php/rdb/connection.php(157): r\Connection->checkResponse(Object(r\pb\Response), 418578082, Object(r\Replace))
    #1 /lib/php/rdb/connection.php(101): r\Connection->receiveResponse(418578082, Object(r\Replace))
    #2 /lib/php/rdb/misc.php(35): r\Connection->_run(Object(r\Replace), NULL)
    #3 /lib/api/rdb.api.php(112): r\Query->run(Object(r\Connection))
    #4 [internal function]: appendToDB('test', '571e5bbb-dfee-4...')
    

    Any ideas how to make this work? I'm stuck until I can find a work around for this... I would note that both $id and $replace are in the scope of the function and that replacing them with static values throws the same error...

    Reviewed by ckmaresca at 2014-01-15 22:07
  • 2. Use phpunit for tests

    re: https://github.com/danielmewes/php-rql/issues/101, should make troubleshooting and refactoring simpler. Would live to get some CI going on this repo as well to ensure that everything is tested before being merged.

    Also fixes some currently broken tests.

    Reviewed by mbrevda at 2015-09-27 11:31
  • 3. PSR2 + PSR4

    Having a standard way of organizing the code is important in order to allow for devs to be able to understand the code better and hopeful improve (or if necessary, fix) it.

    The PHP FIG has defined coding/interface standards to provide a comman set of standards that can be resued by php dev projects. I have shuffled the code a bit to confirm (mostly) to PSR2 and PSR4. Seeing as it is based off https://github.com/danielmewes/php-rql/pull/105, it will be easier to see the diff if that is merged, first. So while I can gladly open a PR, it would be to large to be useful.

    For the final few issues, it would be good to get some feedback before changing them. The list is here.

    Of concern with a refactor of this scale is backwards compatability. As can be seen from the tests, aside for a few namespace changes, there was virtually nothing that would need to be changed by a consumer with these changes. If the few namespace changes ARE of concern, I belive that are some simple (non pretty) workarounds that can be employed.

    Reviewed by mbrevda at 2015-09-30 11:45
  • 4. unable to use getAll or filter() on rethinkdb 1.10 if result is multiple records

    Hi!

    I have issue when i try to fetch data using : $i = array( 'index' => 'userid' );

                $o = r\table('smsq')->getAll($userid, $i)->run($this->conn);
                $arr = $o->toNative();
    

    $arr contains empty array.

    same thing when i use filter instead of getAll()

    Reviewed by kolcvk at 2013-10-10 11:41
  • 5. changes() doesn't work with ->filter()

    This works:

    r\table("some_table")->changes()->run($conn);
    foreach ($cur as $elem) { var_dump($elem->toNative()); }
    

    This doesn't:

    r\table("some_table")->changes()->filter(r\row('foo')->eq('bar'))->run($conn);
    foreach ($cur as $elem) { var_dump($elem->toNative()); }
    
    Reviewed by sandeepshetty at 2014-12-05 22:00
  • 6. getField be more than just a pure php string

    the title says it all i know u wont merge it because i have some other things in it but just look at the change to datum.php file its a simple one liner.

    Reviewed by wojons at 2014-03-29 06:40
  • 7. maor php-ness

    php-rql is currently written to mimic RethinkDB's javascript api. While this reduces the amount of documentation needed, it greatly ups the learning for php dev's, used to working in a more php centric way. Here are a couple of ideas and suggestiong that would be great to discuss:

    • [x] Proper object oriented-ness: Using a myriad of global(ish) functions and having to pass them low level data all the time creates a high maintenance price on developers. For example: having to pass a db connection or name to every r\table() query requires having this information in some sort of global state. issue largely mitigated with DI

    • [x] DI-able: having a containing object as the starting point for all queries will allow for that object to be configured/instantiated/injected by a dependency injection system. This will, again, lower the overhead of global state that needs to be kept in order to run a query. with #107, a Connection and a Db can be injected and used without any other dependencies

    • [x] Lazy connection: once we are more di-able, it would make more sense to not run $this->connect(); until a connection is actually needed. This would cut down the overhead of server hits where the db isn't actually used (say, where an in-memory cache already provided the data required) #107

    • [x] PSR Compatibility: Things like PSR-1, PSR-2, and PSR-4 make it easier to jump in to the code and understand what going on. It also allows for:

    • [x] Lazy includes: There is no need to load all the code at once. Composer's ubiquitous autoloader can intelligently load just those classes that are needed, as they are needed

    • [x] Saner API: Again recognizing the original goal of uniformity with the js docs, the api seems a bit backwards. For example, which of the following is more self-documenting?

      • $result = r\table("tablePhpTest")->insert($document)->run($conn);
      • vs
      • $result = $rdb->table("tablePhpTest")->insert($document)->run();
      • or perhaps: $result = $rdb->insert($document)->inTo("tablePhpTest")->run();

      or

      • $result = r\table("tablePhpTest")->map(function($x) {return $x('someKey');})->run($conn)
      • vs
      • $result = $rdb->table("tablePhpTest")->map(function($x) {return $x('someKey');})->run() Note: marking this as "done" in the sense that it is being handeled upstream: https://github.com/rethinkdb/rethinkdb/issues/3130
    • [x] Separate query and connection: I also wonder if run() at the end of every query is really necessary. It seems that the "rql" way of doing things is to keep the query and connection parts separate. It might be better to have a Connection object, which gets passed a Query. I.e.:

      • $rdb->run((new Table('foo'))->insert($document)); this is currently implemented by default
    • [x] Proper unit testing: a standard framework, like phpunit, would make it easier to test, and also, refactor done in #105

    None of the above should be seen as criticism to the current project, but rather some suggestions on how the learning curve can be lowered, hopefully leading to greater adoption.

    Reviewed by mbrevda at 2015-09-18 07:14
  • 8. contains() doesn't work with prediction

    I tried to do the same thing with php-rql:

    r.table('marvel').filter(function(hero) {
        return r.expr(['Detroit', 'Chicago', 'Hoboken']).contains(hero('city'))
    }).run(conn, callback)
    

    but it fails with message r\RqlUserError: Runtime error: Cannot perform get_field on a non-object non-sequence '13'.

    (13 is one of the values in r/expr)

    Reviewed by duxet at 2014-12-19 16:59
  • 9. Issues with HHVM?

    I've started using PHP-RQL on my server and seem to be having issues getting it to work with HHVM.

    I am using HHVM-3.3.0 and have this strange error occuring only on HHVM. Switching back to PHP-FPM causes no issues, it only seems to occur on HHVM. Also note I am using the package from composer.

    The issue comes when adding a document, I am using the below code:

    $rdb = r\connect('127.0.0.1');
    $rdb->useDb('test');
    $rdb->setTimeout(30);
        $log = [
            'i' => $i
        ];
    r\table("test")->insert($log)->run($rdb);
    

    With PHP-FPM it's fine, it will insert all 1,000 documents with no issue. With HHVM it inserts 1 or 2 and then fails with a RqlDriverError Exception with message of 'Unable to read from socket. Disconnected.' which is being thrown here in the receiveStr function.

    When I remove the line where the exception is thrown and add in a var_dump(strlen($s)); just after the $s = stream_get_contents($this->socket, $length); I start to notice that on the times it fails, it returns a strlen of 8 rather than the 12 that is needed for the pack function here.

    For some reason the socket is only fetching 8 bytes rather than the 12 needed even though it's asking for 12.

    Now the query will run fine if instead of receiving the 12 bytes here and then unpacking it to get the 2 tokens and the size, it will work fine if I get 8 bytes for the tokens and unpack that and then get the last 4 bytes for the size and combine them together. It's a hacky solution, but that gets it to succeed and do the 1,000 inserts.

    Now I'm not 100% entirely sure whose at fault here, if it's a HHVM issue specifically or if it's something in PHP-RQL, I'm leaning towards HHVM since it's fine in PHP-FPM. I'm mainly putting this here to see if anyone else is using this library with HHVM and has the same issue.

    I've been able to duplicate this on at least 3 different systems running HHVM, hopefully someone out there running HHVM along with this library can shed some light on if it affects them.

    I will continue to do some more testing with different HHVM versions and setups to see if maybe my systems are all setup in some strange way.

    Thanks

    Reviewed by RyanTheAllmighty at 2014-11-16 22:11
  • 10. not a string

    datum.php 384

    it only allows a string what happens if i have an object i had a map that was an object and was not working unless i uncommend this it still looks like it crates the correct reql

    Reviewed by wojons at 2014-03-28 10:47
  • 11. Protobuf issues

    Hi -

    I can't seem to get PHP-RQL to use the Protobuf extension. It's definitely installed as it's appears under "Additional .ini files parsed" in phpinfo, and the .so that it points to is present.

    Additionally, if I run r\systemInfo(), it simply returns:

    Protobuf backend: PHP-RQL Version:

    With blank/NULL values for those global variables. I temporarily edited my rdb.php file to echo the $__PHP_RQL_PROTOBUF_BACKEND and $__PHP_RQL_VERSION which outputs pb4php and 1.12.1 respectively.

    I've restarted nginx and reinstalled/made the protobuf files numerous times but it's made no difference.

    Any clue what the issue might be?

    Thanks!

    Reviewed by sonnybaker at 2014-04-17 10:46
Related tags
Doctrine PHP mapping driver

WORK IN PROGRESS! Doctrine PHP mapping driver Alternative mapping driver that allows to write mappings in PHP. Documentation Associations examples TOD

Aug 15, 2021
Driver to seamlessly integrate the Backup Manager into Laravel applications.

Laravel Driver for the Database Backup Manager This package pulls in the framework agnostic Backup Manager and provides seamless integration with Lara

Jul 19, 2022
Oracle DB driver for Laravel 4|5|6|7|8 via OCI8

Oracle DB driver for Laravel 4|5|6|7|8 via OCI8 Laravel-OCI8 Laravel-OCI8 is an Oracle Database Driver package for Laravel. Laravel-OCI8 is an extensi

Aug 12, 2022
🔌 A Doctrine DBAL Driver implementation on top of Swoole Coroutine PostgreSQL extension

Swoole Coroutine PostgreSQL Doctrine DBAL Driver A Doctrine\DBAL\Driver implementation on top of Swoole\Coroutine\PostgreSQL. Getting started Install

Apr 1, 2022
Laravel Thermite is an extended PostgreSQL Laravel database driver to connect to a CockroachDB cluster.
Laravel Thermite is an extended PostgreSQL Laravel database driver to connect to a CockroachDB cluster.

Laravel Thermite Laravel Thermite is an extended PostgreSQL Laravel database driver to connect to a CockroachDB cluster. ?? Supporting If you are usin

Jul 10, 2022
Laravel 5.1 ODBC Driver

l5-odbc-driver Laravel 5.1 ODBC driver Installation To Install this in your Laravel 5.1 app, open composer.json and add: "require": { "garylocke/odb

Sep 19, 2019
A package for using Google Datastore as a database driver

Laravel Eloquent for Google Datastore A package for using Google Datastore as a database driver. By using this package, you can use query builder and

Apr 26, 2022
Feather - a highly performant SQLite Cache Driver for Kirby 3

?? Kirby3 SQLite Cache-Driver Feather - a highly performant SQLite Cache Driver for Kirby 3 Commerical Usage Support open source! This plugin is free

Dec 15, 2021
Simple MySQL library for PHP 5.4+ includes Query Builder, PDO Native functions, Helper functions for quick use.

Simple MySQL library for PHP 5.4+ includes Query Builder, PDO Native functions, Helper functions for quick use.

Jul 14, 2022
A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.

Idiorm http://j4mie.github.com/idiormandparis/ Feature/API complete Idiorm is now considered to be feature complete as of version 1.5.0. Whilst it wil

Aug 16, 2022
Independent query builders for MySQL, PostgreSQL, SQLite, and Microsoft SQL Server.

Aura.SqlQuery Provides query builders for MySQL, Postgres, SQLite, and Microsoft SQL Server. These builders are independent of any particular database

Aug 3, 2022
A SQL query builder with zero dependencies

Latitude Query Builder A SQL query builder with zero dependencies. Attempts to be PSR-1, PSR-2, and PSR-4 compliant. Install composer require latitude

Aug 4, 2022
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Laravel MongoDB This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library ex

Aug 13, 2022
SQL to Laravel Query Builder

Marwan - SQL To Laravel Builder SQL to Laravel Query Builder, A Converter written in PHP Features Converts SQL Queries to Laravel Query Builder. Assis

Aug 3, 2022
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.

Eloquent Filter Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic. Table of Content Introduct

Aug 13, 2022
Database Abstraction Layer, Schema Introspection, Schema Generation, Query Builders

Cycle DBAL Secure, multiple SQL dialects (MySQL, PostgreSQL, SQLite, SQLServer), schema introspection, schema declaration, smart identifier wrappers,

Jul 13, 2022
Get MYSQL statement from query builder in laravel helper

Get MYSQL statement laravel This package allows to get mysql statement that query builder in laravel made it for debugging purposes. Basic usage Dump

Jul 15, 2022
The query filter bundle allows you to filter data from QueryBuilder and the Database
The query filter bundle allows you to filter data from QueryBuilder and the Database

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

Apr 8, 2022
A simple program to query mysql data and display the queried data in JSON format

A simple program to query mysql data and display the queried data in JSON format. The data displayed in JSON format will change and update as the data in your mysql database changes.

Mar 7, 2022