src/UthandoCommon/Service/AbstractMapperService.php
public function save($data)
    {
        $argv = compact('data');
        $argv = $this->prepareEventArguments($argv);

        $this->getEventManager()->trigger(self::EVENT_PRE_SAVE, $this, $argv);

        $data = $argv['data'];

        if ($data instanceof ModelInterface) {
            $data = $this->getHydrator()->extract($data);
        }

        $pk = $this->getMapper()->getPrimaryKey();
        $id = $data[$pk];
        unset($data[$pk]);

        if (0 === $id || null === $id || '' === $id) {
            $result = $this->getMapper()->insert($data);
            $this->clearCacheTags();
        } else {
            if ($this->getById($id)) {
                $result = $this->getMapper()->update($data, [$pk => $id]);
            } else {
                throw new ServiceException('ID ' . $id . ' does not exist');
            }

            $this->removeCacheItem($id);
        }

        $argv = compact('data', 'result');
        $argv = $this->prepareEventArguments($argv);

        $this->getEventManager()->trigger(self::EVENT_POST_SAVE, $this, $argv);

        return $result;
    }
public function save($data)
    {
        $argv = compact('data');
        $argv = $this->prepareEventArguments($argv);

        $this->getEventManager()->trigger(self::EVENT_PRE_SAVE, $this, $argv);

        $data = $argv['data'];

        if ($data instanceof ModelInterface) {
            $data = $this->getHydrator()->extract($data);
        }

        $pk = $this->getMapper()->getPrimaryKey();
        $id = $data[$pk];
        unset($data[$pk]);

        if (0 === $id || null === $id || '' === $id) {
            $result = $this->getMapper()->insert($data);
            $this->clearCacheTags();
        } else {
            if ($this->getById($id)) {
                $result = $this->getMapper()->update($data, [$pk => $id]);
            } else {
                throw new ServiceException('ID ' . $id . ' does not exist');
            }

            $this->removeCacheItem($id);
        }

        $argv = compact('data', 'result');
        $argv = $this->prepareEventArguments($argv);

        $this->getEventManager()->trigger(self::EVENT_POST_SAVE, $this, $argv);

        return $result;
    }
public function save ( $ data ) { $ argv = compact ( 'data' ) ; $ argv = $ this -> prepareEventArguments ( $ argv ) ; $ this -> getEventManager ( ) -> trigger ( self :: EVENT_PRE_SAVE , $ this , $ argv ) ; $ data = $ argv [ 'data' ] ; if ( $ data instanceof ModelInterface ) { $ data = $ this -> getHydrator ( ) -> extract ( $ data ) ; } $ pk = $ this -> getMapper ( ) -> getPrimaryKey ( ) ; $ id = $ data [ $ pk ] ; unset ( $ data [ $ pk ] ) ; if ( 0 === $ id || null === $ id || '' === $ id ) { $ result = $ this -> getMapper ( ) -> insert ( $ data ) ; $ this -> clearCacheTags ( ) ; } else { if ( $ this -> getById ( $ id ) ) { $ result = $ this -> getMapper ( ) -> update ( $ data , [ $ pk => $ id ] ) ; } else { throw new ServiceException ( 'ID ' . $ id . ' does not exist' ) ; } $ this -> removeCacheItem ( $ id ) ; } $ argv = compact ( 'data' , 'result' ) ; $ argv = $ this -> prepareEventArguments ( $ argv ) ; $ this -> getEventManager ( ) -> trigger ( self :: EVENT_POST_SAVE , $ this , $ argv ) ; return $ result ; }
updates a row if id is supplied else insert a new row

@param array|ModelInterface $data
@throws ServiceException
@return int $results number of rows affected or insertId
updates a row if id is supplied else insert a new row