Joomla 3.0.2 - Item Association, How does "key" field in #__associations work?

1,200 views
Skip to first unread message

pe7er

unread,
Dec 14, 2012, 10:11:34 AM12/14/12
to Joomla! General Development
Hi all,

The Item Association in Joomla 3.0.2 is awesome!
The description at http://community.joomla.org/blogs/community/1695-multilanguage-in-302-whats-new.html
is extensive.

However, I can't find technical information about how the
#__associations + "key" field work.
E.g. I have 3 content items:
INSERT INTO `jos_content` VALUES(1, 37, 'testing', 'testing', '<p>one
two three</p>', '', 1, 8, '2012-12-10 06:58:24', 856, [..] 1, 'en-GB',
'');
INSERT INTO `jos_content` VALUES(2, 38, 'testen', 'testen', '<p>een
twee drie</p>', '', 1, 9, '2012-12-10 06:58:43', 856, [..] 1, 'nl-NL',
'');
INSERT INTO `jos_content` VALUES(6, 43, 'Test', 'test', '<p>un deux
trois</p>', '', 1, 10, '2012-12-10 15:55:43', 856, [..] 0, 'fr-FR',
'');

associated with each other, and the #__associations now contains:
id, context, key
1, com_content.item, 299772356e624c6858d75ddc57a06f0e
2, com_content.item, 299772356e624c6858d75ddc57a06f0e
6, com_content.item, 299772356e624c6858d75ddc57a06f0e

I noticed that the "key" field is created upon saving via /
administrator/components/com_content/models/article.php
Between line 441 - 498 it says:

if (!$all_language && count($associations)) {
// Adding new association for these items
$key = md5(json_encode($associations));
$query->clear();
$query->insert('#__associations');

foreach ($associations as $tag => $id) {
$query->values($id.','.$db-
>quote('com_content.item') . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();

My Questions:
1. Can anyone point me to some technical documentation about Item
Association?
2. How does the concept of Item Association work with the "key"
fields?
3. Why are the associations stored as MD5?
4. What SQL statement could you use to create associations directly in
the database using phpMyAdmin?

Thanks in advance!
Peter Martin

pe7er

unread,
Dec 17, 2012, 11:14:52 AM12/17/12
to Joomla! General Development
Thanks Reynaldo!

One additional question:
md5 is a one way encryption/hashing technique, isn't it?
So how does Joomla determine which associated item should be displayed
when a visitor switches language?
How does it determine the md5 hash for the current article?

When it has the md5 hash for the current article,
does it look-up the record for the current item (article),
and then retrieve all records with the same md5 hash (Joined with the
#__content table, which contains the language field) ?

Thanks,
Peter


On Dec 14, 9:28 pm, reynaldo celaya III <rcela...@gmail.com> wrote:
> Hi Peter, ive also been exploring associations and have a good idea of
> whats happening and how to create the sql to associate items
>
> when you save a article item it first deletes all associations that the
> item currently has:
> DELETE FROM #__associations WHERE context='com_content.item' AND id IN (2,1)
>
> It makes a md5 hash of the associations array to use as a unique key. the
> associations array looks like so (var_dump)
> { ["es-ES"]=> string(1) "2" ["en-GB"]=> string(1) "1" }
>
> it then inserts the associated items into the associations db table...
> where id is the article id, context is the item component context(article,
> category..whatever...etc ) and the key is the unique hashed array value
> INSERT INTO #__associations VALUES
> (2,'com_content.item','d3e83603738239d466b1a1d7d3aad3c8'),(1,'com_content.i tem','d3e83603738239d466b1a1d7d3aad3c8')
>
> the md5 is just a better way to store and uniquely identify the
> associations as opposed to a string of array values/ids.
>
> I would just follow the same steps as article to use this table
> yourself....
> delete associations
> calculate new key
> insert new associations
>
>
>
>
>
>
>
> On Friday, December 14, 2012 7:11:34 AM UTC-8, pe7er wrote:
>
> > Hi all,
>
> > The Item Association in Joomla 3.0.2 is awesome!
> > The description at
> >http://community.joomla.org/blogs/community/1695-multilanguage-in-302...

Herman Peeren

unread,
Dec 17, 2012, 2:56:57 PM12/17/12
to joomla-de...@googlegroups.com
On Monday, 17 December 2012 17:14:52 UTC+1, pe7er wrote:
md5 is a one way encryption/hashing technique, isn't it?
So how does Joomla determine which associated item should be displayed
when a visitor switches language?
How does it determine the md5 hash for the current article?

 
The md5-hash is just a unique key that is generated to associate the different records in the associations table. It doesn't matter what it contains as long as it's the same for all associated items. The current item can be retrieved in the table (context and id is known) and so Joomla knows what the unique key is. Then, the other records with the same key can be retrieved, exactly as you describe:


On Monday, 17 December 2012 17:14:52 UTC+1, pe7er wrote:

pe7er

unread,
Dec 18, 2012, 6:48:13 PM12/18/12
to Joomla! General Development
Ah, great. I understand... Thanks for your explanation Herman!

Herman Peeren

unread,
Dec 19, 2012, 2:42:05 AM12/19/12
to joomla-de...@googlegroups.com
Thinking about why it was done how it is done with this hashed value, I appreciated it more and more.

I tried to think out an alternative way:
  • Joomla could have taken the context + id of the item you are editing as key. Then the key would be changed every time you edit a version of that item in an other language. Would surely give proplems with concurrent editing by several people.
  • Joomla could have taken the context + id in the default language version of the item you are editing. It would give problems when the default language changes. And what if there is no item in the default language?
  • So, the unique key should contain a unique identification (= context + id) of the different items that are linked. A string consisting of those different parts. But if you add more and more items to it, how long could such a key become then? A JSON-string is an easy way to serialise an array, but it can still become lengthy. By MD5-hashing it, it's length is limited to 128bits = 16 bytes = a string of 32 hexadecimale "numbers"; so key is char(32).
- Herman

pe7er

unread,
Dec 20, 2012, 8:56:03 AM12/20/12
to Joomla! General Development
Yes, I agree that it's indeed a marvelous method!

I knew that the key was generated with md5, but I didn't understand
how it worked.

I just experimented somewhat more with Joomla 3.0.2 & Item Association
and noticed how it worked:
It does indeed not matter what is used as basis for the md5 key.
The first field (id) is *not* autoincrement.
The combination "id" + context ("com_content.item") refers to the
content table + jos_content.id field.

I have 3 records in jos_content that belong together: 4070 = EN, 4071
= FR and 4072 = PT.
With the following query (notice: 1234567890abcde1234567890abcde12 =
random)
I was able to associate them via phpMyAdmin:

INSERT INTO `jos_associations` VALUES('4070', 'com_content.item',
'1234567890abcde1234567890abcde12');
INSERT INTO `jos_associations` VALUES('4071', 'com_content.item',
'1234567890abcde1234567890abcde12');
INSERT INTO `jos_associations` VALUES('4072', 'com_content.item',
'1234567890abcde1234567890abcde12');

Thanks!
Peter

On Dec 19, 8:42 am, Herman Peeren <herman.pee...@gmail.com> wrote:
> Thinking about why it was done how it is done with this hashed value, I
> appreciated it more and more.
>
> I tried to think out an alternative way:
>
>    - Joomla could have taken the context + id of the item you are editing
>    as key. Then the key would be changed every time you edit a version of that
>    item in an other language. Would surely give proplems with concurrent
>    editing by several people.
>    - Joomla could have taken the context + id in the default language
>    version of the item you are editing. It would give problems when the
>    default language changes. And what if there is no item in the default
>    language?
>    - So, the unique key should contain a unique identification (= context +
Reply all
Reply to author
Forward
0 new messages