TYPO3: Set more than one storage pid for one extension

半世苍凉 提交于 2019-12-12 12:12:36

问题


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

回答1:


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



回答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;
    }
}



回答3:


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


来源:https://stackoverflow.com/questions/41266342/typo3-set-more-than-one-storage-pid-for-one-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!