DEPRECATION WARNING

This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.

EXT: Bidirectional M-M relations

Author:Kasper Skårhøj
Created:2002-11-01T00:32:00
Changed by:Mads Brunn
Changed:2005-11-29T23:52:24
Email:mads@typoconsult.dk

EXT: Bidirectional M-M relations

Extension Key: bidirectional

Copyright 2000-2002, mads@typoconsult.dk, <mads@typoconsult.dk>

This document is published under the Open Content License

available from http://www.opencontent.org/opl.shtml

The content of this document is related to TYPO3

- a GNU/GPL CMS/Framework available from www.typo3.com

Table of Contents

EXT: Bidirectional M-M relations 1

Introduction 1

What does it do? 1

Screenshots 2

Users manual 2

FAQ 2

Adminstration 2

FAQ 2

Configuration 2

FAQ 2

Reference 2

Tutorial 3

Known problems 3

To-Do list 3

Changelog 3

Important guidelines 4

Issues with Open Office documentation for TYPO3 4

Inserting images 4

Paragraph styles 4

Linking 5

Meta data and updates 5

Introduction

What does it do?

This extension introduces the concept of bidirectional M-M relations to the TYPO3 framework. NOTE: This extension is only useful for programmers who wish to implement bidirectional relations in their extension tables. You should only go on reading this if you have some experience with extension programming.

M-M relations are already used in many extensions as a mean of establishing a “link” between two tables. For further reading about this see the TYPO3 Core APIs section 4 about the Table Configuration Array (TCA). Since I assume that you already have some knowledge about programming extensions for the TYPO3 framework I will not go into detail on how this works.

The problem at hand

It has bothered me for some time that MM-relations in TYPO3s backend were only singledirectional. To explain what I mean by that, take this example:

You have two tables, tx_myext_table1 and tx_myext_table2 . In tx_myext_table1 you have configured a field of type “select”. The item array of this select field is filled with records from t x_myext_table2 by using an M-M relation. For this purpose a third “join”-table tx_myext_table1_table2_mm is used.

In order to find any relations TYPO3 expects the tx_myext_table1_table2_mm to contain the following fields:

uid_local - this points to the uid of the local table (tx_myext_table1). uid_foreign – this points to the uid of the foreign table (tx_myext_table2).

So far everything is good. You can view, edit and delete relations between the two tables - but only from the local table´s point of view . Now, what if you wanted to create a “select” field in the tx_myext_table2 which was populated with records from the tx_myext_table1 using the same “join” table ( tx_myext_table1_table2_mm) ? The short answer to that question is: You can't.

To give you a real world example, take a look at the tt_news extension. You can open a single tt_news record and assign it to one or more categories. But you can't open a category and select which tt_news records should be assigned to it.

This is were this extension comes in. It allows you to create “select” and “group” fields that uses the uid_local field in the “join” table to lookup records in the foreign table.

Configuration

The extension works by extending the way you configure M-M relations in the $TCA for fields of type “select” and “group”.

((generated))

Addition to Core API's (section 4.2.9 ['columns'][fieldname]['config'] / TYPE: "select")
...

Key

...

Datatype

...

Description

...

Scope

MM

Key

MM

Datatype

string

(table name)

Description

Means that the relation to the records of "foreign_table" / "new_foreign_table" is done with a M-M relation with a third "join" table.

That table typically has three columns:

  • uid_local, uid_foreign for uids respectively.
  • sorting is a required field used for ordering the items.

The fieldname of the config is not used for data-storage anymore but rather it's set to the number of records in the relation on each update, so the field should be an integer.

Notice: Using MM relations you can ONLY store real relations for foreign tables in the list - no additional string values or non-record values.

Additional information for bidirectional M-M relations:

If you add an “|” followed by the keyword “foreign” TYPO3 will use the uid_local field to lookup foreign records.

Example:

'config' => Array(
    'type' => 'select',
    'foreign_table' => 'tx_myext_table1',
    'MM' => 'tx_myext_table1_table2_mm | foreign',
    'maxitems' => 100
)

Scope

Proc.

...

Key

...

Datatype

...

Description

...

Scope

Addition to Core API's (section 4.2.10 ['columns'][fieldname]['config'] / TYPE: "group")
...

Key

...

Datatype

...

Description

...

Scope

MM

Key

MM

Datatype

string

(table name)

Description

Defines MM relation table to use.

Means that the relation to the files/db is done with a M-M relation through a third "join" table.

A MM-table must have these four columns:

  • uid_local - for the local uid.
  • uid_foreign - for the foreign uid.If the "internal_type" is "file" then the "uid_foreign" should be a varchar or 60 or so (for the filename) instead of an unsigned integer as you would use for the uid.
  • tablenames - is required if you use multitable-relations and this field must be a varchar of approx. 30In case of files, the tablenames field is never used.
  • sorting - is a required field used for ordering the items.

The fieldname of the config is not used for data-storage any more but rather it's set to the number of records in the relation on each update, so the field should be an integer.

Additional information for bidirectional M-M relations:

If you add an “|” followed by the keyword “foreign” TYPO3 will use the uid_local field to lookup foreign records.

Example:

'config' => Array(
    'type' => 'group',
    'allowed' => 'tx_myext_table1',
    'MM' => 'tx_myext_table1_table2_mm | foreign',
    'maxitems' => 100
)

Sample SQL table definitions for MM relations look like:

#
# Example MM table for database relations
#

CREATE TABLE example_db_mm (
  uid_local int(11) DEFAULT '0' NOT NULL,
  uid_foreign int(11) DEFAULT '0' NOT NULL,
  tablenames varchar(30) DEFAULT '' NOT NULL,
  sorting int(11) DEFAULT '0' NOT NULL,
  KEY uid_local (uid_local),
  KEY uid_foreign (uid_foreign)
) TYPE=MyISAM;

#
# Example MM table for file attachments
#

CREATE TABLE example_files_mm (
  uid_local int(11) DEFAULT '0' NOT NULL,
  uid_foreign varchar(60) DEFAULT '' NOT NULL,
  sorting int(11) DEFAULT '0' NOT NULL,
  KEY uid_local (uid_local),
  KEY uid_foreign (uid_foreign)
) TYPE=MyISAM;

Scope

Proc.

...

Key

...

Datatype

...

Description

...

Scope

Tutorial

((generated))

Example: Adding a bidirectional field in tt_news

In this section I will go through an example of how to use bidirectional M-M relations. This example extends the tt_news extension by adding a bidirectional field to the tt_news_cat table. NOTE: An implementation of this example is uploaded to the TER in the bidirectional_example extension.

Creating the field in the database

We start by creating the field in the database:

#
# Table structure for table 'tt_news_cat'
#
CREATE TABLE tt_news_cat (
        tx_bidirectionalexample_tt_news int(11) DEFAULT '0' NOT NULL
);
Konfiguring the field

Next, we configure the field and adds it to the $TCA:

 0: <?php
 1: if (!defined ('TYPO3_MODE'))     die ('Access denied.');
 2: $tempColumns = Array (
 3:     "tx_bidirectionalexample_tt_news" => Array (
 4:         "exclude" => 1,
 5:         "label" => "News in this category",
 6:         "config" => Array (
 7:             "type" => "select",
 8:             "foreign_table" => "tt_news",
 9:             "foreign_table_where" => "ORDER BY tt_news.crdate",
10:             "size" => 10,
11:             "minitems" => 0,
12:             "maxitems" => 100,
13:             "MM" => "tt_news_cat_mm | foreign",
14:         )
15:     ),
16: );
17: t3lib_div::loadTCA("tt_news_cat");
18: t3lib_extMgm::addTCAcolumns("tt_news_cat",$tempColumns,1);
19: t3lib_extMgm::addToAllTCAtypes("tt_news_cat","tx_bidirectionalexample_tt_news;;;;1-1-1");
20: ?>

Notice the syntax of line 13

Known problems

((generated))

XCLASS'ing

In order to achieve it's goal this extension XCLASSes “t3lib_loadDBGroup”. In my opinion XCLASS'ing is a very bad way of extending the functionality of TYPO3 and most of the time I only use the technique for “user_*” extensions (extensions that are not going to be made public). Therefore I advise you only to use this extension if you really need the functionality of bidirectional relations.

Sorting

There are some issues with the sorting of items which have still to be cleared out.

List module

Values from bidirectional fields may not display correctly in the List module's “extended mode”.

To-Do list

- Make bidirectional relations part of the core

Changelog

1.0 First release

img-1 EXT: Bidirectional M-M relations - 4