如何在数据库中设置和保存数据在typo3中

如何在数据库中设置和保存数据在typo3中

问题描述:

我有控制器索引操作的代码。如何在数据库中设置和保存数据在typo3中

public function indexAction() { 
      $this->initContentObject(); 
      $titleforSocial = $GLOBALS['TSFE']->page['title']; 
      $uidforSocial = $GLOBALS['TSFE']->page['uid']; 
      $pidforSocial = $GLOBALS['TSFE']->page['pid']; 
      echo "title: ".$titleforSocial . " uid: ". $uidforSocial . " pid:" .$pidforSocial; 

      $elementUid = $this->cObj->data['_LOCALIZED_UID'] ? $this->cObj->data['_LOCALIZED_UID'] : $this->cObj->data['uid']; 
      $buttonResults = $this->contentRepository->findByIdentifier($elementUid); 
      $pagesResults = $this->pagesRepository->findByIdentifier($elementUid); 
      $button_text = $buttonResults->getButtontext(); 
      $page_title = $pagesResults->setTitle('testing...'); 
      var_dump($button_text); 
      $pagesResultsz = $this->pagesRepository->findByIdentifier($elementUid); 
      var_dump($pagesResultsz); 
      exit; 
      $button_text = $this->cObj->data['buttonText']; 
      $this->view->assign("button_text", $button_text); 
    } 

我的主要问题是如何使用set model方法将数据保存到数据库中。当我转储但不保存到数据库时,当前设置'测试...'。 我正在使用typo3 7.6

您必须将您的对象传递给将其保存到数据库的存储库。您需要使用的方法不同取决于对象是否已经存在,或者它是新的。

对于新对象,你必须使用add()方法:

$this->pagesRepository->add($pagesResults); 

而对于现有的使用update()

$this->pagesRepository->update($pagesResults); 
+0

我已经尝试过这种 $这个 - > pagesRepository->更新($ pagesResults); 但它只是设置值,并不保存到数据库。 我是否需要在我的仓库中定义update()? – NK143

+0

如果您的存储库扩展'Repository',则不需要。尝试注入'PersistenceManager'并将对象传递给'update()'调用'$ this-> persistanceManager-> persistAll();'。 – marian0

+0

你能告诉我什么是PersistenceManager和我们为什么使用它? 我是typo3的新手,所以不太了解它。 – NK143