HOWTO: Handle 1:n relationshiops between Article pubtypes in 0.9.10

Michael Härtl <[email protected]> Thu, 23 Sep 2004 14:28:31 +0200
Newsgroups gmane.comp.cms.xaraya.knowledge-base
Organization Xaraya
Message-ID <[email protected]>
Since i know how hard it can be to find all the required information for 
some common scenarios i'll try to share my self-collected wisdom here 
with you ;-).


Scenario
--------

* Lets say, you have defined two article types "Parent" and "Child". You 
want to attach as many child articles to a parent as you like.
(The child pubtypes can of course hold any information you want.)

* For some reason you also don't want to use the example setup from the 
subitems module which uses DD objects to hold all your child 
information. This can be the case when you want to supply the user with 
easy navigation and adminsitration for all your child objects out of the 
box.
(Don't ask me, if this also could easily be achieved with DD objects. 
mikespub? ;-) )


What we'll do
-------------

We'll use a DD object to hold the child article id's. Then we create a 
link between this object and the parent article with subitems.

How to setup
------------

* Create the two pubtypes for parent and child in the articles module 
like described in the xaraya guide.

* Install the Subitems module if not done so yet

* Go to Dynamic Data -> View Objects

* Create a new object for your child id's. You can name and label it as 
you like but avoid using reserved words in xaraya. Leave the other 
fields untouched.

* After creation, your new object should be listed under "View Dynamic 
Objcects".

* Click on "Edit" behind your object and then on "Modify Dynamic Properties"

* Now add a field of type "Item ID" and label it like "myobjectid" or 
something. Click on "Update Properties"

* Add a field type "Dropdown List", also give it a label for later use 
and add this to the field validation:
xarModAPIFunc('articles','user','dropdownlist',array('ptid' => 19))
You must set the ptid to the id of your child publication type. Go to 
the articles Pubtype List, Edit your pubtype and have a look at the URL 
to find out.

Now you have created the neccessary object. If you want, you can already 
try to add an item from your object in DD (DD -> View Objects, <your 
object> "View", New). You should get a dropdown list with all the 
article titles of your child pubtype. But don't create a real object 
here, don't know if this would mess things up later.

To finish setup all you have to do is to tell xaraya that you want to 
link this object to your parent pubtype via sublinks. Here we go:

* Go to Subitems -> Add Link

* Select your just created object from the pulldown, enter "articles" as 
hooked module name and the id of your parent articles pubtype as hooked 
item type. (like above: see list

* Finally go to Modules -> Configure Hooks -> Subitems and create a hook 
for your parent pubtype.

Finished! :-)

Now you should be able to attach child articles to your parent.

For more information on how to work with the child articles in your 
parent template watch out for another HOWTO to come ;-)

Mike H.