如果我只想在PhalconPHP中使用MongoDB,我应该对db使用什么设置?

时间:2013-10-12 11:38:56

标签: php mongodb phalcon

我使用Phalcon DevTools(1.2.3)创建了一个简单的Phalcon项目。 现在我想将MongoDB用于数据库。如何正确设置? 我走到这一步(见下面的代码):

这是我的config.php

<?php

return new \Phalcon\Config(array(
'database' => array(
    'adapter'     => 'Nosql', //Was 'Mysql', but Nosql is not supported?
    'host'        => 'localhost',
    'username'    => 'root',
    'password'    => '',
    'dbname'      => 'test',
),
'application' => array(
    'controllersDir' => __DIR__ . '/../../app/controllers/',
    'modelsDir'      => __DIR__ . '/../../app/models/',
    'viewsDir'       => __DIR__ . '/../../app/views/',
    'pluginsDir'     => __DIR__ . '/../../app/plugins/',
    'libraryDir'     => __DIR__ . '/../../app/library/',
    'cacheDir'       => __DIR__ . '/../../app/cache/',
    'baseUri'        => 'localhost/',
)
));

这是我的services.php

<?php    

use Phalcon\DI\FactoryDefault,
Phalcon\Mvc\View,
Phalcon\Mvc\Url as UrlResolver,
//Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter, //Do I need this when I use Mongo?
Phalcon\Mvc\View\Engine\Volt as VoltEngine,
Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter,
Phalcon\Session\Adapter\Files as SessionAdapter;

/**
 * The FactoryDefault Dependency Injector automatically register the right services  providing a full stack framework
 */
$di = new FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function() use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
}, true);

/**
 * Setting up the view component
 */
$di->set('view', function() use ($config) {

$view = new View();

$view->setViewsDir($config->application->viewsDir);

$view->registerEngines(array(
    '.volt' => function($view, $di) use ($config) {

        $volt = new VoltEngine($view, $di);

        $volt->setOptions(array(
            'compiledPath' => $config->application->cacheDir,
            'compiledSeparator' => '_'
        ));

        return $volt;
    },
    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));

return $view;
}, true);

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('mongo', function() use ($config) {
    $mongo = new Mongo();
    return $mongo->selectDb($config->database->dbname);
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function() {
    return new MetaDataAdapter();
});

/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function() {
    $session = new SessionAdapter();
    $session->start();
    return $session;
});

我使用文档将标准的mysql数据库连接更改为mongo。 但现在我必须在Config中设置我的数据库适配器,但Nosql似乎不起作用。在尝试创建模型时,DevTools会在终端中抛出此错误:

Error: Adapter Nosql is not supported

当我在配置中为适配器输入'Mysql'并尝试创建模型时,这是我得到的错误:

Error: SQLSTATE[HY000] [2002] No such file or directory

是否需要设置Mysql适配器才能使用Mongo / Nosql?或者我应该为适配器/配置添加其他东西?有任何想法吗?

4 个答案:

答案 0 :(得分:3)

service.php 文件中,您已注册mongo服务

$di->set('mongo', function() {
    $mongo = new Mongo();
    return $mongo->selectDb("DB_NAME");
}, true);
下面是

以下代码行,

$di->set('collectionManager', function(){
  return new Phalcon\Mvc\Collection\Manager();
}, true);

完成上述操作后,您需要确保您的模型从\ Phalcon \ Mvc \ Collection

扩展

E.g。 Customers.php

class Customers extends \Phalcon\Mvc\Collection
{
    public $name;
    public $email;
}

完成上述操作后,您可以验证一切是否正常,如下所示

$robot       = new Customers();
$robot->email= "abc@test.com";
$robot->name = "XYZ";
if ($robot->save() == false) 
{
echo "Could not be Saved!!";
}
else
{
echo "Data Saved!!";
}

您可以将以上代码放在任何Controller-Action中并尝试。

如果一切顺利,您应该能够在MongoDB数据库中看到在Customer集合中创建的一个文档。

请参阅http://docs.phalconphp.com/en/latest/reference/odm.html了解更多信息..

答案 1 :(得分:1)

$di->set('mongo', function() {
$user = 'blog';
$password = 'blog';
$host = 'localhost';
$port = '27017';
$db = 'blog';
$cn = sprintf('mongodb://%s:%d/%s',$host,$port,$db);


$con = new Mongo($cn,array('username'=>$user,'password'=>$password));

return $con->selectDB($db);
}, true);

答案 2 :(得分:0)

As of the latest PhalconPHP,MongoDB似乎只是一个受支持的缓存选项,而不是用作数据库的替代品。

答案 3 :(得分:0)

看起来Config组件没有MongoDB适配器,孵化器(https://github.com/phalcon/incubator)中只有MySQL和其他几个(基于文本)。

您仍然可以将MongoDB用于ACL和模型,请参阅https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Acl/Adapterhttp://docs.phalconphp.com/en/latest/reference/odm.html#