M
M
max_mara2012-01-27 03:03:07
Magento
max_mara, 2012-01-27 03:03:07

Magento get table without prefix

Hello, is there a table?

<entities>
    <user>
        <table>oem_user</table>
    </user>
</entities>


this table belongs to another CMS that stores its belongings in the same database as Magento

For magento, a module is being developed that should have access to the tables of this CMS, but that's bad luck, magento has the prefix mage_

Therefore
protected function _construct()
{
    $this->_init('mass/user', 'id');
}   

Says that
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hyper.mage_oem_user' doesn't exist

How to make it so that the prefix is ​​not added to this table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
max_mara, 2012-01-27
@max_mara

It often happens that he asked himself, answered himself.
I overridden the getTableName method
and did as written here
stackoverflow.com/questions/2417134/how-to-override-table-name-andprefix-in-magento-model

class Meta_Mass_Model_Resource extends Mage_Core_Model_Resource
{
    public function getTableName($modelEntity)
    {
        $tableSuffix = null;
        if (is_array($modelEntity)) {
            list($modelEntity, $tableSuffix) = $modelEntity;
        }

        $parts = explode('/', $modelEntity);
        if (isset($parts[1])) {
            list($model, $entity) = $parts;
            $entityConfig = false;
            if (!empty(Mage::getConfig()->getNode()->global->models->{$model}->resourceModel)) {
                $resourceModel = (string)Mage::getConfig()->getNode()->global->models->{$model}->resourceModel;
                $entityConfig  = $this->getEntity($resourceModel, $entity);
            }

            if ($entityConfig && !empty($entityConfig->table)) {
                $tableName = (string)$entityConfig->table;
            } else {
                Mage::throwException(Mage::helper('core')->__('Can\'t retrieve entity config: %s', $modelEntity));
            }
        } else {
            $tableName = $modelEntity;
        }

        Mage::dispatchEvent('resource_get_tablename', array(
            'resource'      => $this,
            'model_entity'  => $modelEntity,
            'table_name'    => $tableName,
            'table_suffix'  => $tableSuffix
        ));

        $mappedTableName = $this->getMappedTableName($tableName);
        if ($mappedTableName) {
            $tableName = $mappedTableName;
        } else {
            if($entityConfig->ignore_prefix) {
                $tablePrefix = '';
            } else {
                $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
            }
            
            $tableName = $tablePrefix . $tableName;
        }

        if (!is_null($tableSuffix)) {
            $tableName .= '_' . $tableSuffix;
        }
        return $this->getConnection(self::DEFAULT_READ_RESOURCE)->getTableName($tableName);
    }    

}

<models>
            <core>
                <rewrite>
                    <resource>Meta_Mass_Model_Resource</resource>
                </rewrite>
            </core>
</models>

O
Oleg Batishchev, 2013-05-07
@z0rg

The fact is that it (the prefix) should be added, it's like a rule of the engine. If you use manually created tables without a prefix, this does not mean that the magenta should know about it, you gave it the name of the entity, it gets it and adds a prefix, everything is reasonable. And I think it's wrong to override
this method.
Try this, but I'm not sure about this method.

<entities>
    <user>
        <table>oem_user</table>
  <prefix></prefix>
    </user>
</entities>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question