0

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');
    }
}
8
  • $model what is the value of this ? – Rajeev K Tomy Sep 3 '14 at 14:14
  • Mage::getSingleton('inventorymanagement/warehouse_product'); which is valid, i.e. I can use this to read from the database successfully if I use getCollection() – Dan Hanly Sep 3 '14 at 14:18
  • you have values in that table too? but can't access the table ? – Rajeev K Tomy Sep 3 '14 at 14:21
  • I manually added to the database to test, yes. – Dan Hanly Sep 3 '14 at 14:22
  • getCollection() is working. So most probably issue lies in setup resources. Can we have a look on your resource files? please show relevant config.xml part and files – Rajeev K Tomy Sep 3 '14 at 14:24
1

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!

1
  • Please mark this as accepted. – Marius Nov 11 '14 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.