Demon’s Code

Typo3 MM-relation

by Nox on Apr.02, 2013, under PHP

Recently I had a problem with a typo3 plugin.

I needed to connect some clients with a category and the internet failed me.

TL;DR: The steps to do are:

  • Connect 2 models in the extension builder with a n:m relation and save
  • Open one of the relations with your editor and change it by using the other relations table and adding 'MM_opposite_field' => 'fieldname' to the configuration
I wanted to connect a client-model with a categories-model. So I first created the client model
  • Create a new Model
  • Name it "Client" and set the aggregate root flag to have it build the model data
  • Create another Model
  • Name it "Category" and set the aggregate root flag
  • Now add a relation to Client and name it "categories", connect it to the Category-model
  • Add a relation to Category and name it "clients", connect it to the Client-model
  • Save the extension
  • Open Configuration/TCA/Client.php to see:
    'foreign_table' => 'tx_kssclient_domain_model_category',
    'MM' => 'tx_kssclient_client_category_mm',
  • Open Configuration/TCA/Category.php to see:
    'foreign_table' => 'tx_kssclient_domain_model_client',
    'MM' => 'tx_kssclient_category_client_mm',
  • These lines need to be changed to look like this:
    'foreign_table' => 'tx_kssclient_domain_model_client',
    'MM' => 'tx_kssclient_client_category_mm',
    'MM_opposite_field' => 'categories',
  • The generated code was creating to separate n:m-relations. The changed line will turn the category-client n:m-relation into a client-category m:n-relation, which in turn creates the desired m:m relation
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...


All entries, chronologically...