protected function _insert($entity, $data)
    {
        $primary = (array)$this->getPrimaryKey();
        if (empty($primary)) {
            $msg = sprintf(
                'Cannot insert row in "%s" table, it has no primary key.',
                $this->getTable()
            );
            throw new RuntimeException($msg);
        }
        $keys = array_fill(0, count($primary), null);
        $id = (array)$this->_newId($primary) + $keys;

        // Generate primary keys preferring values in $data.
        $primary = array_combine($primary, $id);
        $primary = array_intersect_key($data, $primary) + $primary;

        $filteredKeys = array_filter($primary, function ($v) {
            return $v !== null;
        });
        $data += $filteredKeys;

        if (count($primary) > 1) {
            $schema = $this->getSchema();
            foreach ($primary as $k => $v) {
                if (!isset($data[$k]) && empty($schema->getColumn($k)['autoIncrement'])) {
                    $msg = 'Cannot insert row, some of the primary key values are missing. ';
                    $msg .= sprintf(
                        'Got (%s), expecting (%s)',
                        implode(', ', $filteredKeys + $entity->extract(array_keys($primary))),
                        implode(', ', array_keys($primary))
                    );
                    throw new RuntimeException($msg);
                }
            }
        }

        $success = false;
        if (empty($data)) {
            return $success;
        }

        $statement = $this->query()->insert(array_keys($data))
            ->values($data)
            ->execute();

        if ($statement->rowCount() !== 0) {
            $success = $entity;
            $entity->set($filteredKeys, ['guard' => false]);
            $schema = $this->getSchema();
            $driver = $this->getConnection()->getDriver();
            foreach ($primary as $key => $v) {
                if (!isset($data[$key])) {
                    $id = $statement->lastInsertId($this->getTable(), $key);
                    $type = $schema->getColumnType($key);
                    $entity->set($key, Type::build($type)->toPHP($id, $driver));
                    break;
                }
            }
        }
        $statement->closeCursor();

        return $success;
    }
protected function _insert($entity, $data)
    {
        $primary = (array)$this->getPrimaryKey();
        if (empty($primary)) {
            $msg = sprintf(
                'Cannot insert row in "%s" table, it has no primary key.',
                $this->getTable()
            );
            throw new RuntimeException($msg);
        }
        $keys = array_fill(0, count($primary), null);
        $id = (array)$this->_newId($primary) + $keys;

        // Generate primary keys preferring values in $data.
        $primary = array_combine($primary, $id);
        $primary = array_intersect_key($data, $primary) + $primary;

        $filteredKeys = array_filter($primary, function ($v) {
            return $v !== null;
        });
        $data += $filteredKeys;

        if (count($primary) > 1) {
            $schema = $this->getSchema();
            foreach ($primary as $k => $v) {
                if (!isset($data[$k]) && empty($schema->getColumn($k)['autoIncrement'])) {
                    $msg = 'Cannot insert row, some of the primary key values are missing. ';
                    $msg .= sprintf(
                        'Got (%s), expecting (%s)',
                        implode(', ', $filteredKeys + $entity->extract(array_keys($primary))),
                        implode(', ', array_keys($primary))
                    );
                    throw new RuntimeException($msg);
                }
            }
        }

        $success = false;
        if (empty($data)) {
            return $success;
        }

        $statement = $this->query()->insert(array_keys($data))
            ->values($data)
            ->execute();

        if ($statement->rowCount() !== 0) {
            $success = $entity;
            $entity->set($filteredKeys, ['guard' => false]);
            $schema = $this->getSchema();
            $driver = $this->getConnection()->getDriver();
            foreach ($primary as $key => $v) {
                if (!isset($data[$key])) {
                    $id = $statement->lastInsertId($this->getTable(), $key);
                    $type = $schema->getColumnType($key);
                    $entity->set($key, Type::build($type)->toPHP($id, $driver));
                    break;
                }
            }
        }
        $statement->closeCursor();

        return $success;
    }
protected function _insert ( $ entity , $ data ) { $ primary = ( array ) $ this -> getPrimaryKey ( ) ; if ( empty ( $ primary ) ) { $ msg = sprintf ( 'Cannot insert row in "%s" table, it has no primary key.' , $ this -> getTable ( ) ) ; throw new RuntimeException ( $ msg ) ; } $ keys = array_fill ( 0 , count ( $ primary ) , null ) ; $ id = ( array ) $ this -> _newId ( $ primary ) + $ keys ; // Generate primary keys preferring values in $data. $ primary = array_combine ( $ primary , $ id ) ; $ primary = array_intersect_key ( $ data , $ primary ) + $ primary ; $ filteredKeys = array_filter ( $ primary , function ( $ v ) { return $ v !== null ; } ) ; $ data += $ filteredKeys ; if ( count ( $ primary ) > 1 ) { $ schema = $ this -> getSchema ( ) ; foreach ( $ primary as $ k => $ v ) { if ( ! isset ( $ data [ $ k ] ) && empty ( $ schema -> getColumn ( $ k ) [ 'autoIncrement' ] ) ) { $ msg = 'Cannot insert row, some of the primary key values are missing. ' ; $ msg .= sprintf ( 'Got (%s), expecting (%s)' , implode ( ', ' , $ filteredKeys + $ entity -> extract ( array_keys ( $ primary ) ) ) , implode ( ', ' , array_keys ( $ primary ) ) ) ; throw new RuntimeException ( $ msg ) ; } } } $ success = false ; if ( empty ( $ data ) ) { return $ success ; } $ statement = $ this -> query ( ) -> insert ( array_keys ( $ data ) ) -> values ( $ data ) -> execute ( ) ; if ( $ statement -> rowCount ( ) !== 0 ) { $ success = $ entity ; $ entity -> set ( $ filteredKeys , [ 'guard' => false ] ) ; $ schema = $ this -> getSchema ( ) ; $ driver = $ this -> getConnection ( ) -> getDriver ( ) ; foreach ( $ primary as $ key => $ v ) { if ( ! isset ( $ data [ $ key ] ) ) { $ id = $ statement -> lastInsertId ( $ this -> getTable ( ) , $ key ) ; $ type = $ schema -> getColumnType ( $ key ) ; $ entity -> set ( $ key , Type :: build ( $ type ) -> toPHP ( $ id , $ driver ) ) ; break ; } } } $ statement -> closeCursor ( ) ; return $ success ; }
Auxiliary function to handle the insert of an entity's data in the table

@param \Cake\Datasource\EntityInterface $entity the subject entity from were $data was extracted
@param array $data The actual data that needs to be saved
@return \Cake\Datasource\EntityInterface|bool
@throws \RuntimeException if not all the primary keys where supplied or could
be generated when the table has composite primary keys. Or when the table has no primary key.
Auxiliary function to handle the insert of an entity s data in the table