0

I have a form that submits data to my controller. The function createAction() takes care for adding a new record to the database.

I need to get a certain value (specialValue) from the previous record in the database, which is neither deleted nor hidden. However, with the following, I can only get the uid (not respecting the deleted/hidden state).

public function createAction(Tx_MyExt_Domain_Model_MyObject $myobject) {
    $this->myObjectRepository->add($myobject);

    $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');        
    $persistenceManager->persistAll();

    $uid = $myobject->getUid();
    $previousMyObject = $this->myObjectRepository->findByUid($uid-1);
    $myobject->setSpecialValue($previousMyObject->getSpecialValue() +1);
}

Is there something like a findPrevious() method for my repository, or do I have to create it myself?

Mateng
  • 3,742
  • 5
  • 37
  • 64

1 Answers1

1

Get the first available (not hidden, not deleted) object with uid less, than new one ($uid in your case), using descending sorting, add this to your repo:

public function findPreviousAvailable($uid) {
    $query = $this->createQuery()
        ->setOrderings(array('uid'=> Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING));

    $query->matching($query->lessThan('uid', $uid));

    return $query->execute()->getFirst();
}

until you won't force ignoring enabled fields, they will be included in query (also user groups, access times etc.)

And... no, afaik there's no such method available out-of-the-box.

biesior
  • 55,576
  • 10
  • 125
  • 182