Download the PHP package bowlofsoup/couchbase-access-layer without Composer

On this page you can find all versions of the php package bowlofsoup/couchbase-access-layer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package couchbase-access-layer

Minimum PHP Version Build Status Coverage Status

Installation

composer require bowlofsoup/couchbase-access-layer

Couchbase Access Layer

A simple layer on top of the PHP Couchbase SDK. Basically you get a bucket repository class which acts as a layer between your code and Couchbase.

The repository helps you to:

Usage

Do use Parameters

Important: When building a query, always try to use parameters.

Incorrect:

$queryBuilder
    ->where('data.creationDate >= ' . $creationDate);

Correct:

$queryBuilder
    ->where('data.creationDate >= $creationDate')
    ->setParameter('creationDate', '2019-01-10');

This will prevent injection.

The query builder supports the following N1QL clauses

Documentation for clauses can be found On the Couchbase site.

Examples

<?php

declare(strict_types=1);

namespace Some\Name\Space;

use BowlOfSoup\CouchbaseAccessLayer\Exception\CouchbaseQueryException;
use BowlOfSoup\CouchbaseAccessLayer\Factory\ClusterFactory;
use BowlOfSoup\CouchbaseAccessLayer\Model\Result;
use BowlOfSoup\CouchbaseAccessLayer\Repository\BucketRepository;

class Foo
{
    public function exampleUsingTheQueryBuilder()
    {
        $result = $this->querySomeBucket();

        foreach ($result as $item) {
            // each $item contains a Couchbase document which matched the query.
        }

        // $result implements the \JsonSerializableInterface.
        $jsonEncoded = json_encode($result);

        // These (can) only differ when query with limit/offset is done.
        $resultCount = $result->getCount();
        $resultTotalCount = $result->getTotalCount();

        // Return just the data you queried
        $justTheData = $result->get();

        // Get only one (or the first) result
        $singleResult = $result->getFirstResult();
    }

    public function exampleUsingDirectCalls()
    {
        $clusterFactory = new ClusterFactory('couchbaseHost', 'couchbaseUsername', 'couchbasePassword');
        $bucketRepository = new BucketRepository('someBucketName', $clusterFactory);

        $bucketRepository->upsert('some document id', 'the content of the document');

        $bucketRepository->remove('some document id);

        $documentContent = $bucketRepository->getByKey('some document id');
    }

    public function exampleExecutingManualQuery()
    {
        $clusterFactory = new ClusterFactory('couchbaseHost', 'couchbaseUsername', 'couchbasePassword');
        $bucketRepository = new BucketRepository('someBucketName', $clusterFactory);

        // Creating an index for a bucket
        $bucketRepository->executeQuery(
            CREATE INDEX i_foo_field_name
            ON `' . $bucketRepository->getBucketName() . '`(`field`, `data.name`)
            WHERE (`documentType` = "foo")
        );

        $result = $bucketRepository->executeQuery(
            'SELECT someField FROM `bucket name` WHERE someOtherField = $someOtherField',
            ['someOtherField' => 'some value']
        );

        // This will only return one result.
        $result = $bucketRepository->executeQueryWithOneResult();
    }

    /**
     * @return \BowlOfSoup\CouchbaseAccessLayer\Model\Result
     */
    private function querySomeBucket(): Result
    {
        $clusterFactory = new ClusterFactory('couchbaseHost', 'couchbaseUsername', 'couchbasePassword');
        $bucketRepository = new BucketRepository('someBucketName', $clusterFactory);

        $queryBuilder = $bucketRepository->createQueryBuilder();

        $queryBuilder
            ->select('data.name')
            // See that you can put in your own logic, like COUNT and such:
            ->select('COUNT(data.name) AS count')
            ->where('foo = $foo')
            ->where('someField = $someField')
            ->where('type = $type');

        $queryBuilder->setParameters([
            'foo' => 'someFooValue',
            'someKey' => 'someKeyValue',
            'type' => 'someDocumentType',
        ]);

        $queryBuilder
            ->where('data.creationDate >= $creationDate')
            ->setParameter('creationDate', '2019-01-10');

        $queryBuilder
            ->groupBy('data.name')
            ->limit(10)
            ->offset(20);

        try {
            return $bucketRepository->getResult($queryBuilder);
        } catch (CouchbaseQueryException $e) {
            // Something went wrong.
        }
    }
}

For a WHERE clause you can put in logic yourself:

$queryBuilder->where('CONTAINS(SUBSTR(name,0,1),"C")');

For a GROUP BY clause you can put in logic yourself too:

$queryBuilder->groupBy('city LETTING MinimumThingsToSee = 400 HAVING COUNT(DISTINCT name) > MinimumThingsToSee');

Using a sub query in a FROM statement

When using a sub select in a FROM statement with $queryBuilder->fromSubQuery() and you're using parameters, put the parameters in the 'master' query builder. More info on this page.

$queryBuilderForSubSelect = new QueryBuilder('bucket_name');
$queryBuilderForSubSelect->where('type = $foo');

$queryBuilder = new QueryBuilder('bucket_name');

$queryBuilder
    ->select('q1.someFieldOfTheSubSelect')
    ->fromSubQuery($queryBuilderForSubSelect, 'q1')
    ->setParameter('foo', 'value1');

Unit tests

In a previous version, the tests used a dummy couchbase instance called CouchbaseMock.jar. This is not compatible with recent Couchbase versions. Therefore, the current situation has a Docker setup with an actual Couchbase instance for the tests. Assuming you have Docker installed, you can run the tests by running the following commands:


All versions of couchbase-access-layer with dependencies

PHP Build Version
Package Version
Requires ext-couchbase Version *
ext-json Version *
php Version ^8.3
couchbase/couchbase Version ^4.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bowlofsoup/couchbase-access-layer contains the following files

Loading the files please wait ....