TYPO3 CommandController:如何设置表字段"排序" Extbase对象?

时间:2016-04-27 17:05:31

标签: typo3 extbase typo3-6.2.x

我无法设置数据库字段"排序"将Extbase对象添加到它的存储库时。

其他数据库字段已正确填写,但不知何故$this->language->setSorting(8) 未将数据库字段排序设置为为8.在我的情况下,值为总是0。

My Code在我的TYPO3 CommandController看起来像这样:

//Inject vars
/**
 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
 * @inject
 */
protected $objectManager;

/**
 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
 * @inject
 */
protected $persistenceManager;

/**
 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings
 * @inject
 */
protected $querySettings;

/**
 * languageRepository
 *
 * @var \ITCENTER\ItcJobcenter\Domain\Repository\LanguageRepository
 * @inject
 */
protected $languageRepository;

public function languageApiCommand($storagePid, \DateTime $dateTime = null) {

    // Set storagePid from "Command Controller Task" $storagePid
    $this->storagePid = $storagePid;

    // Query-Settings (PID)
    $this->querySettings->setStoragePageIds(array($this->storagePid));
    $this->languageRepository->setDefaultQuerySettings($this->querySettings);

    // Create my neue language object
    $this->language = $this->objectManager->get('\ITCENTER\ItcJobcenter\Domain\Model\Language');
    $this->language->setTitle("MyT itle");
    $this->language->setPid($this->storagePid);
    $this->language->setSorting(8);
    $this->languageRepository->add($this->language);

    // Persist new language object to database
    $this->persistenceManager->persistAll();
}

数据库字段称为排序并且存在! 我还设置了一个变量"排序"和LanguageModel中的getter / setter!

我的LanguageModel有这个额外的代码部分:

/**
 * @var integer
 */
protected $sorting;

/**
 * Get sorting
 *
 * @return integer
 */
public function getSorting() {
    return $this->sorting;
}

/**
 * Set sorting
 *
 * @param integer $sorting sorting
 * @return void
 */
public function setSorting($sorting) {
    $this->sorting = $sorting;
} 

2 个答案:

答案 0 :(得分:1)

工作解决方案:

最后,我自己找到了缺失的部分。

如果你想从FrontendPlugin或CommandControllerTask操作数据库字段,就像在我的情况下“排序”一样,你必须 TCA中添加这个“字段”的定义对应表

因此在columns => array( --INSERT HERE-- )定义内添加如下内容:

columns => array(
    'sorting' => array(
        'config' => array(
            'type' => 'passthrough',
        ),
    ),
)

答案 1 :(得分:1)

不要手动设置字段排序。 使用DataHandler类进行排序。

如果您想更改条目的排序位置,请使用如下命令:

$cmd[ tablename ][ uid ][ command ] = value

您可以在此处找到更多信息:

https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/Database/Index.html

使用命令移动,您可以交换表格中条目的位置。

当您创建了命令时,您可以使用此代码执行

$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$tce->start(array(), $cmd);
$tce->process_cmdmap();

这与TYPO3在后端用于排序列表的命令相同。 Typo3 Backend

有关DataHandler调用的更多信息,请查看此处:

https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/UsingDataHandler/Index.html

示例存储库:

    class SortedRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
    protected $defaultOrderings = array(
        'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
    );

    public function moveUp($entity)
    {

        $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
        $tce->stripslashes_values = 0;
        $entityTwoBefore = $this->getTwoBefore($entity);

        if ($entityTwoBefore != null) {
            //category is minimum 3
            //can set over UID
            $cmd[$this->getTableName($entity)][$entity->getUid()]['move']= 0-$entityTwoBefore->getUid();
        } else {
            //can only set over pid
            $cmd[$this->getTableName($entity)][$entity->getUid()]['move']= Util::getStorageID();
        }

        $tce->start(array(), $cmd);
        $tce->process_cmdmap();

    }

    public function moveDown($entity)
    {
        $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
        $tce->stripslashes_values = 0;

        $nextEntity = $this->getNext($entity);

        if ($nextEntity != null) {
            $cmd[$this->getTableName($entity)][$entity->getUid()]['move'] = 0 - $nextEntity->getUid();
        }

        $tce->start(array(), $cmd);
        $tce->process_cmdmap();
    }

    private function getNext($entity)
    {
        $entities = $this->findAll();
        $match = false;
        foreach ($entities as $entityFor) {
            if ($entityFor->getUid() == $entity->getUid()) {
                $match = true;
                continue;
            }
            if ($match == true) {
                return $entityFor;
            }
        }
    }

    private function getBefore($entity)
    {
        $entities = array_reverse($this->findAll()->toArray());
        $match = false;
        foreach ($entities as $entityFor) {
            if ($entityFor->getUid() == $entity->getUid()) {
                $match = true;
                continue;
            }
            if ($match == true) {
                return $entityFor;
            }
        }
    }

    private function getTwoBefore($entity)
    {
        $entityTwoBefore = null;
        $entityBefore = $this->getBefore($entity);
        if ($entityBefore != null) {
            $entityTwoBefore = $this->getBefore($entityBefore);
        }

        return $entityTwoBefore;

    }

    /**
     * Return the current tablename
     *
     * @return string
     */
    private function getTableName($entity) {

        /** @var DataMapper $dataMapper */
        $dataMapper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');

        return $dataMapper->getDataMap(get_class($entity))->getTableName();
    }
}

如果您的存储库扩展了SortedRepository,您可以使用方法 moveUp() moveDown()

注意:数据库表需要字段"排序"。您需要在ext_tables.sql文件和模型类的TCA中使用它:

ext_tables.sql:

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

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

    name varchar(255) DEFAULT '' 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,

    t3ver_oid int(11) DEFAULT '0' NOT NULL,
    t3ver_id int(11) DEFAULT '0' NOT NULL,
    t3ver_wsid int(11) DEFAULT '0' NOT NULL,
    t3ver_label varchar(255) DEFAULT '' NOT NULL,
    t3ver_state tinyint(4) DEFAULT '0' NOT NULL,
    t3ver_stage int(11) DEFAULT '0' NOT NULL,
    t3ver_count int(11) DEFAULT '0' NOT NULL,
    t3ver_tstamp int(11) DEFAULT '0' NOT NULL,
    t3ver_move_id int(11) DEFAULT '0' NOT NULL,
    sorting int(11) DEFAULT '0' NOT NULL,

    sys_language_uid int(11) DEFAULT '0' NOT NULL,
    l10n_parent int(11) DEFAULT '0' NOT NULL,
    l10n_diffsource mediumblob,

    PRIMARY KEY (uid),
    KEY parent (pid),
    KEY t3ver_oid (t3ver_oid,t3ver_wsid),
 KEY language (l10n_parent,sys_language_uid)

);

在模型的TCA中:

    <?php
return array(
    'ctrl' => array(
        'title' => 'LLL:EXT:pdvdownloadportal/Resources/Private/Language/locallang_db.xlf:tx_pdvdownloadportal_domain_model_subcategory',
        'label' => 'name',
        'tstamp' => 'tstamp',
        'crdate' => 'crdate',
        'cruser_id' => 'cruser_id',
        'dividers2tabs' => TRUE,
        'versioningWS' => 2,
        'versioning_followPages' => TRUE,
        'sortby' => 'sorting',
    ...