Giter VIP home page Giter VIP logo

Comments (8)

yajra avatar yajra commented on July 17, 2024

Due to several times this question were raised to me, I've decided to explore the possibility of stand alone usage and so here we go: ^_^

  • add "yajra/laravel-oci8": "1.*" on your composer then run composer install
  • create database.php and add the code below
require 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;
use yajra\Oci8\Connectors\OracleConnector;
use yajra\Oci8\Oci8Connection;

$capsule = new Capsule;

$manager = $capsule->getDatabaseManager();
$manager->extend('oracle', function($config)
{
    $connector = new OracleConnector();
    $connection = $connector->connect($config);
    $db = new Oci8Connection($connection, $config["database"], $config["prefix"]);
    // set oracle date format to match PHP's date
    $db->setDateFormat('YYYY-MM-DD HH24:MI:SS');
    return $db;
});

$capsule->addConnection(array(
    'driver'   => 'oracle',
    'host'     => 'oracle.host',
    'database' => 'xe',
    'username' => 'user',
    'password' => 'password',
    'prefix'   => '',
    'port'  => 1521
));

$capsule->bootEloquent();
  • Now we can start working with database tables just like we would if we were using Laravel!
include 'database.php';

// Create the User model
class User extends Illuminate\Database\Eloquent\Model {
    public $timestamps = false;
}

// Grab a user with an id of 1
$user = User::find(1);

echo $user->toJson(); die();

Thanks to http://www.edzynda.com/use-laravels-eloquent-orm-outside-of-laravel/ for the idea on how to implement this.

Please provide a feedback if this works and will add the snippet on read me. Thanks!

from laravel-oci8.

aGrmbl avatar aGrmbl commented on July 17, 2024

Hey, thanks for the answer!

I'm getting null $instance inside Manager functions: "Call to a member function connection() on null @ ­illuminate/­database/­Illuminate/­Database/­Capsule/­Manager.php:85".

Here's test code I use:

// Check that we have working DB
$conn = oci_pconnect('zq', 'zq', '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))');
oci_close($conn);

// Try to do the magic
use Illuminate\Database\Capsule\Manager as Capsule;
use yajra\Oci8\Connectors\OracleConnector;
use yajra\Oci8\Oci8Connection;

$DB = new Capsule;

$DB->getDatabaseManager()->extend('oracle', function ($config) {
    $connection = (new OracleConnector)->connect($config);
    $db = new Oci8Connection($connection, $config["database"], $config["prefix"]);
    $db->setDateFormat('YYYY-MM-DD HH24:MI:SS');

    return $db;
});

$DB->addConnection(array(
    'driver' => 'oracle',
    'host' => 'localhost',
    'database' => 'orcl',
    'service_name' => 'orcl',
    'username' => 'zq',
    'password' => 'zq',
    'prefix' => '',
    'port' => 1521
));

$DB->bootEloquent();

$row = $DB->table('dual')->first();

print_r($row);

It's worth noting that IDEA for some reason complains that there's no "extend" method as well.

from laravel-oci8.

yajra avatar yajra commented on July 17, 2024

The issue is on $DB->table() try using $row = $DB->getConnection()->table('dual')->first(); instead.

However, the weird part is that the default connection is not loaded. Will check on that part or maybe it's just the way it works when accessing DB directly? Not really sure atm coz i haven't tried using the package outside Laravel.

from laravel-oci8.

yajra avatar yajra commented on July 17, 2024

reviewing Laravel's API and confirmed that you indeed need to get the connection. Your $DB variable is just a Capsule and not an actual connection.

from laravel-oci8.

aGrmbl avatar aGrmbl commented on July 17, 2024

Thanks a lot! I was able to get it to connect and run test query.

From the initial benchmarks it looks though that for some reason it is horrendously slow (or maybe it's me not used to big fancy libraries?). Like 50 reqs/sec for a simple select, as compared to 500+ reqs/sec for oci_parse/execute/fetch. I've hard-coded pconnect just to be sure.

from laravel-oci8.

yajra avatar yajra commented on July 17, 2024

I would agree that it was slow due to "big fancy libraries" and the package also performs additional queries even for simple select which are required for this to work on Laravel.

Anyways, glad I was able help and to know that this package works as stand alone. ^_^

from laravel-oci8.

ruolinn avatar ruolinn commented on July 17, 2024
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

from laravel-oci8.

yajra avatar yajra commented on July 17, 2024

thanks!

from laravel-oci8.

Related Issues (20)

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.