sachoo/phalcon-cassandra

Cassandra library for Phalcon

1.3.2 2018-02-21 12:51 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:41:15 UTC


README

status

Requirements

Cassandra php driver (https://github.com/datastax/php-driver) hasn't appropriate version number. The only version number available is for C/C++ driver version (wich is not the version of the extension itself). Because of it, version of this library follow the cassandra php-driver real version.

If you need to use cassandra php-driver v1.2.2, please install our library at 1.2.2 Follow the exact same logic for other available versions.

Installing via Composer

Install composer in a common location or in your project:

curl -s http://getcomposer.org/installer | php

Create the composer.json file as follows:

{
    "require-dev": {
        "sachoo/phalcon-cassandra": "1.3.0"
    }
}

Run the composer installer:

php composer.phar install

How to use

First of all you have to add and define some conf in your phalcon config file

return new \Phalcon\Config([
    'cassandra' => [
        'adapter' => 'Cassandra',
        'hosts' => [ 'localhost'],
        'keyspace' => 'lobs',
        'consistency' => 'ONE', // Infos here \cassandra\ConsistencyLevel
        'retryPolicies' => 'DefaultPolicy',
    ]
]);

Then, you have to declare Cassandra service in your dependency injector :

/**
 * This service returns a Cassandra database
 */
$di->setShared('dbCassandra', function () {
    $config = $this->getConfig();
    $connection = new \Phalcon\Db\Adapter\Cassandra($config->cassandra->toArray());
    return $connection;
});

IMPORTANT, if you are using mysql too, you have to change the name of mysql service in dbMysql. For exemple:

$di->setShared('dbMysql', function () {
    $config = $this->getConfig();
    $params = [
        'host' => $config->mysql->host,
        'port' => isset($config->mysql->port) ? $config->mysql->port : 3306,
        'username' => $config->mysql->username,
        'password' => $config->mysql->password,
        'dbname' => $config->mysql->dbname,
        'charset' => $config->mysql->charset
    ];
    return new \Phalcon\Db\Adapter\Pdo\Mysql($params);
});

Then, you just have to declare the correct database service to your models in @Source annotation. For exemple:

<?php
namespace Models\Cassandra;

/**
 * Class Article
 * @package Models\Cassandra
 *
 * @Source('dbCassandra', 'article')
 */
class Article extends \Phalcon\Mvc\CassandraModel
{
    /**
     * @Primary
     * @Column(type="uuid", nullable=false)
     */
    public $uuid;

    /**
     * @Column(type="varchar", nullable=false)
     */
    public $title;

    /**
     * @Column(type="varchar", nullable=false)
     */
    public $description;

    /**
     * list<uuid>
     * uuid of linked articles
     * @Column(type="list", nullable=true)
     */
    public $links;

    /**
     * @Column(type="float", nullable=true)
     */
    public $lat;

    /**
     * @Column(type="float", nullable=true)
     */
    public $lon;

    /**
     * @Column(type="int", nullable=true)
     */
    public $radius;

    /**
     * @Column(type="varchar", nullable=false)
     */
    public $status;

    /**
     * @Column(type="varchar", nullable=false)
     */
    public $type;

    /**
     * @Column(type="varchar", nullable=false)
     */
    public $language;
}