Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing data from 2 tables in Magento custom module

Tags:

model

magento

I need some help. I have created a custom module in magento that needs to interact with multiple tables.

I have used the following to get the table names

 <entities>
     <support1>
       <table>table1</table>
     </support1>
     <support2>
       <table>table2</table>
     </support2>   
     <support3>
       <table>table3</table>
     </support3>      
  </entities>

i have then added the following to my model

  public function _construct()
 {
     parent::_construct();
     $this->_init('support/support1');
     $this->_init('support/support2');
     $this->_init('support/support3');
 }

In the mysql4 folder i have...

 public function _construct()
 {
     $this->_init('support/support1', 'ticket_id');
     $this->_init('support/support2', 'dept_id');
     $this->_init('support/support3', 'priority_id');
 }

And in Collection.php i have...

public function _construct()
 {
     parent::_construct();
     $this->_init('support/support1');
     $this->_init('support/support2');
     $this->_init('support/support3');
 }

So using

$collection = Mage::getModel('support/support')->getCollection();

How can i define access to support1 or support2 etc. I have tried using...

$collection = Mage::getModel('support/support1')->getCollection();

and

$collection = Mage::getModel('support/support')->getCollection('support1');

but both failed, how is this supposed to work??

Thanks in advance.

like image 480
JSweete Avatar asked Mar 23 '13 00:03

JSweete


2 Answers

Magento doesn't have a "one module, one data class" structure. Instead, a single module may contain many different models. Each model class accesses a single table.

So, your code generation tool gave you three classes something like

Package_Support_Model_Support                             //model
Package_Support_Model_Resource_Mysql4_Support             //model resource
Package_Support_Model_Resource_Mysql4_Support_Collection  //collection

These three classes are what makes up a single model in Magento.

So if you wanted support1, you'll need three more classes

Package_Support_Model_Support1                             //model
Package_Support_Model_Resource_Mysql4_Support1             //model resource
Package_Support_Model_Resource_Mysql4_Support_Collection1  //collection

Which would enable

Mage::getModel('support/support1');

The above code sample gets a model from the support module whose name is support1.

The specifics are too much for a single StackOverflow answer, but If you need more help this is an older article of mine that covers creating a model from scratch without code creation tools.

like image 65
Alan Storm Avatar answered Oct 23 '22 10:10

Alan Storm


Try create the following folder structure and update the class definition for each file as needed

|-/Model
|---Support1.php
|---Support2.php
|---Support3.php
|------Mysql4
|--------Support1.php
|--------Support1
|----------Collection.php
|--------Support2.php
|--------Support2
|----------Collection.php
|--------Support3.php
|--------Support3
|----------Collection.php


class <CompanyName>_<ModuelName>_Model_Support[x] extends Mage_Core_Model_Abstract

class <CompanyName>_<ModuelName>_Model_Mysql4_Support[x] extends Mage_Core_Model_Mysql4_Abstract

class <CompanyName>_<ModuelName>_Model_Mysql4_Support[x]_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
like image 33
Renon Stewart Avatar answered Oct 23 '22 11:10

Renon Stewart