简体   繁体   中英

how to set and save data in database in typo3

I have this code of the controller's index action.

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

my main question is that how to save data to the database using set method of model. the current one sets 'testings...' when I dump but doesn't save it to database. I am using typo3 7.6

You have to pass your object to repository which saves it to database. Method which you need to use differs depends on object already exists or it's new one.

For new object you have to use add() method:

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

And for existing one use update() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM