Im working on a custom admin module for magento, and trying to learn the magento approach at the same time =). Im currently on version CE 1.6.
I’ve followed a couple of tutorials and articles and managed to setup an custom db table and I think I managed the make collection class to work. (I should say Im new to Zend Framework/Magento and a beginner skilled programmer). At least the following code get’s me the right result:
$department_collection = Mage::getModel('custom/systemconfig')->getCollection()
->addFilter('name','departments');
When I var_dump this with $department_collection->getData() I get an array with the filtered rows from my DB.
Now, when I try to do this:
foreach ($department_collection as $department) {
$department->delete();
}
I get an exception from Magento:
-Warning: include(Mage\Upperfield\Model\Systemconfig.php) [function.include]: failed to open stream: No such file or directory in D:\wamp\www\magento\lib\Varien\Autoload.php on line 93
The problem is that I guess I setup my directory structure wrong, but just don’t understand what it is. And the trace isn’t much help as it is concatenated and only shows inrelevant info.
My directory structure looks like this:
+app
+code
+local
+Namespace
+Module
+Model
+Mysql4
+Systemconfig
Collection.php
Systemconfig.php
Systemconfig.php
I’ve loaded my models with:
//file: ../Mysql4/Systemconfig.php
class Namespace_Module_Model_Mysql4_Systemconfig extends Mage_Core_Model_Mysql4_Abstract{
protected function _construct()
{
$this->_init('module/systemconfig', 'systemconfig_id');
}
}
// file: ../Mysql4/Systemconfig/Collection.php
class Namespace_Module_Model_Mysql4_Systemconfig_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
protected function _construct()
{
$this->_init('namespace/systemconfig');
}
}
My config.xml looks currently like this (excerpt):
<models>
<module>
<class>Namespace_Module_Model</class>
<resourceModel>module_mysql4</resourceModel>
</module>
<module_mysql4>
<class>Namespace_Module_Model_Mysql4</class>
<entities>
<systemconfig>
<table>module_systemconfig</table>
</systemconfig>
</entities>
</module_mysql4>
My guess is either:
I setup the collection class with wrong structure, or
I missunderstand how to use the collection object.
Any wiz out there that could explain to me what happens here?
Gratefull for any help.
Best regards
Adde
The error you got was from improper xml definitions OR class naming/file structure – which should be identical. When the Autoloader read your config.xml and looked where you told it to in said config.xml, it did not find the file(s) it was looking for.
A little info on collections and their parent models, if it helps:
If you are working directly with a collection, you should use:
If you are working with a Model that takes a collection’s data, and modifies the data (e.g. sums things up, adds additional data, removes things etc.) you should call the model like so:
Either way, in your config.xml, you need to define that you are using a resource model and which tables (if you have custom tables) it should be working with. You should never have to write a new collection to interact with default Magento tables, as there are already collections that you can call OR extend (not recommended, you should use Observers).
Here is an example of a proper collection definition in config.xml:
In this file, the inline definitions and nodes that I’ve written MUST be written like that, you cannot have a return, it must be the same line. This is a small drawback to how the Magento autoloader works.
Hope this helps!