TYPO3: Set more than one storage pid for one extension

2.6k views Asked by At

I builded an extension that has a 'details' table that holds details with a title and a description that be included inline to another object. Right now new details are stored in the same pid as the object, but I'd like to change that.

this question was answered by Merec and in the comments he points to a solution (add the column "pid" to your model, this is the first the model looks at) but asked to formulate a separate question for it ...

I took his suggestion but could not get it to work, so here is the separate question, in addition I would like to know how to get a value from the configuration to be used as pid for this.

update: René Pflamm pointed out that I should underline that I'm trying to set this Pid for saving in the backend, not in the frontend ... I basically recognized this destinction later on

my constants.ts :

plugin.tx_myext {
  persistence {
    # cat=plugin.tx_myext/storage/a; type=string; label=Default storage PID
    defaultStoragePid =
    # cat=plugin.tx_myext/storage/a; type=string; label=Details storage PID
    detailsStoragePid =
  }
}

my setup.ts

plugin.tx_myext {
  persistence {
    storagePid = {$plugin.tx_myext.persistence.defaultStoragePid}
    detailPid = {$plugin.tx_myext.persistence.detailsStoragePid}
  }
}
3

There are 3 answers

7
René Pflamm On

You can, when create elements in your extension, say the model which pid should be use.

In your TS:

plugin.tx_myext.settings {
  detailPid = {$plugin.tx_myext.persistence.detailsStoragePid}
}

In your code above it can look like:

public function createDetailsAction(Detail $detail) {
  $detail->setPid($this->settings['detailPid']);
  $this->detailRepository->add($detail);
}
3
Daniel On

I am not sure if I understood you correctly but you can tell extbase to look in multiple pids for your records and state for each record where it should be stored:

plugin.tx_myext {
  persistence {
    storagePid = {$plugin.tx_myext.persistence.defaultStoragePid},{$plugin.tx_myext.persistence.detailStoragePid}
    classes {
      Vendor\MyExt\Domain\Model\Detail {
        newRecordStoragePid = {$plugin.tx_myext.persistence.detailStoragePid}
      }
    }
  }
}
3
Merec On

Models inherits from TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject which has getter and setter for $pid. If you set the field, all automation to set the field (i.e. newRecordStoragePid in typoscript) are not used.

With this, you can set all storage locations you want.

$myModel = $this->objectManager->create('Vendor\\Namespace\\Domain\\Model\\MyModel');
$myModel->setPid(4321);

Part from TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject:

/**
 * @var int The id of the page the record is "stored".
 */
protected $pid;

/**
 * Setter for the pid.
 *
 * @param int|NULL $pid
 * @return void
 */
public function setPid($pid)
{
    if ($pid === null) {
        $this->pid = null;
    } else {
        $this->pid = (int)$pid;
    }
}

/**
 * Getter for the pid.
 *
 * @return int The pid or NULL if none set yet.
 */
public function getPid()
{
    if ($this->pid === null) {
        return null;
    } else {
        return (int)$this->pid;
    }
}