开发者

joomla 1.6, component: How to do component to link between 2 tables with m2m and display list in edit page?

I'm new to Joomla! and also new to the component development.

I want to create a component which be able to link between 2 tables.

joomla v 1.6:

A table's structure:

CREATE TABLE `#__a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

B table's structure:

CREATE TABLE `#__b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_IN开发者_运维百科CREMENT=0 DEFAULT CHARSET=utf8;

AB table's structure:

CREATE TABLE `#__ab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_a` int(11) NOT NULL,
`id_b` int(11) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

Assume that we have already created the basic create, edit page and delete action with the MVC for A and B (created from the hello world component). From the hellow world tutorial we could know clear about the file and folder structure and the componet's code, but in the tutorial there is only 1 table, but here there is 2 tables and also new id id table.

So it means that in the component we're developing here, there are 2 submenus.

In the edit or new page

  1. There is A details block which we can fill name.(done from the tutorial)

  2. There is B linking block which we can choose the B (select option, can select more then 1) to add into the A and also display list the B we added. In every item in the list, it has a delete button or link to unlink between the A and B.

Any ideas how to do please?

Thanks and Best Regards Friends,

Rithy


First you need some logic that will save the results in the reference table and the second part is to retrieve the data to be shown on the user side. You need a Model:

class CompControllerA extends JControllerForm { 
   // Here put your save code
   //....
   function save() {
     $formData = JRequest::getVar('jform');
     $bRecords = $formData['bRecords'];
     $aRecordId = $formData['id'];
     $referenceModel->delete($aRecordId); // Delete all records that has same ID as current record
     foreach($bRecords as $row) {
        $data['id']=0;
        $data['a_id']=$aRecordId;
        $data['b_id']=(int)$row;
        $bModel->save($data);
     }

     // dont forget to call parent method
     parent::save();
   }
}

Next step is when you create the for just take the results from the reference table and show the correct results in the form.

Here is some example code of a component I've built.

class IbookModelExtra extends JModelAdmin
{
    protected function loadFormData()
    {
        $db =& $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('b_id')->from('#__table_a_b')->where('a_id='.$data->id);
        $db->setQuery((string)$query);
        $data->b = $db->loadResultArray();
    }
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜