Database connection with Firebird Adapter in Zend Framework - zend-framework

I just started to learn about ZF and Firebird because of a project I am assigned to. I have been trying to make the connection between both for several days now, but I haven't succeed yet.
I tried ZF with PDO_Mysql and it works just fine as it does Firebird connection with PHP (out of ZF), but when I try to make the connection with the Firebird adapter in ZF it keeps displaying all kinds of errors.
So, just to check. To make the connection in ZF with Firebird it should be done with the the adapter (Firebird.php) which I have configure in application.ini? I have something like this in the application.ini:
**resources.db.adapter = "Firebird"
resources.db.params.host = "localhost"
resources.db.params.dbname = "C:/wamp/www/WebTH/application/data/THDATA.gdb"
resources.db.params.username = "sysdba"
resources.db.params.password = "masterkey"**
Resulting error: ...Firebird.php): failed to open stream: No such file or directory in ...\Loader.php
I have also seen that a function needs to be added in the Bootstrap.php. If I add the function initDb in the bootstrap.php like this:
**protected function _initDb()
{
$this->bootstrap('config');
$config = $this->getResource('config');
$db = Zend_Db::factory('Firebird', array(
'host' => $config->Database->Server,
'username' => $config->Database->Username,
'password' => $config->Database->Password,
'dbname' => $config->Database->DBName,
'adapterNamespace' => 'ZendX_Db_Adapter'
));
return $db;
}**
I get the error: ...Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "config" not found' in ...\BootstrapAbstract.php
I would like to know what do I really need to do in order to make the connection work. Sorry if this is too obvious, but I haven't been able to find a basic connection case specific for Zend Framework and Firebird, therefore I am not really sure on what I should do and where I should go.

[Disclosure: I've never used the Firebird db.]
As you are probably aware, the Firebird adapter does not ship as part of the core ZF1 package. Looks like you can pull it from extras under the ZendX prefix:
http://framework.zend.com/svn/framework/extras/trunk/library/ZendX/Db/Adapter/
Drop the Firebird.php and Firebird/ files into a directory library/ZendX/Db/Adapter/.
If your application.ini uses the resources.db.* keys, then there is no need for an _initDb() method in your Bootstrap class. Just make sure that you include the adapterNamespace key in there, as well:
resources.db.params.adapterNamespace = "ZendX_Db_Adapter_"
You might also need to add the ZendX prefix to your autoloader namespaces:
autoloaderNamespaces[] = "ZendX_"
Not tested, but something like this should work.

Related

Using Jenssegers MongoDB with Slim and Capsule

I am using the Slim 4 framework along with Jenssegers MongoDB library and Capsule (Illuminate\Database from Laravel). I have got the MongoDB extension installed on my Linux server and everything seems ok connection wise, but I cannot seem to insert data into the database or get anything from it. I have tried with the query builder and Eloquent. My code with query builder example is below.
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->getDatabaseManager()->extend('mongodb', function($config, $name) {
$config['name'] = $name;
return new \Jenssegers\Mongodb\Connection($config);
});
$capsule->addConnection([
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'testing',
'username' => '',
'password' => '',
], 'mongodb');
// 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();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$capsule->connection('mongodb')->collection('testing')->insert([
'test1'=>'hello',
'test2'=>'world',
]);
The database and collection exist as I can see them in Compass. Can anyone see where I'm going wrong with the code or is it a configuration issue?
There were two problems, one was the driver missing as you point out, but the other was the fact my PHP is running in Linux using Vagrant and it was pointing to localhost, but the MongoDB server is running on the Windows machine and not Linux. Thanks.

Eloquent innodb - SQLSTATE[42S02]: Base table or view not found Table doesn't exist

I have this weird crash and trying for few hours to solve with no success, check everywhere on the internet and didn't find a solution.
I am running Eloquent (without laravel). I have two servers (frontend and backend). The DB is in the backend server.
I am running the exact same code to connect to the database using laravel from both servers. From the backend server everything works. In the frontend server, I get mixed result. Out of 9 tables:
three works, and I am able to retrieve the data.
three return [items:protected] => Array (), while table has data to return.
three cause fatal error SQLSTATE[42S02]: Base table or view not found Table doesn't exist
from #1 I understand that this is not related to issue with database user/password.
for #2, I checked the queries using getQueryLog(), and I run the queries in phpmyadmin, and they work.
from what I observed so far, all the tables that cause #3 are all InnoDB. Why my other tables are MyIsam I don't know.
And the exact same code works in the backend server, so it is not an issue with tables names are in singular or some other code related issue.
backend server is php 5.5.9, eloquent 5.4.45
frontend server is php 5.6.32, eloquent 5.4.36
Any idea what to do about #2 (return data empty) and #3 (php fatal error)?
Ok, found this crazy bug(s).
First, I found out that we had an old version of the database on the frontend server. It was not supposed to be there, so I didn't even think about it as a possibility.
In addition, there was a problem in the code:
$dbhost = __MYSQLDB_HOST__;
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$dbname = 'xxxxx';
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => 'localhost', // <= this is the problem!!!
'database' => $dbname,
'username' => $dbuser,
'password' => $dbpass,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
As you can see, when connecting to the database it used localhost instead of the $dbhost variable which contained the remote database host on the backend server.

Lumen and MongoDB?

Is it somehow possible to include the mongodb connection settings into a lumen framework. As from what I saw the config/database.php is loaded internally in the lumen package. Is there a way to extend it somehow to include the mongodb connection settings?
We're actually using Lumen, Laravel, Mongo, and MySQL in one giant project so I can help you through this one. Assuming you want to use MongoDB with eloquent instead of with the raw MongoClient. You can find the library I'm using from jenssegers here.
Install MongoDB Extension
Firstly you'll need to install the dependencies for PHP to interact with mongo. The specifics for installing the mongo extension can be found on the PHP documentation.
After that you'll have to edit the php.ini files for the platforms (apache/cli/nginx) to load the extension. I added the following before Module Settings
extension=mongo.so
It goes without saying you need to restart apache/nginx after changing the configuration.
Configuring Lumen
In your root lumen folder you can add it to your requirements with the following command.
composer require jenssegers/mongodb
From there you'll need to also load the MongodbServiceProvider before Facades or Eloquent is initialized.
$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);
$app->withFacades();
$app->withEloquent();
For simplicity of organizing configuration I also created a config folder and a database.php config file. Since Lumen doesn't try to autoload or search this directory we have to tell it to load this config. I put the following line right before the loading the application routes.
$app->configure('database');
In database.php the mongodb driver requires a specific structure. I've included mysql in here as I use both, but if you're using mongo exclusively you can change default to mongodb and remove the mysql config.
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mongodb' => array(
'driver' => 'mongodb',
'host' => env('MONGODB_HOST', 'localhost'),
'port' => env('MONGODB_PORT', 27017),
'username' => env('MONGODB_USERNAME', ''),
'password' => env('MONGODB_PASSWORD', ''),
'database' => env('MONGODB_DATABASE', ''),
'options' => array(
'db' => env('MONGODB_AUTHDATABASE', '') //Sets the auth DB
)
),
],
];
With the configuration out of the way you can now create a model, as of writing this to create a model for mongo (check the github page) you can use the following as a base. You can ignore the $connection variable if mongo is your default driver.
<?php
namespace App;
use Jenssegers\Mongodb\Model as Eloquent;
class Example extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'example';
protected $primaryKey = '_id';
}
There you go, you should be able to interact with mongo normally, for the specifics of the driver check out the github page for documentation on it.
If this answer helped you could you mark it as the answer?
2016 (Update)
There is now a simple Doctrine MongoDB ODM Provider for the Lumen PHP framework.
composer require nordsoftware/lumen-doctrine-mongodb-odm
GitHub Source Code
Warning
jenssegers/mongodb is a Driver sitting on top of Illumante's Eloquent ORM.
Think of it: Eloquent ORM is primary made for SQL. And let's cut with the chase: The package is the reinvention of the wheel - as a side effect, major mongodb features are not supported. Besides that, the package is unstable and unmaintained.
Be aware, jenssegers/mongodb will vent your anger and frustration:
Just a change in #Sieabah user:
instead: extension=mongo.so
choose: extension=mongodb.so

Cakephp 2.0 Mongodb Plugin not loading

I am trying to change my database over to Mongodb. I have Mongo loaded and working from the shell. Now I am trying to get cakephp 2.0 to connect to the database. I have downloaded and installed ichiaway's drive into the app/plugin directory as Mongodb. I have included the line
CakePlugin::load('Mongodb');
in my bootstrap.php file. I have changed my database.php file to
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'mongodb.mongodbSource',
'host' => 'localhost',
'database' => 'service4u',
'port' => 27017
);
The database works as a service right now and I can start the mongo shell and work with the database just fine but when I try and load a page from my app I get the following error.
Missing Plugin
Error: The application is trying to load a file from the mongodb plugin
Error: Make sure your plugin mongodb is in the app\Plugin directory and was loaded
<?php
CakePlugin::load('mongodb');
Loading all plugins: If you wish to load all plugins at once, use the following line in your app\Config\bootstrap.php file
CakePlugin::loadAll();
I am at a loss now on what to do. I have tried putting the files in different folders inside the Mongodb folder in the plugin folder but nothing I do helps. Any insight would be helpful. Thanks.
Was able to find the answer. The only thing I had wrong was in my database.php file the datasource should be camelCase. I had lowercase m's, changed them to M and it worked.
Found the answer here
CakePHP 2 can not find plugin
Make sure downlaod file more then 2.5.if not please
Download mongoDB plugin for cake php.
just download and put he all file on app/plugin/Mongodb.
set database in database.php
public $default = array(
'datasource' => 'Mongodb.MongodbSource',
'host' => 'localhost',
'database' => 'blog',
'port' => 27017,
'prefix' => '',
'persistent' => 'true',
);
public $test = array(
'datasource' => 'Mongodb.MongodbSource',
'database' => 'test_mongo',
'host' => 'localhost',
'port' => 27017,
);
and add blow code on bootstrap.php
CakePlugin::load('Mongodb');

problems using Zend_Db when running php as cli script

I am trying to add some administration scripts to a Zend Framework project, that will end up being run nightly via cron.
However I've hit my first problem with the script when trying to use Zend_Db. I am currently doing a very simple SQL call to get some results and just display them using var_dump() however I get the following error message:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
The script so far looks like this:
<?php
chdir(dirname(__FILE__));
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/London');
//directory setup and class loading
set_include_path('.' . PATH_SEPARATOR . '../library/' . PATH_SEPARATOR . '../application/models/'
. PATH_SEPARATOR . '../library/MyApp/'
. PATH_SEPARATOR . get_include_path());
include "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
//start sessions
Zend_Session::start();
//read configuration data
$config = new Zend_Config_Xml('../application/config/config.xml', 'app');
//we create an ability to capture errors and write them to an error log if there are problems.
$log = new Zend_Log();
$writer = new Zend_Log_Writer_Stream($config->log->logfile);
$log->addWriter($writer);
$filter = new Zend_Log_Filter_Priority((int)$config->log->level);
$log->addFilter($filter);
//we now need to get the list of graduates that need to have their CV's removed from the search engine
$date = new Zend_Date();
$date->sub(3, Zend_Date::MONTH);
echo $date->get(Zend_Date::ISO_8601);
$sql = "SELECT userid, guid FROM users WHERE lastlogin = ? AND active = 1";
$db = Zend_Db::factory($config->database);
try{
$results = $db->fetchAll($sql, $date->get(Zend_Date::ISO_8601));
var_dump($results);
}catch(Zend_Db_Exception $dbe){
$log->debug($dbe->getMessage()."\n".$dbe->getTraceAsString());
}
//close database.
if($db->isConnected()){
$db->closeConnection();
}
What am I missing? I know that the database connection settings in the Zend_Config object works and the web application is running fine with no issues.
I am running this using Zend Framework v1.7.6 at present.
Also are there any generic tips for using the Zend Framework in a cli script?
Many thanks.
PHP will try to connect to mysql via tcp/ip, unless the mysql server is on the same machine as the script and "localhost" is used as the db host. Then it will use a filesystem socket.
It could be that mysql cannot create /tmp/mysql.sock due to permissions, or that it's not configured to do so. The Mysql manual should have more on that.
A quick way to get around this for now should be to change the db host in your Zend_Db settings to an actual ip mysql listens on, not "localhost" or "127.0.0.1".
also check your php.ini and look for the mysql_sock
this is often a trap for Mac users with Mamp installed: Snow Leopard comes with a php configured which is separate to the ph included with Mamp