Typo3 MM关系

时间:2014-12-29 15:51:43

标签: typo3 extbase typo3-6.2.x

我在两个表之间有一个n:m的关系,所以typo3扩展构建器为我生成了一个mm表。

所以现在我想在这两个表之间添加一个关系吗?

一个表称为pruefling,一个称为fach

并且mm表称为fach_pruefling_mm

我试过这个:

$fach->setMatrikelnr($pruefling->getUid());

扩展构建器如下所示: enter image description here

更新 Fach模型

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

/**
 * fachnr
 *
 * @var string
 * @validate NotEmpty
 */
protected $fachnr = '';

/**
 * fachname
 *
 * @var string
 * @validate NotEmpty
 */
protected $fachname = '';

/**
 * pruefer
 *
 * @var string
 * @validate NotEmpty
 */
protected $pruefer = '';

/**
 * notenschema
 *
 * @var string
 * @validate NotEmpty
 */
protected $notenschema = '';

/**
 * modulnr
 *
 * @var string
 * @validate NotEmpty
 */
protected $modulnr = '';

/**
 * matrikelnr
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\ReRe\Rere\Domain\Model\Pruefling>
 */
protected $matrikelnr = NULL;

/**
 * Returns the fachnr
 *
 * @return string $fachnr
 */
public function getFachnr() {
    return $this->fachnr;
}

/**
 * Sets the fachnr
 *
 * @param string $fachnr
 * @return void
 */
public function setFachnr($fachnr) {
    $this->fachnr = $fachnr;
}

/**
 * Returns the fachname
 *
 * @return string $fachname
 */
public function getFachname() {
    return $this->fachname;
}

/**
 * Sets the fachname
 *
 * @param string $fachname
 * @return void
 */
public function setFachname($fachname) {
    $this->fachname = $fachname;
}

/**
 * Returns the pruefer
 *
 * @return string $pruefer
 */
public function getPruefer() {
    return $this->pruefer;
}

/**
 * Sets the pruefer
 *
 * @param string $pruefer
 * @return void
 */
public function setPruefer($pruefer) {
    $this->pruefer = $pruefer;
}

/**
 * Returns the notenschema
 *
 * @return string $notenschema
 */
public function getNotenschema() {
    return $this->notenschema;
}

/**
 * Sets the notenschema
 *
 * @param string $notenschema
 * @return void
 */
public function setNotenschema($notenschema) {
    $this->notenschema = $notenschema;
}

/**
 * __construct
 */
public function __construct() {
    //Do not remove the next line: It would break the functionality
    $this->initStorageObjects();
}

/**
 * Initializes all ObjectStorage properties
 * Do not modify this method!
 * It will be rewritten on each save in the extension builder
 * You may modify the constructor of this class instead
 *
 * @return void
 */
protected function initStorageObjects() {
    $this->matrikelnr = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}

/**
 * Returns the modulnr
 *
 * @return string $modulnr
 */
public function getModulnr() {
    return $this->modulnr;
}

/**
 * Sets the modulnr
 *
 * @param string $modulnr
 * @return void
 */
public function setModulnr($modulnr) {
    $this->modulnr = $modulnr;
}

public function getMatrikelnr() {
    return $this->matrikelnr;
}

/**
 * @param $matrikelnr
 */
public function setMatrikelnr($matrikelnr) {
    $this->matrikelnr = $matrikelnr;
}

}

1 个答案:

答案 0 :(得分:2)

事实上,您需要使用方法addMatrikelnr(...)removeMatrikelnr(...)来添加和删除关系,它们是由Fach模型中的Builder生成的。

看一下模型中的setMatrikelnr方法,它将集合而不是单个对象作为参数。

m:n关系有一套典型的方法:

<?php
namespace ReRe\Rere\Domain\Model;

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

    /**
     * matrikelnr
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\ReRe\Rere\Domain\Model\Pruefling>
     */
    protected $matrikelnr = NULL;

    /**
     * __construct
     */
    public function __construct() {
        //Do not remove the next line: It would break the functionality
        $this->initStorageObjects();
    }

    /**
     * Initializes all ObjectStorage properties
     * Do not modify this method!
     * It will be rewritten on each save in the extension builder
     * You may modify the constructor of this class instead
     *
     * @return void
     */
    protected function initStorageObjects() {
        $this->matrikelnr = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }

    /**
     * Adds a Pruefling
     *
     * @param \ReRe\Rere\Domain\Model\Pruefling $matrikelnr
     * @return void
     */
    public function addMatrikelnr(\ReRe\Rere\Domain\Model\Pruefling $matrikelnr) {
        $this->matrikelnr->attach($matrikelnr);
    }

    /**
     * Removes a Pruefling
     *
     * @param \ReRe\Rere\Domain\Model\Pruefling $matrikelnrToRemove The Pruefling to be removed
     * @return void
     */
    public function removeMatrikelnr(\ReRe\Rere\Domain\Model\Pruefling $matrikelnrToRemove) {
        $this->matrikelnr->detach($matrikelnrToRemove);
    }

    /**
     * Returns the matrikelnr
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\ReRe\Rere\Domain\Model\Pruefling> $matrikelnr
     */
    public function getMatrikelnr() {
        return $this->matrikelnr;
    }

    /**
     * Sets the matrikelnr
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\ReRe\Rere\Domain\Model\Pruefling> $matrikelnr
     * @return void
     */
    public function setMatrikelnr(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $matrikelnr) {
        $this->matrikelnr = $matrikelnr;
    }

}