Component writing - MVC - howto correctly code CRUD for one to one, one to many, many to many

888 views
Skip to first unread message

Varkhan

unread,
Apr 16, 2013, 2:52:08 AM4/16/13
to Joomla! General Development
Hello there,

I'm new to Joomla programming and as you can imagine a bit confused.
I've got a background in C++/Java programming and some PHP (but really
not an expert). I've read the Joomla! Programming by Dexter&Landry
(great, but without simple, precise examples), some tutorials here and
there including part of the http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5.

I'm writing a first "test" component that works with specific tables
and do not use Joomla's (e.g. Categories). What I need to know is how
to do things correctly with related tables (one to one, one to many,
many to many) if I want to follow the Joomla MVC.

Specific problem : how to work (backend first, then frontend) on
tables with relationships ?

Here the basics :

Tables :

Name : OriginalTeam
Fields : id, name

Name : Team
Fields : id, name, idOriginalTeam, description


idOriginalTeam being of course one to one related to OriginalTeam.id
(without real constraint validation on the mysql side)

What I want to do is IMHO simple :

Backend :

1) show the list of Teams with Original Team name and not id => done,
quite easy, you just have to set the query in getOptions to do that

$query->select('a.id,a.name,b.name AS
originalteam,a.description');
$query->from('#__avansisleague_team AS a,
#__avansisleague_originalteam AS b');
$query->where('a.idoriginalteam = b.id');


and then store the options accordingly

foreach($teams as $team)
{
//echo ("<br/> in JFFOriginalTeam
- team id/name : " . $team->id."/".$team->name) ;
$options[] =
JHtml::_('select.option', $team->id, $team->name, $team->originalteam,
$team->description);
}


2) edit the Team and show instead of the idOriginalTeam a combobox
with the available names out of the database

3) save the Team with of course the correct idOriginalTeam and not the
OriginalTeam.name

Again, that's something quite easy, routine in any language/framework
I came across, and I could brute force it writing probably poor code
compared to what I could do using the Joomla framework correctly...
and the question is similar for one to many and many to many cases.

In other words, how do I modify e.g. the models/forms/team.xml, the
models/fields/team.php and so on to be able to do that ?

Certainly lies the answer somewhere but as far as I've read, any
example I saw in Joomla of e.g. one to one cases rely on the Joomla
categories and that's not what I want, as any component I later on
write might be completely separated from the joomla DB.

Thanks in advance for your help and greetings out of Switzerland

Dmitry Rekun

unread,
Apr 16, 2013, 10:33:34 AM4/16/13
to joomla-de...@googlegroups.com
Hi!

There are no correct or incorrect ways. Just make your form data available for your model and then you can implement any code that you want.
The main flow is always the same: submit the form -> handle it within the controller -> process it with the model -> redirect to the view. Joomla's current implementation allows to do it very easy. If you followed http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5 then you should understand the flow.

May you have some specific questions?

Dmitry

Alan Hartless

unread,
Apr 16, 2013, 3:01:56 PM4/16/13
to joomla-de...@googlegroups.com
Just doing a very quick look at your code, I think a few things are missing/wrong.

First, does that query pull the data you want?  It looks like it should probably have a $query->join()... in there?

Second, our JHtml::_('select.option') call looks a bit funny.  You may want to take another look at the API.  

public static function option($value, $text="", $optKey = "value", $optText = "text", $disable = false) {
...
}

You could also use  JHTML::_('select.genericlist') without having to loop through the data from the database.  For example,

$query->select('a.id,a.name,b.name AS originalteam,a.description');
$query->from('#__avansisleague_team AS a");
$query->join("left", "#__avansisleague_originalteam AS b ON a.idoriginalteam =b.id");
$db->setQuery($query);
$results = $db->loadObjectlist();
$combobox = JHtml::_("select.genericlist", $results, 'selectboxName', '', 'id', 'name', '');

(probably bugs in there as I'm not testing it).

Are you using JForms and thus the reason you are asking about team.xml?  If so, you may want to take a read through http://www.ostraining.com/blog/how-tos/development/getting-started-with-jform/

Thanks,
Alan



--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gilles Volluz-Gasdia

unread,
Apr 17, 2013, 3:42:07 AM4/17/13
to Joomla! General Development
Ok, the answer was quite simple and elegant : use a custom JFormField.... and in fact, the one I already had for the OriginalTeam... works perfectly fine. I wish I could have seen it clearly and simply written way before losing hours on trying to find a solution when it already was under my very eyes...


2013/4/16 Varkhan <grisa...@gmail.com>



--
Elen síla lúmenn' omentielvo
Reply all
Reply to author
Forward
0 new messages