Showing posts with label Resources. Show all posts
Showing posts with label Resources. Show all posts

Wednesday 14 September 2016

How to create custom model and resources file in existing module with database in magento

Step 1 : Update code in config.xml file of the model

<models>
...
...
<kanasagra_jaydip_resource>
...
...
<printsignsize>
   <table>name of your table</table>
</printsignsize>
</kanasagra_jaydip_resource>
</models>

Step 2 : Add new file in Model as Printsignsize.php

<?php
class Kanasagra_Jaydip_Model_Printsignsize extends Mage_Core_Model_Abstract
{

    const ENTITY    = 'kanasagra_jaydip_printsignsize';
    const CACHE_TAG = 'kanasagra_jaydip_printsignsize';
    protected $_eventPrefix = 'kanasagra_jaydip_printsignsize';
    protected $_eventObject = 'printsignsize';
    public function _construct()
    {
        parent::_construct();
        $this->_init('kanasagra_jaydip/printsignsize');
    }
    protected function _beforeSave()
    {
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()) {
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }
    public function getPrintsignUrl()
    {
        return Mage::getUrl('kanasagra_jaydip/printsignsize/view', array('id'=>$this->getId()));
    }
    protected function _afterSave()
    {
        return parent::_afterSave();
    }
    public function getDefaultValues()
    {
        $values = array();
        $values['status'] = 1;
        return $values;
    }
 
}

Step 3 : Add new file in model/Resource as Printsignsize.php

<?php
class Kanasagra_Jaydip_Model_Resource_Printsignsize extends Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
        $this->_init('kanasagra_jaydip/printsignsize', 'entity_id');
    }
    public function lookupStoreIds($printsignId)
    {
        $adapter = $this->_getReadAdapter();
        $select  = $adapter->select()
            ->from($this->getTable('kanasagra_jaydip/printsignsize_store'), 'store_id')
            ->where('printsignsize_id = ?', (int)$printsignId);
        return $adapter->fetchCol($select);
    }
 
    protected function _afterLoad(Mage_Core_Model_Abstract $object)
    {
        if ($object->getId()) {
            $stores = $this->lookupStoreIds($object->getId());
            $object->setData('store_id', $stores);
        }
        return parent::_afterLoad($object);
    }
    protected function _getLoadSelect($field, $value, $object)
    {
        $select = parent::_getLoadSelect($field, $value, $object);
        if ($object->getStoreId()) {
            $storeIds = array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId());
            $select->join(
                array('signprint_printsignsize_store' => $this->getTable('kanasagra_jaydip/printsignsize_store')),
                $this->getMainTable() . '.entity_id = signprint_printsignsize_store.printsignsize_id',
                array()
            )
            ->where('signprint_printsignsize_store.store_id IN (?)', $storeIds)
            ->order('signprint_printsignsize_store.store_id DESC')
            ->limit(1);
        }
        return $select;
    }
    protected function _afterSave(Mage_Core_Model_Abstract $object)
    {
        $oldStores = $this->lookupStoreIds($object->getId());
        $newStores = (array)$object->getStores();
     
        if (empty($newStores)) {
            $newStores = (array)$object->getStoreId();
        }
        $table  = $this->getTable('kanasagra_jaydip/printsignsize_store');
        $insert = array_diff($newStores, $oldStores);
        $delete = array_diff($oldStores, $newStores);
        if ($delete) {
            $where = array(
                'printsignsize_id = ?' => (int) $object->getId(),
                'store_id IN (?)' => $delete
            );
            $this->_getWriteAdapter()->delete($table, $where);
        }
        if ($insert) {
            $data = array();
            foreach ($insert as $storeId) {
                $data[] = array(
                    'printsignsize_id'  => (int) $object->getId(),
                    'store_id' => (int) $storeId
                );
            }
            $this->_getWriteAdapter()->insertMultiple($table, $data);
        }
        return parent::_afterSave($object);
    }
}

Step 4 : Add new file in model/Resource/Printsignsize as Collection.php


<?php
class Kanasagra_Jaydip_Model_Resource_Printsignsize_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected $_joinedFields = array();
    protected function _construct()
    {
        parent::_construct();
        $this->_init('kanasagra_jaydip/printsignsize');
        $this->_map['fields']['store'] = 'store_table.store_id';
    }
    public function addStoreFilter($store, $withAdmin = true)
    {
        if (!isset($this->_joinedFields['store'])) {
            if ($store instanceof Mage_Core_Model_Store) {
                $store = array($store->getId());
            }
            if (!is_array($store)) {
                $store = array($store);
            }
            if ($withAdmin) {
                $store[] = Mage_Core_Model_App::ADMIN_STORE_ID;
            }
            $this->addFilter('store', array('in' => $store), 'public');
            $this->_joinedFields['store'] = true;
        }
        return $this;
    }
    protected function _renderFiltersBefore()
    {
        if ($this->getFilter('store')) {
            $this->getSelect()->join(
                array('store_table' => $this->getTable('kanasagra_jaydip/printsignsize_store')),
                'main_table.entity_id = store_table.printsignsize_id',
                array()
            )
            ->group('main_table.entity_id');
            /*
             * Allow analytic functions usage because of one field grouping
             */
            $this->_useAnalyticFunction = true;
        }
        return parent::_renderFiltersBefore();
    }
    protected function _toOptionArray($valueField='entity_id', $labelField='materials', $additional=array())
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }
    protected function _toOptionHash($valueField='entity_id', $labelField='materials')
    {
        return parent::_toOptionHash($valueField, $labelField);
    }
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}
Step 5 : Add following code into your Controller File

$printsignsize = Mage::getModel('kanasagra_jaydip/printsignsize');
                 
$printsignsize
             ->setPrintsignid($printsign->getId())
             ->setSizename($size['sizename'])
             ->setSizeword($size['sizeword'])
             ->setSizeprice($size['sizeprice'])
             ->setStores($answer['votes']);
$printsignsize->save();

Step 6 : Create table in your database.

Step 7 : Don't forget clear the cache. Finish n Enjoy!!!