Benutzer: Gast
Eintrag: Extbase: Bidirektionale MM-Relation

Extbase: Bidirektionale MM-Relation

von: marcel
Tags: TCA, Extbase, Typo3
Kreise: Typo3

ext_tables.sql

Code: SQL
#
# Table structure for table 'tx_exttitle_domain_model_aaa'
#
CREATE TABLE tx_exttitle_domain_model_aaa (

	uid int(11) NOT NULL auto_increment,
	pid int(11) DEFAULT '0' NOT NULL,

	bbb int(11) unsigned DEFAULT '0' NOT NULL,

	tstamp int(11) unsigned DEFAULT '0' NOT NULL,
	crdate int(11) unsigned DEFAULT '0' NOT NULL,
	cruser_id int(11) unsigned DEFAULT '0' NOT NULL,
	deleted tinyint(4) unsigned DEFAULT '0' NOT NULL,
	hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
	starttime int(11) unsigned DEFAULT '0' NOT NULL,
	endtime int(11) unsigned DEFAULT '0' NOT NULL,

	PRIMARY KEY (uid),
	KEY parent (pid),
);

#
# Table structure for table 'tx_exttitle_domain_model_bbb'
#
CREATE TABLE tx_exttitle_domain_model_bbb (

	uid int(11) NOT NULL auto_increment,
	pid int(11) DEFAULT '0' NOT NULL,
	
	aaa int(11) unsigned DEFAULT '0' NOT NULL,

	tstamp int(11) unsigned DEFAULT '0' NOT NULL,
	crdate int(11) unsigned DEFAULT '0' NOT NULL,
	cruser_id int(11) unsigned DEFAULT '0' NOT NULL,
	deleted tinyint(4) unsigned DEFAULT '0' NOT NULL,
	hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
	starttime int(11) unsigned DEFAULT '0' NOT NULL,
	endtime int(11) unsigned DEFAULT '0' NOT NULL,

	PRIMARY KEY (uid),
	KEY parent (pid),
);

#
# Table structure for table 'tx_exttitle_bbb_aaa_mm'
#
CREATE TABLE tx_exttitle_bbb_aaa_mm (
	uid_local int(11) unsigned DEFAULT '0' NOT NULL,
	uid_foreign int(11) unsigned DEFAULT '0' NOT NULL,
	sorting int(11) unsigned DEFAULT '0' NOT NULL,
	sorting_foreign int(11) unsigned DEFAULT '0' NOT NULL,
	bbb int(11) unsigned DEFAULT '0' NOT NULL,

	KEY uid_local (uid_local),
	KEY uid_foreign (uid_foreign)
);

TCA Aaa.php

Code: PHP
'bbb'           => array(
  'exclude' => 0,
  'label'   => 'BBB',
  'config'  => array(
    'type'          => 'group',
    'internal_type' => 'db',
    'foreign_table' => 'tx_exttitle_domain_model_bbb',
    'allowed'       => 'tx_exttitle_domain_model_bbb',
    'MM'            => 'tx_exttitle_bbb_aaa_mm',
    'size'          => 10,
    'maxitems'      => 99999,
  ),
),

TCA Bbb.php

Code: PHP
'aaa'           => array(
  'exclude' => 0,
  'label'   => 'AAA',
  'config'  => array(
    'type'          => 'group',
    'internal_type' => 'db',
    'foreign_table' => 'tx_exttitle_domain_model_aaa',
    'allowed'       => 'tx_exttitle_domain_model_aaa',
    'MM'            => 'tx_exttitle_bbb_aaa_mm',
    'MM_opposite_field' => 'bbb',
    'size'          => 10,
    'maxitems'      => 99999,
  ),
),

Domain/Model/Aaa.php

Code: PHP
<?php
namespace VENDOR\Exttitle\Domain\Model;

class Aaa extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

  /**
   * bbb
   *
   * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Bbb>
   * @lazy
   */
  protected $bbb;
  

  /**
   * __construct
   */
  public function __construct() {
    $this->initStorageObjects();
  }

  /**
   * Initializes all ObjectStorage properties.
   *
   * @return void
   */
  protected function initStorageObjects() {
    $this->bbb = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
  }

  /**
   * Adds a Bbb
   *
   * @param \VENDOR\Exttitle\Domain\Model\Bbb $bbb
   * @return void
   */
  public function addBbb(\VENDOR\Exttitle\Domain\Model\Bbb $bbb) {
    $this->bbb->attach($bbb);
  }

  /**
   * Removes a Bbb
   *
   * @param \VENDOR\Exttitle\Domain\Model\Bbb $bbbToRemove The Bbb to be removed
   * @return void
   */
  public function removeBbb(\VENDOR\Exttitle\Domain\Model\Bbb $bbbToRemove) {
    $this->bbb->detach($bbbToRemove);
  }

  /**
   * Returns the bbb
   *
   * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Bbb> $bbb
   */
  public function getBbb() {
    return $this->bbb;
  }

  /**
   * Sets the bbb
   *
   * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Bbb> $bbb
   * @return void
   */
  public function setBbb(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $bbb) {
    $this->bbb = $bbb;
  }

}
?>

Domain/Model/Bbb.php

Code: PHP
<?php
namespace VENDOR\Exttitle\Domain\Model;

class Bbb extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

  /**
   * aaa
   *
   * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Aaa>
   * @lazy
   */
  protected $aaa;
  

  /**
   * __construct
   */
  public function __construct() {
    $this->initStorageObjects();
  }

  /**
   * Initializes all ObjectStorage properties.
   *
   * @return void
   */
  protected function initStorageObjects() {
    $this->aaa = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
  }

  /**
   * Adds a Aaa
   *
   * @param \VENDOR\Exttitle\Domain\Model\Aaa $aaa
   * @return void
   */
  public function addAaa(\VENDOR\Exttitle\Domain\Model\Aaa $aaa) {
    $this->aaa->attach($aaa);
  }

  /**
   * Removes a Aaa
   *
   * @param \VENDOR\Exttitle\Domain\Model\Aaa $aaaToRemove The Aaa to be removed
   * @return void
   */
  public function removeAaa(\VENDOR\Exttitle\Domain\Model\Aaa $aaaToRemove) {
    $this->aaa->detach($aaaToRemove);
  }

  /**
   * Returns the aaa
   *
   * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Aaa> $aaa
   */
  public function getAaa() {
    return $this->aaa;
  }

  /**
   * Sets the aaa
   *
   * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\VENDOR\Exttitle\Domain\Model\Aaa> $aaa
   * @return void
   */
  public function setAaa(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $aaa) {
    $this->aaa = $aaa;
  }

}
?>
(1) Artem Schander schrieb am 20. 2. 2014 um 16:05 Uhr
Hallo Marcel, danke für das Tutorial. Es hat mir sehr geholfen :)
In der ext_tables.sql hat sich jedoch in Zeile 31 ein Fehler eingeschlichen. Das "tx_exttitle_domain_model_bbb" sollte wohl eher auf "aaa" verweisen ;)

Grüsse Artem
(2) marcel schrieb am 21. 2. 2014 um 10:46 Uhr

Danke Artem,

hab’s korrigiert – das kommt wohl davon, wenn man versucht Bezeichner für die Webseite neu zu setzen ;)

(3) Jacques Valet schrieb am 4. 2. 2015 um 23:33 Uhr
Hi Marcel

Warum braucht es das 'bbb' in der Tabelle tx_exttitle_bbb_aaa_mm?

Viele Grüsse
Jacques
(4) marcel schrieb am 5. 2. 2015 um 17:59 Uhr

Hi Jacques,
darüber bin ich auch schon gestolpert und konnte mir keinen Reim daraus machen. Ohne geht/ging es nicht. Es wird im TCA als MM_opposite_field angegeben, obwohl in der DB keine Werte enthalten sind – bestimmt irgendeine interne Sache im Repo.
LG MArcel

(5) Tobi Kummer schrieb am 30. 12. 2015 um 10:42 Uhr
Ahhhh, Marcel, guter Junge! Hast mir grad den Arsch gerettet mir dem Tutorial!
Bei mir klappt das auch bestens ohne den "bbb" in der MM-Tabelle.
Denke, den brauchst du nicht. Der Parameter "MM_opposite_field" verweist auf das Feld in der anderen Tabelle, nicht in der MM-Tabelle. Bei dir also "bbb", was richtig ist. Müsste so klappen.
LG Tobi