Code Monkey home page Code Monkey logo

cassandrapdo4doctrine's Introduction

CassandraPDO4Doctrine

Introduction

CassandraPDO4Doctrine is the Cassandra driver for Doctrine2. It extends Doctrine2 PDOConnection, using YACassandraPDO driver (https://github.com/Orange-OpenSource/YACassandraPDO).

Installation

  • Download Apache Cassandra PDO and compile into a PHP extension.
  • After cloning and building, make sure the pdo extension is enabled by adding this line to your cli php.ini file (usually /etc/php5/cli/php.ini):
extension=pdo_cassandra.so
  • Set up your Symfony2 project (or any PHP framework that uses Doctrine2) and copy the files in CassandraPDO4Doctrine directory into the coresponding folders.
New files:
vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOCassandra/Driver.php
vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/CassandraPlatform.php
vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/Keywords/CassandraKeywords.php
vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/CassandraSchemaManager.php
vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOCassandra/CassandraConnection.php 

Modified files:
vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php (added pdo_cassandra)
vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php (getDateStringFromHex for timestamp type from PDO)
vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/FloatType.php (override getBindingType())

Usage

  • Create a KEYSPACE and COLUMNFAMILY in your Cassandra for testing (use cqlsh or another tool that you prefer). Below is the stuff used for my test
CREATE KEYSPACE mydb WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
USE mydb;

CREATE TABLE product (name varchar,price float,description varchar,created timestamp, 
	PRIMARY KEY (name,created,price)) 
	WITH CLUSTERING ORDER BY (created DESC);
INSERT INTO product (name, price, description, created) VALUES ('prod1', 1.00,'prod #1 desc',dateof(now()));
INSERT INTO product (name, price, description, created) VALUES ('prod2', 2.00,'prod #2 desc',dateof(now()));
INSERT INTO product (name, price, description, created) VALUES ('prod3', 3.00,'prod #3 desc',dateof(now()));
  • Config your DB connection for Cassandra. E.g.
parameters:
    database_driver:   pdo_cassandra
    database_host:     127.0.0.1
    database_port:     9160
    database_name:     mydb
    database_user:     mydbuser
    database_password: mypass
  • Tests All tests below are conducted on Symfony2 enviroment.

    • Create a new record
$product = new Product();
$product->setName('A Foo Bar 1');
$product->setPrice(1.12);
$product->setDescription('blah blah blah');
$product->setCreated();
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
  • Find records using repo object
$repo = $this->getDoctrine()
            ->getRepository('MyBundle:Product');
$products = $repo->findBy( 
                array('name' => 'A Foo Bar 1'),
                array('created' => 'ASC')));
  • Find records using DQL
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT p FROM MyBundle:Product p");
$products = $query->getResult();
  • Find records using Doctrine's Query Builder
$repo = $this->getDoctrine()
            ->getRepository('MyBundle:Product');
$query = $repo->createQueryBuilder('p')
  ->where('p.price > :price')
  ->andWhere('p.name =:name')
  ->andWhere('p.created =:created')
  ->setParameter('price', 19.99,'float')//must specify type explicitly
  ->setParameter('name', 'A Foo Bar 1')
  ->setParameter('created', '2014-12-10')
  ->orderBy('p.created', 'ASC')
  ->getQuery();
$products = $query->getResult();
  • Find records using Native SQL (i.e. CQL)
$em = $this->getDoctrine()->getManager();
$rsm = new ResultSetMapping();
//build rsm
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('price', 'price');

$query = $em->createNativeQuery('SELECT * FROM product WHERE name = ?', $rsm);
$query->setParameter(1, 'A Foo Bar 1');  
var_dump($query->getResult());
  • Counting
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT count(p.name) FROM MyBundle:Product p WHERE p.name= 'A Foo Bar 1' ORDER BY p.created ASC");
$cnt = $query->getResult();

cassandrapdo4doctrine's People

Contributors

balalaikaboy avatar

Watchers

Sergey Nagaytsev avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.