Magento – SetData and Save successed but nothing gets written to the data base

adminformmagento-1model

As the question title states, I am using setData and then save to update/add data to my table, but whenever I save my custom product nothing gets written to the database even when the data is 'set' in the table. I followed this post http://alanstorm.com/magento_models_orm and added a set up block under resources.

This is my config.xml file

<?xml version="1.0"?>

<config>
    <modules>
        <FPM_TrackProcessor>
            <version>1.0.0.0.1</version>
        </FPM_TrackProcessor>
    </modules>
    <crontab>
        <jobs>
            <fpm_trackprocessor>
                <schedule>
                    <cron_expr>0,30 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>fpm_trackprocessor/cron::processtracks</model>
                </run>
            </fpm_trackprocessor>
        </jobs>
    </crontab>
    <global>
      <models>
        <fpm_trackprocessor>
          <class>FPM_TrackProcessor_Model</class>
          <resourceModel>fpm_trackprocessor_mysql4</resourceModel>
        </fpm_trackprocessor>
        <fpm_trackprocessor_mysql4>
          <class>FPM_TrackProcessor_Model_Mysql4</class>
          <entities>
            <trackprocessor>
              <table>fpm_trackprocessor_trackprocessor</table>
            </trackprocessor>
          </entities>
       </fpm_trackprocessor_mysql4>
      </models>
      <resources>
        <fpm_trackprocessor_setup>
          <setup>
            <module>FPM_TrackProcessor</module>
            <class>FPM_TrackProcessor_Model_Resource_Setup</class>
          </setup>
          <connection>
            <use>core_setup</use>
          </connection>
        </fpm_trackprocessor_setup>
        <fpm_trackprocessor_write>
          <connection>
            <use>core_write</use>
          </connection>
        </fpm_trackprocessor_write>
        <fpm_trackprocessor_read>
          <connection>
            <use>core_read</use>
          </connection>
        </fpm_trackprocessor_read>
      </resources>
    </global>
</config>

I have FPM/TrackProcessor/Model/TrackProcessor.php

<?php
class FPM_TrackProcessor_Model_TrackProcessor extends Mage_Core_Model_Abstract{

  protected function _construct(){
    $this->_init('fpm_trackprocessor/trackprocessor');
  }

}

FPM/TrackProcessor/Model/MySql4/TrackProcessor.php

<?php
class FPM_TrackProcessor_Model_Mysql4_TrackProcessor extends Mage_Core_Model_Mysql4_Abstract {

  protected function _construct(){
    $this->_init('fpm_trackprocessor/trackprocessor', 'entity_id');
  }
}

FPM/TrackProcessor/Model/Mysql4/TrackProcessor/Collection.php

<?php
class FPM_TrackProcessor_Model_Mysql4_TrackProcessor_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
    protected function _construct()
    {   
            $this->_init('fpm_trackprocessor/trackprocessor');
    }   
}

And FPM/TrackProcessor/Model/Resource/Setup.php

<?php
class FPM_TrackProcessor_Model_Resource_Setup
    extends Mage_Catalog_Model_Resource_Setup {
}

Finally in the controller of my custom module at

FPM/Volumes/controllers/Adminhtml/Volumes/VolumeConroller.php

I have this code, to write to the db, inside the saveAction function

        $volumeFiles = $_FILES['volume']['name'];
        $trackFiles = $volumeFiles['track'];
        $editsFiles = $volumeFiles['edits'];
        $currentVolume = $this->getRequest()->getPost('volume');
        $volumeName = str_replace(' ', '_',$currentVolume['volume_name']);
        $trackDir = 'media/volume/tracks/'.$volumeName.'/';
        $volumeId = $this->getRequest()->getParam('id');
        $entityId = $volumeId = $volumeId + 0;
        $status = 0;
        $trackModel = Mage::getModel('fpm_trackprocessor/trackprocessor');

        //TRACK DB WRITE
        $trackType = 0;
        $trackNumIndex = 0;
        foreach($trackFiles as $track){
          $tmpStrArray = explode('.',$track);
          $trackName = $tmpStrArray[0];
          $tmpStrArray = str_split($trackName, 1); 
          $trackDir = $trackDir.$tmpStrArray[0].'/';
          $trackNum = $currentVolume['order'][$trackNumIndex] + 0;
          //WRITING TO DB
          $dbData = array(
            'volume_id'         => $volumeId,
            'entity_id'         => $entityId,
            'input_track_path'  => $trackDir,
            'status'            => $status,
            'track_num'         => $trackNum,
            'track_type'        => $trackType,
          );
          $trackModel->setData($dbData);
          try{
            $insertId = $trackModel->save()->getId();
            echo "Data inserted. Insert ID: ".$insertId;
          }catch(Exception $e){
            echo $e->getMessage();  
          }
        }

Currently, the code works, my echoed success message appears and nothing breaks, per say, when the custom product(volume) is saved, but when I check my table using mysql bench, all the fields are still null. I've also cleared the cache countless times, and have gone as far as to delete the install script from core_resources in my table and also dropping my fpm_trackprocessor_trackprocessor table from my schema, but nothing seems to work. Would anyone please point out what I am doing wrong and explain why nothing is being saved to my db?

Best Answer

Try to step-up xdebug and work your way into the save function to see what is happening. Magento will do a desc table before a save to make sure it only tries to save columns that are on the table.

Check out Mage_Core_Model_Resource_Db_Abstract::_prepareDataForTable.

/**
 * Prepare data for passed table
 *
 * @param Varien_Object $object
 * @param string $table
 * @return array
 */
protected function _prepareDataForTable(Varien_Object $object, $table)
{
    $data = array();
    $fields = $this->_getWriteAdapter()->describeTable($table);
    foreach (array_keys($fields) as $field) {
        if ($object->hasData($field)) {
            $fieldValue = $object->getData($field);
            if ($fieldValue instanceof Zend_Db_Expr) {
                $data[$field] = $fieldValue;
            } else {
                if (null !== $fieldValue) {
                    $fieldValue   = $this->_prepareTableValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
                    $data[$field] = $this->_getWriteAdapter()->prepareColumnValue($fields[$field], $fieldValue);
                } else if (!empty($fields[$field]['NULLABLE'])) {
                    $data[$field] = null;
                }
            }
        }
    }
    return $data;
}
Related Topic