15

I am trying to connect Mongo Doctrine ODM to Magento. I have successfully connected zend with mongodb. and I dont know how to Connect the both. I have placed Mongodb odm in "lib" folder of magento and i am having the Issue of linking the lib to magento. I am trying to "include" the library base class. But the library contains many namespaces. I dont think magento supports namespace. so its showing error. . Helps are highly appreciated. Thanks in advance.

2 Answers 2

4

I think this is a very good question, which is connected with how Magento loads classes.

There are no good way to fix that without changing Magento files.

So the main problem is in lib/Varien/Autoload.php

public function autoload($class)
{
    if ($this->_collectClasses) {
        $this->_arrLoadedClasses[self::$_scope][] = $class;
    }
    if ($this->_isIncludePathDefined) {
        $classFile =  COMPILER_INCLUDE_PATH . DIRECTORY_SEPARATOR . $class;
    } else {
        $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
    }
    $classFile.= '.php';
    return include $classFile;
}

Varien_Autoload::autoload - this method can only load classes that follow "Pear naming conventions" -- Mage_Core_Model_Config.

But if namespaces are used $class will contain Mage\\Core\\Model\\Config.

So we can add one more check and fix namespace problem

public function autoload($class)
{
    if ($this->_collectClasses) {
        $this->_arrLoadedClasses[self::$_scope][] = $class;
    }
    if ($this->_isIncludePathDefined) {
        $classFile =  COMPILER_INCLUDE_PATH . DIRECTORY_SEPARATOR . $class;
    } else if (strpos($class, "\\") !== false) {
        $classFile = str_replace("\\", DIRECTORY_SEPARATOR, $class);
    } else {
        $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
    }
    $classFile.= '.php';
    return include $classFile;
}

Now you can use libraries which use namespaces.

In addition here you will find code changes list to use namespaces in Magento.

1
  • Olekssi , I tried this one. Its not working. I am saying this in straight. I dont want to confuse you. Can u Connect magento with mongo with the help of doctrine ODM. If you have any idea, please share with me..
    – Sundar
    Commented Mar 7, 2014 at 9:24
3

try these approach, i was able to use two separate database on single magento setup.

for that you will have to create configuration, follow the steps below.

in app/etc/modules

<?xml version="1.0"?>
<config>
    <modules>
        <Deph_Externaldb>
            <active>true</active>
            <codePool>local</codePool>
        </Deph_Externaldb>
    </modules>
</config>

in app/code/local and be sure to update the database details below here

<?xml version="1.0"?>
<config>
    <modules>
        <Deph_Externaldb>
            <version>0.1.0</version>
        </Deph_Externaldb>
    </modules>
    <global>
        <resources>
            <externaldb_write>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_write>
            <externaldb_read>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_read>
            <externaldb_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </externaldb_setup>
            <externaldb_database>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[db_username]]></username>
                    <password><![CDATA[db_password]]></password>
                    <dbname><![CDATA[db_name]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </externaldb_database>
        </resources>
    </global>
</config>

your config is ready, you call use the below connection string to access the db

<?php

    $resource   = Mage::getSingleton('core/resource');
    $conn       = $resource->getConnection('externaldb_read');
    $results    = $conn->query('SELECT * FROM tblName');

    print_r($results)
1
  • do you know if its possible to use that approach with a mongodb configuration? like : <document_db> <connection_string><![CDATA[mongodb://localhost:27017/]]></connection_string> <dbname><![CDATA[db]]></dbname> </document_db>
    – s_h
    Commented Jan 5, 2016 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.