TYPO3 CommandController: How to set table field “sorting” of Extbase Object?

五迷三道 提交于 2019-12-08 09:03:32

问题


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

回答1:


WORKING SOLUTION:

Finally i found the missing part by myself.

If you want to manipulate a database field from an FrontendPlugin or an CommandControllerTask like in my case "sorting" you have to add a definition of this "field" in the TCA of the corresponding table.

Therefore add inside the columns => array( --INSERT HERE-- ) definition something like this:

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



回答2:


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


来源:https://stackoverflow.com/questions/36896377/typo3-commandcontroller-how-to-set-table-field-sorting-of-extbase-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!