2016-04-27 49 views
0

将Extbase对象添加到其存储库时,无法设置数据库字段“排序”。TYPO3 CommandController:如何设置Extbase对象的表字段“排序”?

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

我的代码在我的TYPO3命令控制器长相像这样:

//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!

我的语言模型还具有以下附加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

工作液:

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

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

因此添加columns => array(--INSERT HERE--)定义是这样的内部:

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

很有意思只你设置你的排序固定为8 ...我想知道如何使用列表视图中的一些本地排序(拖放或箭头)来排序我的项目和填写表字段排序,也许你有线索。 .. – webMan

1

不要设置现场手动排序。 使用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(); 

这是相同的COMAND TYPO3使用在后端进行排序列表执行COMAND。 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()

注意: DB-Table需要字段“排序”。你需要在文件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', 
    ...