Question

I have a model called Warehouses and another called Warehouse Products which is associated. The issue I'm having is that Warehouse Products doesn't save, despite it's save() function being wrapped in a try-catch which produces no issues.

try {
    $model->save();
    $session->addSuccess($this->__('The product has been saved.'));
    $this->_redirect('*/*/');

    return;            
} catch (Mage_Core_Exception $e) {
    $session->addError($e->getMessage());
} catch (Exception $e) {
    $session->addError($this->__('An error occurred while saving this product.'));
}

If I use the model to return the table, I get the following:

$model->getResource()->getTable();
// Can't retrieve entity config

This obviously suggests there's something amiss with my setup -

<inventorymanagement>
    <class>CompanyName_InventoryManagement_Model</class>
    <resourceModel>inventorymanagement_resource</resourceModel>
</inventorymanagement>
<inventorymanagement_resource>
    <class>TheGenieLab_InventoryManagement_Model_Resource</class>
    <entities>
        <warehouse>
            <table>inventorymanagement_warehouse</table>
        </warehouse>
        <warehouse_product>
            <table>inventorymanagement_warehouse_product</table>
        </warehouse_product>
    </entities>
</inventorymanagement_resource>

The resources section:

<resources>
    <inventorymanagement_setup>
        <setup>
            <module>TheGenieLab_InventoryManagement</module>
        </setup>
    </inventorymanagement_setup>
</resources>

My files are laid out as below:

- Resource
    - Warehouse
        - Product
            - Collection.php
        - Collection.php
        - Product.php
    - Warehouse.php
- Warehouse
    - Product.php
- Warehouse.php

Is there something else I need to do?

EDIT

Here's some further code
Warehouse > Product.php

class CompanyName_InventoryManagement_Model_Warehouse_Product extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('inventorymanagement/warehouse_product');
    }
}

Resource > Warehouse > Product.php

class CompanyName_InventoryManagement_Model_Resource_Warehouse_Product extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_init('inventorymanagement/warehouse_product', 'id');
    }
}

Resource > Warehouse > Product > Collection.php

class CompanyName_InventoryManagement_Model_Resource_Warehouse_Product_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    protected function _construct()
    {
        $this->_init('inventorymanagement/warehouse_product');
    }
}
Was it helpful?

Solution

I've figured it out.

Prior to the $model->save(); I had this:

$model = Mage::getModel('inventorymanagement/warehouse_product');
$product = $postData['warehouse_product'];
if (isset($product['id'])) {
    $model->load($product['id']);
}
$product['product_id'] = str_replace('product/', '', $product['product_id']);
unset($product['save']);
$model->setData($product);

The issue was that as $product['id'] was set as a hidden field on the form, then it was always set; thus the isset check was true. It was saving, but because the $model->load() was happening, it wasn't saving anywhere in particular.

For Addition methods, I removed this check completely along with the load statement, then I simply ran this unset($product['id']); prior to setting the data.

All working great!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top