Как определить, какую таблицу следует использовать в методе подготовки сбора?

magento.stackexchange https://magento.stackexchange.com//questions/51674

Вопрос

Из этого Изменить имя таблицы для коллекций административной сетки ветка, одно сомнение зародилось.

Если мы использовали 2 таблицы в нашем пользовательском модуле, как определить, какую таблицу следует использовать в методе подготовки коллекции?

С нетерпением жду вашего ответа!

Это было полезно?

Решение

Несколько моделей для модуля с одной таблицей для каждой модели

Создать несколько моделей для модели легко. Вам нужно define multiple entities на ваш Module Model resourceModel и это corresponding entity entity_id Table.

Пример:

        <custommodule_resource>
            <class>Amit_Custommodule_Model_Resource</class>
            <entities>
                <custommoduleone>
                    <table>custommoduleTestOne</table>
                </custommoduleone>
        <custommoduletwo>
                    <table>custommoduleTestTwo</table>
        </custommoduletwo>
            </entities>
        </custommodule_resource>

Полный код модели модуля:

<models>
    <custommodule>
    <class>Amit_Custommodule_Model</class>
    <resourceModel>custommodule_resource</resourceModel>
    </custommodule>
    <custommodule_resource>
    <class>Amit_Custommodule_Model_Resource</class>
    <entities>
        <custommoduleone>
        <table>custommoduleTestOne</table>
        </custommoduleone>
        <custommoduletwo>
        <table>custommoduleTestTwo</table>
        </custommoduletwo>
    </entities>
    </custommodule_resource>
</models>

Эта модель дает вам две модели для модуля

  1. Mage::getModel('custommodule/custommoduletwo') // в качестве сущностей используется custommoduletwo
  2. Mage::getModel('custommodule/custommoduleone')

Процесс определения класса

Согласно Magento для each Model требуется необходимость создания соответствующего Модель, Ресурс, Коллекция сорт

Класс модели для модели Mage::getModel('custommodule/custommoduleone')

<?php
class Amit_Custommodule_Model_Custommoduleone extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('custommodule/custommoduleone');
    }

}

Класс ресурса

app/code/community/amit/custommodule/model/resource/custommoduleone.php - Подробнее см.:

<?php
class Amit_Custommodule_Model_Resource_Custommoduleone extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('custommodule/custommoduleone', 'table_Primarykey');
    }
}

Класс коллекции:

Путь к файлу коллекции: Collection.php приложение/код/сообщество/Amit/Custommodule/Model/Resource/Custommoduleone

<?php
class Amit_Custommodule_Model_Resource_Custommoduleone_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _constuct(){
        $this->_init('custommodule/custommoduleone');    
    }
}

Вторая модель:

Класс модели

<?php
class Amit_Custommodule_Model_CustommoduleTwo extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('custommodule/custommoduletwo');
    }

}

Класс ресурса

приложение/код/сообщество/Amit/Custommodule/Model/Resource/Custommoduletwo.php

<?php
class Amit_Custommodule_Model_Resource_Custommoduletwo extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('custommodule/custommoduletwo', 'table_Primarykey');
    }
}

Класс коллекции:

Путь к файлу коллекции: Collection.php приложение/код/сообщество/Amit/Custommodule/Model/Resource/Custommoduletwo

<?php
class Amit_Custommodule_Model_Resource_Custommoduletwo_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _constuct(){
        $this->_init('custommodule/custommoduletwo');    
    }
}

ПРИМЕЧАНИЕ. В ближайшее время я предоставлю несколько таблиц для одной модели.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top