TYPO3 CommandController:如何设置表字段的“排序".的Extbase对象? [英] TYPO3 CommandController: How to set table field "sorting" of Extbase Object?

查看:75
本文介绍了TYPO3 CommandController:如何设置表字段的“排序".的Extbase对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将Extbase对象添加到其存储库中时,我无法将数据库字段设置为排序".

其他数据库字段已正确填充,但是以某种方式$this->language->setSorting(8) 未设置数据库字段 sorting 为8.在我的情况下,该值始终为0. /p>

我的代码在我的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中设置了一个变量"sorting"和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;
} 

解决方案

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

如果要更改条目的排序位置,请使用comand之类的

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

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

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

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

创建了comand后,便可以使用以下代码执行

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

这与TYPO3在后端中用于对列表进行排序的命令相同.

有关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',
    ...

I can't set the database field "sorting" when adding an Extbase Object to it's Repository.

Other database fields are filled correctly, but somehow $this->language->setSorting(8) isn't setting the database field sorting to 8. In my case the value is always 0.

My Code looks in my TYPO3 CommandController looks like this:

//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();
}

Database field is called sorting and is existing! I also set a variable "sorting" and getter/setter in the LanguageModel!

My LanguageModel has this additional codepart:

/**
 * @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;
} 

解决方案

Don't set the field sorting manually. Use the DataHandler class for sorting.

When you want do change the sorting position of an entry use a comand like:

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

You can find more information right here:

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

With the comand move you can swap the position of an entry in the table.

When you have the created the comand you can it execute with this code

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

This is the same comand TYPO3 uses in the Backend for sorting a list.

For more Information over the DataHandler call look here:

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

Example Repository:

    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();
    }
}

If your Repository extends SortedRepository you can use the Methods moveUp() and moveDown().

Note: The DB-Table need the field "sorting". You need it in the file ext_tables.sql and in the TCA of the model class:

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)

);

And in the TCA of the Model:

    <?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',
    ...

这篇关于TYPO3 CommandController:如何设置表字段的“排序".的Extbase对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆