Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TYPO3 extbase: how to use ObjectStorage?

Tags:

typo3

extbase

I'm trying to use a m:n relation, the same way as FrontEndUser is related to FrontEndUserGroup, e.g. without intermediate mm table. In my controller, I build my object, then I call $barRepository->update($barObject); to update the values of my object. However, it fails on the update function with the error:

Fatal error: Call to undefined method Cbrunet\Up\Domain\Model\Foo::getPosition() in /home/cbrunet/websites/typo3_src-6.1.1/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php on line 486

where Foo is the type of the object contained in the ObjectStorage of Bar. My understanding is that getPosition should be called on the ObjectStorage, not on the object contained into that ObjectStorage. However, I cannot figure out why this is not working in my case.

This is in TYPO3 6.1.5. Any hint would be appreciated.


The model of Bar which has a m:n relation to Foo looks like:

namespace Cbrunet\Up\Domain\Model;

class Bar extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Cbrunet\Up\Domain\Model\Foo>
 */
protected $myprop;

public function __construct() {
    $this->myprop = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}

/**
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $myprop
 * @return void
 */
public function setMyprop(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $myprop) {
    $this->myprop = $myprop;
}

/**
 * @param \Cbrunet\Up\Domain\Model\Foo $myprop
 * @return void
 */
public function addMyprop(\Cbrunet\Up\Domain\Model\Foo $myprop) {
    $this->myprop->attach($myprop);
}

/**
 * @param \Cbrunet\Up\Domain\Model\Foo $myprop
 * @return void
 */
public function removeMyprop(\Cbrunet\Up\Domain\Model\Foo $myprop) {
    $this->myprop->detach($myprop);
}

/**
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
 */
public function getMyprop() {
    return $this->myprop;
}
}

The relevant code in my controller looks like:

/**
 * action update
 *
 * @return void
 */
public function updateAction() {
    $args = $this->request->getArgument('myargs');
    foreach ($args as $k=>$val) {
        $pp = $this->barRepository->findOneByAprop($k); // another prop of Bar, not illustrated in the code above.
        $listepour = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        foreach ($val as $p) {
            $ap = $this->fooRepository->findOneByUid(intval($p));
            $listepour->attach($ap);
        }
        $pp->setMyprop($listepour);
        $this->barRepository->update($pp); // error occurs here
    }
    $this->redirect('list');
}
like image 446
Charles Brunet Avatar asked Oct 15 '25 18:10

Charles Brunet


1 Answers

  • Do you also have configured your TCA?
  • do you have an initStorageObjects-function in your domain model?

Also you can try to build these case with the extension-manager and compare the code.

like image 142
freshp Avatar answered Oct 19 '25 04:10

freshp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!