2
votes

I would like to build a preview page for a create form. I set "deleted" property of the record to "1" when in previewAction because in the BE the list module is used to approve the inserted records - so if the record was never finally saved its deleted anyway.

Problem: I can create the record (deleted=1) - I can jump back to the form (no history back for I have to keep the created object). But if I submit again the property mapping tells me

Object of type MyModel with identity "3" not found.

Of course that's because its deleted. The settings in the Repository to ignore deleted are not taking action here.

Yes I could bypass the Extbase magic by filling up everything manually, but this is not what I want.

Here is the action to get an idea what I'm trying

 /**
 * action preview
 *
 * @param MyModel
 * @return void
 */
public function previewAction(MyModel $newModel)
{
    //check if model was already saved
    $uid = $this->request->hasArgument('uid') ? this->request->getArgument('uid') : 0;
    if($uid){
        $newModel = $this->myRepository->findDeletedByUid($uid);
        $this->myRepository->update($newModel);
    }
    else{
        $newModel->setDeleted(true);
        $this->myRepository->add($newModel);
    }

    $this->view->assign('ad', $newModel);
    $this->persistenceManager->persistAll();

    $uid = $this->persistenceManager->getIdentifierByObject($newModel);
    $this->view->assign('uid', $uid);
}

Any ideas?

4

4 Answers

5
votes

The Extbase default query settings suppress deleted objects.

Since you've already stated the custom query findDeletedByUid() in your repository, you just need to set it to include deleted records. It is important, however, that if you want to call your controller action using the object, you'll have to retrieve it before calling the action. Use an initialization action for that. The initializaton will be called automatically before the action.

If you want to set wether the object is deleted, you'll also going to need to define a property, getter and setter in your Domain Model and a proper definition in your tca to enable the data mapper to access the column.

In the repository:

public function findDeletedByUid($uid) {
    $query = $this->createQuery();
    $query->getQuerySettings()->setIncludeDeleted(true);
    $query->matching(
        $query->equals('uid',$uid)
    );
    return $query->execute();
}

In your Controller class:

/**
 * initialize action previewAction
 * Overrides the default initializeAction with one that can retrieve deleted objects
 */
public function initializePreviewAction(){
    if( $this->request->hasArgument('mymodel') ){
      $uid = $this->request->getArgument('mymodel');
      if( $mymodel = $this->mymodelRepository->findDeletedByUid($uid) ){
          $this->request->setArgument($mymodel);
      } else {
          // handle non retrievable object here
      }
    } else {
      // handle missing argument here
    }
}

In your Domain Model:

...
/**
 * @var bool
 */
protected $deleted;

/**
 * @return bool
 */
public function getDeleted() {
    return $this->deleted;
}

/**
 * @param bool $deleted
 */
public function setDeleted($deleted) {
    $this->deleted = $deleted;
}

In your tca.php

...
'deleted' => array(
    'exclude' => 1,
    'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.deleted',
    'config' => array(
        'type' => 'check',
    ),
),
0
votes

Instead of doing any magic with deleted, you should use the hidden field to allow editors to preview documents. You can tell your query to include hidden records inside the repository.

0
votes

Your findDeletedByUid($uid) function caught my eye. If it's not a custom function, should it use something like findByDeleted(TRUE) or findByDeleted(1) in combination with ->getFirst() or ->findByUid()? You can find discussions in the Extbase manual reference and the Repository __call() function API sections.

0
votes

Thanks for all hints.

I think depending to the answers its not possible without bypass extbase property-mapping magic. So I think in general its not a good idea to do it like that.

So I put now my own flag "stored" to the model.

In BE List-Module the not "stored" objects are still visible, but using an own BE Module or deleting the not "stored" object by a cron-job should do the job.

If anyone has a bedder idea feel free to share it :-)