TYPO3: Copy an Object of the extbase/fluid extension

If you want to copy an object in the Extbase extension of typo3, you can use the following code to achieve the same.

 

public function copyAction(\VENDOR\Ext\Domain\Model\Object $object)

{

 

    // create new object

    $newObject = $this->objectManager->get("VENDOR\\Ext\\Domain\\Model\\Object") ;

    $properties = ObjectAccess::getGettableProperties($object);

 

    foreach ($properties as $propertyName => $propertyValue) {

        ObjectAccess::setProperty($newObject, $propertyName, $propertyValue);

    }

 

    $newObject->setPage($newPages);

 

    // add the object

    $this->objectRepository->add($newObject);

}