Php Codeigniter:自动连接到Neo4J数据库
我一直在我的一个个人项目中使用codeigniter 2.x。数据库是MySql,但我决定迁移到Neo4J 我使用了我安装的名为GraphAware的库。到目前为止,它按预期运行。我的测试代码如下:Php Codeigniter:自动连接到Neo4J数据库,php,database,codeigniter,neo4j,graphaware,Php,Database,Codeigniter,Neo4j,Graphaware,我一直在我的一个个人项目中使用codeigniter 2.x。数据库是MySql,但我决定迁移到Neo4J 我使用了我安装的名为GraphAware的库。到目前为止,它按预期运行。我的测试代码如下: $user = 'neo4j'; $password = 'mypass'; $host = 'myhost:7474'; $myDB = 'mydb'; $client = ClientBuilder::create() ->addC
$user = 'neo4j';
$password = 'mypass';
$host = 'myhost:7474';
$myDB = 'mydb';
$client = ClientBuilder::create()
->addConnection('default','http://'.$user.':'.$password.'@'.$host.'/'.$myDB)
->build();
$query = "MATCH (n:user) RETURN n.username";
$result = $client->run($query);
到目前为止还不错
我的问题如下:如何在页面创建时自动连接到neo4j数据库,这样就不必每次都手动创建连接
在我看来,上面的代码会变成这样:
$db = $this->db->load('neo4j');
$query = "MATCH (n:user) RETURN n.username";
$result = $db->run($query);
我一直在codeigniter中搜索,由于缺乏对一些核心概念的理解,我似乎找不到解决方案
你知道如何进行吗
谢谢
Loïc.我的目标是在每次页面加载时自动连接到Neo4J数据库,而不必在每次需要访问数据库时手动连接到数据库。我还担心不修改任何codeigniter系统文件 编辑:更新的解决方案。我的第一个解决方案是有问题的,因为它需要模型调用控制器来获取DB对象。这是不切实际的,并且违反了MVC工作流,在MVC工作流中,应该由控制器调用模型,而不是相反 更新的解决方案: 对于此解决方案,我们需要创建两个方面:
- 新的图书馆课程
- 一个新的控制器类
- 修改配置文件并插入数据库详细信息和凭据
- connect():执行到数据库的连接,并将数据库对象保存在私有类变量$neo4j中
- get_db():返回保存在函数connect()中的数据库对象
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j {
private $neo4j;
//connect to the neo4j database
public function connect(){
//load CI to access config files
$CI = & get_instance();
//Get database details from the config file.
$user = $CI->config->item('username');
$password = $CI->config->item('password');
$host = $CI->config->item('hostname');
$port = $CI->config->item('port');
$database = $CI->config->item('database');
//build connection to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.':'.$port)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Neo4j_controller extends TNK_Controller{
//connect to neo4j db on instantiation
function __construct(){
parent::__construct();
//load the neo4j library that we created
$this->load->library('neo4j');
//connect to the db
$this->neo4j->connect();
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['hostname'] = 'test.mysite.me';
$config['username'] = 'neo4j';
$config['password'] = 'my_password';
$config['database'] = 'mygraphdb';
$config['port'] = '7474';
/* End of file neo4jDatabase.php */
/* Location: ./application/config/neo4jDatabase.php */
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j_controller extends Your_Normal_Controller{
private $neo4j;
//connect to db on instantiation
function __construct(){
parent::__construct();
$this->connect();
}
//Connect to the neo4j db
private function connect(){
//I'll get these from config file later on
$user = 'neo4j';
$password = 'mypass';
$host = 'myhost:7474';
$myDB = 'mydb';
//build connect to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.'/'.$myDB)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
4)修改配置文件(可选)
我对此有意见。最后,我创建了自己的配置文件neo4jDatabase.php
如果您在创建自己的配置文件或修改当前配置文件时也遇到问题,您仍然可以在neo4j库文件中硬编码数据库数据。这是一个坏习惯,但当它必须起作用时,它必须起作用
neo4jDatabase.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j {
private $neo4j;
//connect to the neo4j database
public function connect(){
//load CI to access config files
$CI = & get_instance();
//Get database details from the config file.
$user = $CI->config->item('username');
$password = $CI->config->item('password');
$host = $CI->config->item('hostname');
$port = $CI->config->item('port');
$database = $CI->config->item('database');
//build connection to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.':'.$port)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Neo4j_controller extends TNK_Controller{
//connect to neo4j db on instantiation
function __construct(){
parent::__construct();
//load the neo4j library that we created
$this->load->library('neo4j');
//connect to the db
$this->neo4j->connect();
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['hostname'] = 'test.mysite.me';
$config['username'] = 'neo4j';
$config['password'] = 'my_password';
$config['database'] = 'mygraphdb';
$config['port'] = '7474';
/* End of file neo4jDatabase.php */
/* Location: ./application/config/neo4jDatabase.php */
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j_controller extends Your_Normal_Controller{
private $neo4j;
//connect to db on instantiation
function __construct(){
parent::__construct();
$this->connect();
}
//Connect to the neo4j db
private function connect(){
//I'll get these from config file later on
$user = 'neo4j';
$password = 'mypass';
$host = 'myhost:7474';
$myDB = 'mydb';
//build connect to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.'/'.$myDB)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
2)使您的普通控制器实例化Neo4j_控制器
class Test extends Neo4j_controller {...}
3) 在控制器(此处称为测试)中,创建一个函数,您将:
- 通过调用$this->Get_DB()获取DB对象
- 使用该对象执行查询
public function db_query(){
$db = $this->get_db();
$query = "match (n:user) return n";
print_r($db->run($query)->getRecords());
}
调用函数db_query时,它返回用户节点,而不需要我们手动编写任何代码来连接neo4j数据库
Loïc.我的目标是在每次页面加载时自动连接到Neo4J数据库,而不必在每次需要访问数据库时手动连接到数据库。我还担心不修改任何codeigniter系统文件 编辑:更新的解决方案。我的第一个解决方案是有问题的,因为它需要模型调用控制器来获取DB对象。这是不切实际的,并且违反了MVC工作流,在MVC工作流中,应该由控制器调用模型,而不是相反 更新的解决方案: 对于此解决方案,我们需要创建两个方面:
- 新的图书馆课程
- 一个新的控制器类
- 修改配置文件并插入数据库详细信息和凭据
- connect():执行到数据库的连接,并将数据库对象保存在私有类变量$neo4j中
- get_db():返回保存在函数connect()中的数据库对象
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j {
private $neo4j;
//connect to the neo4j database
public function connect(){
//load CI to access config files
$CI = & get_instance();
//Get database details from the config file.
$user = $CI->config->item('username');
$password = $CI->config->item('password');
$host = $CI->config->item('hostname');
$port = $CI->config->item('port');
$database = $CI->config->item('database');
//build connection to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.':'.$port)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Neo4j_controller extends TNK_Controller{
//connect to neo4j db on instantiation
function __construct(){
parent::__construct();
//load the neo4j library that we created
$this->load->library('neo4j');
//connect to the db
$this->neo4j->connect();
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['hostname'] = 'test.mysite.me';
$config['username'] = 'neo4j';
$config['password'] = 'my_password';
$config['database'] = 'mygraphdb';
$config['port'] = '7474';
/* End of file neo4jDatabase.php */
/* Location: ./application/config/neo4jDatabase.php */
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j_controller extends Your_Normal_Controller{
private $neo4j;
//connect to db on instantiation
function __construct(){
parent::__construct();
$this->connect();
}
//Connect to the neo4j db
private function connect(){
//I'll get these from config file later on
$user = 'neo4j';
$password = 'mypass';
$host = 'myhost:7474';
$myDB = 'mydb';
//build connect to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.'/'.$myDB)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
4)修改配置文件(可选)
我对此有意见。最后,我创建了自己的配置文件neo4jDatabase.php
如果您在创建自己的配置文件或修改当前配置文件时也遇到问题,您仍然可以在neo4j库文件中硬编码数据库数据。这是一个坏习惯,但当它必须起作用时,它必须起作用
neo4jDatabase.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j {
private $neo4j;
//connect to the neo4j database
public function connect(){
//load CI to access config files
$CI = & get_instance();
//Get database details from the config file.
$user = $CI->config->item('username');
$password = $CI->config->item('password');
$host = $CI->config->item('hostname');
$port = $CI->config->item('port');
$database = $CI->config->item('database');
//build connection to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.':'.$port)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Neo4j_controller extends TNK_Controller{
//connect to neo4j db on instantiation
function __construct(){
parent::__construct();
//load the neo4j library that we created
$this->load->library('neo4j');
//connect to the db
$this->neo4j->connect();
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['hostname'] = 'test.mysite.me';
$config['username'] = 'neo4j';
$config['password'] = 'my_password';
$config['database'] = 'mygraphdb';
$config['port'] = '7474';
/* End of file neo4jDatabase.php */
/* Location: ./application/config/neo4jDatabase.php */
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
class Neo4j_controller extends Your_Normal_Controller{
private $neo4j;
//connect to db on instantiation
function __construct(){
parent::__construct();
$this->connect();
}
//Connect to the neo4j db
private function connect(){
//I'll get these from config file later on
$user = 'neo4j';
$password = 'mypass';
$host = 'myhost:7474';
$myDB = 'mydb';
//build connect to db
$client = ClientBuilder::create()
->addConnection('default', 'http://'.$user.':'.$password.'@'.$host.'/'.$myDB)
->build();
//save the connection object in a private class variable
$this->neo4j = $client;
}
//Returns the connection object to the neo4j database
public function get_db(){
return $this->neo4j;
}
}
2)使您的普通控制器实例化Neo4j_控制器
class Test extends Neo4j_controller {...}
3) 在控制器(此处称为测试)中,创建一个函数,您将:
- 通过调用$this->Get_DB()获取DB对象
- 使用该对象执行查询
public function db_query(){
$db = $this->get_db();
$query = "match (n:user) return n";
print_r($db->run($query)->getRecords());
}
调用函数db_query时,它返回用户节点,而不需要我们手动编写任何代码来连接neo4j数据库
Loïc.通常,您会创建一个neo4j服务,该服务将创建一次连接,并在需要时注入该服务。也许在谷歌上寻找Codigniter+DependencyInjection。我是GraphAware客户端的维护者,但我没有CI方面的经验。谢谢你的帮助Christophe,但我知道