src/ORM/DataObject.php
public function duplicate($doWrite = true, $relations = null)
    {
        // Handle legacy behaviour
        if (is_string($relations) || $relations === true) {
            if ($relations === true) {
                $relations = 'many_many';
            }
            Deprecation::notice('5.0', 'Use cascade_duplicates config instead of providing a string to duplicate()');
            $relations = array_keys($this->config()->get($relations)) ?: [];
        }

        // Get duplicates
        if ($relations === null) {
            $relations = $this->config()->get('cascade_duplicates');
            // Remove any duplicate entries before duplicating them
            if (is_array($relations)) {
                $relations = array_unique($relations);
            }
        }

        // Create unsaved raw duplicate
        $map = $this->toMap();
        unset($map['Created']);
        /** @var static $clone */
        $clone = Injector::inst()->create(static::class, $map, false, $this->getSourceQueryParams());
        $clone->ID = 0;

        // Note: Extensions such as versioned may update $relations here
        $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite, $relations);
        if ($relations) {
            $this->duplicateRelations($this, $clone, $relations);
        }
        if ($doWrite) {
            $clone->write();
        }
        $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite, $relations);

        return $clone;
    }
public function duplicate($doWrite = true, $relations = null)
    {
        // Handle legacy behaviour
        if (is_string($relations) || $relations === true) {
            if ($relations === true) {
                $relations = 'many_many';
            }
            Deprecation::notice('5.0', 'Use cascade_duplicates config instead of providing a string to duplicate()');
            $relations = array_keys($this->config()->get($relations)) ?: [];
        }

        // Get duplicates
        if ($relations === null) {
            $relations = $this->config()->get('cascade_duplicates');
            // Remove any duplicate entries before duplicating them
            if (is_array($relations)) {
                $relations = array_unique($relations);
            }
        }

        // Create unsaved raw duplicate
        $map = $this->toMap();
        unset($map['Created']);
        /** @var static $clone */
        $clone = Injector::inst()->create(static::class, $map, false, $this->getSourceQueryParams());
        $clone->ID = 0;

        // Note: Extensions such as versioned may update $relations here
        $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite, $relations);
        if ($relations) {
            $this->duplicateRelations($this, $clone, $relations);
        }
        if ($doWrite) {
            $clone->write();
        }
        $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite, $relations);

        return $clone;
    }
public function duplicate ( $ doWrite = true , $ relations = null ) { // Handle legacy behaviour if ( is_string ( $ relations ) || $ relations === true ) { if ( $ relations === true ) { $ relations = 'many_many' ; } Deprecation :: notice ( '5.0' , 'Use cascade_duplicates config instead of providing a string to duplicate()' ) ; $ relations = array_keys ( $ this -> config ( ) -> get ( $ relations ) ) ? : [ ] ; } // Get duplicates if ( $ relations === null ) { $ relations = $ this -> config ( ) -> get ( 'cascade_duplicates' ) ; // Remove any duplicate entries before duplicating them if ( is_array ( $ relations ) ) { $ relations = array_unique ( $ relations ) ; } } // Create unsaved raw duplicate $ map = $ this -> toMap ( ) ; unset ( $ map [ 'Created' ] ) ; /** @var static $clone */ $ clone = Injector :: inst ( ) -> create ( static :: class , $ map , false , $ this -> getSourceQueryParams ( ) ) ; $ clone -> ID = 0 ; // Note: Extensions such as versioned may update $relations here $ clone -> invokeWithExtensions ( 'onBeforeDuplicate' , $ this , $ doWrite , $ relations ) ; if ( $ relations ) { $ this -> duplicateRelations ( $ this , $ clone , $ relations ) ; } if ( $ doWrite ) { $ clone -> write ( ) ; } $ clone -> invokeWithExtensions ( 'onAfterDuplicate' , $ this , $ doWrite , $ relations ) ; return $ clone ; }
Create a duplicate of this node. Can duplicate many_many relations

@param bool $doWrite Perform a write() operation before returning the object.
If this is true, it will create the duplicate in the database.
@param array|null|false $relations List of relations to duplicate.
Will default to `cascade_duplicates` if null.
Set to 'false' to force none.
Set to specific array of names to duplicate to override these.
Note: If using versioned, this will additionally failover to `owns` config.
@return static A duplicate of this node. The exact type will be the type of this node.
Create a duplicate of this node . Can duplicate many_many relations