4

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}
  }
}
webman
  • 1,117
  • 15
  • 41

3 Answers3

3

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}
      }
    }
  }
}
Daniel
  • 6,916
  • 2
  • 36
  • 47
  • It looks just right, any other pointers how to debug? I can't get it to work ... tx_myext was named originally tx_myext_objects by the builder but changing that did not affect the other settings (and I tried to set it back without result for the newRecordStoragePid), the snippet you give is setup.ts is it not ? – webman Dec 22 '16 at 04:51
  • if I'd use config. or module. instead of plugin. ? still trying to get it to work – webman Dec 26 '16 at 15:53
  • what about saving in the backend? – webman Dec 29 '16 at 03:28
2

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;
    }
}
Merec
  • 2,751
  • 1
  • 14
  • 21
  • where would I set it ? the details table which should store in a separate pid is NOT aggregate root, standard from the extension builder I have only a model for this table ... (sorry I'm not that handy with php classes and typo3 yet) – webman Dec 23 '16 at 12:08
  • the "aggregate root" option in the extension builder just specifies, if a controller and repository will be generated upon save. You can manually create a respository class for each model. Copy and paste it from the other aggregate root (if there is one set) or check out the extbase documentation. – Merec Dec 23 '16 at 15:28
  • `$myModel = $this->objectManager->create('Vendor\\Namespace\\Domain\\Model\\MyModel'); $myModel->setPid(4321);` should be added to DetailsRepository.php ?, maybe very stupid question but I do not recognize this syntax – webman Dec 23 '16 at 16:48
0

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);
}
René Pflamm
  • 3,273
  • 17
  • 29
  • In which class would I put this snippet ? Details is not aggregate root, I've only a model Details.php ... – webman Dec 23 '16 at 05:18
  • In an controller where you create your details. – René Pflamm Dec 23 '16 at 05:40
  • thank you 4 the attention, if you build with extension builder there is no controller nor a repository for the details so there is no call for create, I did create them but I get no result ... – webman Dec 23 '16 at 10:06
  • it looks as if it should be in the controller, does that not only affect the front-end action ? – webman Dec 26 '16 at 16:03
  • That's right. For the backend I don't have any ideas how to solve it. – René Pflamm Dec 26 '16 at 16:19
  • thank you, I think that one of the brains will help me to find it, then it will be available here ... – webman Dec 26 '16 at 16:31
  • I think you should highlight this information in the main thread. No body will think that you want this for backend. – René Pflamm Dec 26 '16 at 16:40