2017 © Pedro Peláez
 

library database

A Drupal 7 ORM as headless version

image

drupalheadless/database

A Drupal 7 ORM as headless version

  • Sunday, December 18, 2016
  • by juanparati
  • Repository
  • 1 Watchers
  • 2 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

DrupalHeadless

Description

The DrupalHeadless project provides the following components as a compatible PSR-4 package:, (*1)

  • The original Drupal 7 Database Abstraction Layer
  • A replacement for the Entity Field Query

This package was created in order to provide an elegant and suitable way of interact with Drupal 7 databases without the requeriment of install a full Drupal 7 stack. DrupalHeadless can used as with standalone scripts or can be deployed via composer in different solutions like for example Drupal 8., (*2)

How to use the database abstraction layer?

If you are familiar with Drupal 7 Database API, then it is going to be easy for you to use the DrupalHeadless database abstraction layer because it is based in the original abstraction layer., (*3)

Database connection and query

 use \DrupalHeadless\Database\Database;

 // Set connection info
 Database::addConnectionInfo('default', 'default', [
     'driver'    => 'mysql',
     'database'  => 'drupal',
     'username'  => 'root',
     'password'  => 'root',
     'host'      => '127.0.0.1',
     'prefix'    => 'drupal_'
 ]);

 $query = Database::getConnection('default', 'default');

 $st = $query->select('node', 'n');

 $result = $st->fields('n')
     ->range(0, 10)
     ->execute()
     ->fetchAll();

As an alternative it is also possible to use the statement helper:, (*4)

 use \DrupalHeadless\Database\Database;
 use \DrupalHeadless\Database\DatabaseHelper as DH;

 // Set connection info
 Database::addConnectionInfo('default', 'default', [
     'driver'    => 'mysql',
     'database'  => 'drupal',
     'username'  => 'root',
     'password'  => 'root',
     'host'      => '127.0.0.1',
     'prefix'    => 'drupal_'
 ]);


 $result = DH::select('node', 'n')
     ->fields('n')
     ->range(0, 10)
     ->execute()
     ->fetchAllAssoc('nid');

How to use the Entity Field Query?

Select (and retrieve all the fields)

    use \DrupalHeadless\Database\Database;
    use \DrupalHeadless\Entity\EntityController as EC;
    use \DrupalHeadless\Entity\Model;


    // Set connection info
    Database::addConnectionInfo('default', 'default', [
        'driver'    => 'mysql',
        'database'  => 'drupal',
        'username'  => 'root',
        'password'  => 'root',
        'host'      => '127.0.0.1',
        'prefix'    => 'drupal_'
    ]);


    $connection = Database::getConnection('default', 'default');


    // Search the first 5 nodes with bundle "article" that contain the word "Test" into the body field
    $result_node = EC::entity($connection, new Model\Node(), 'article')
        ->load()
        ->select()
        ->fieldCondition('body', 'value', '%Test%', 'LIKE')
        ->range(0, 5)
        ->fetchAll();


    // Search the user with UID = 1
    $result_user = EC::entity($connection, new Model\Users(), 'user')
        ->load()
        ->select()
        ->propertyCondition('uid', 1)
        ->fetch();

Insert

use \DrupalHeadless\Database\Database;
use \DrupalHeadless\Entity\EntityController as EC;
use \DrupalHeadless\Entity\Model;


// Set connection info
Database::addConnectionInfo('default', 'default', [
    'driver'    => 'mysql',
    'database'  => 'drupal',
    'username'  => 'root',
    'password'  => 'root',
    'host'      => '127.0.0.1',
    'prefix'    => 'drupal_'
]);


$connection = Database::getConnection('default', 'default');


$entity = EC::entity($connection, new Model\Node(), 'article')->load();


// Load entity node and bundle "article" which "title" is equal to "This is a test"
$result_node = $entity->propertyCondition('title', 'This is test')->fetch();


// Insert into the custom/dynamic field the value "myvideo" with delta 0
$entity = $entity->insertFieldset('field_videoid', $result_node->nid, ['value' => 'myvideo'], 0);

The Versions

18/12 2016

dev-master

9999999-dev

A Drupal 7 ORM as headless version

  Sources   Download

GPL

The Requires

  • php >=5.5.0

 

The Development Requires

by Juan Lago

orm database php drupal