src/Common/Traits/Model/Localised.php
public function create(array $aData = [], $bReturnObject = false, \Nails\Common\Factory\Locale $oLocale = null)
    {
        /** @var Database $oDb */
        $oDb = Factory::service('Database');

        if (empty($oLocale)) {
            throw new ModelException(
                self::class . ': A locale must be defined when creating a localised item'
            );
        }

        $aData['language'] = $oLocale->getLanguage();
        $aData['region']   = $oLocale->getRegion();
        unset($aData['locale']);

        if (empty($aData[$this->tableIdColumn])) {
            $oDb->set($this->tableIdColumn, null);
            $oDb->insert(parent::getTableName());
            $aData[$this->tableIdColumn] = $oDb->insert_id();
            if (empty($aData[$this->tableIdColumn])) {
                throw new ModelException(
                    'Failed to generate parent item for localised object'
                );
            }
            $bCreatedItem = true;
        }

        /**
         * This is to prevent primary key conflicts if a previously deleted item still
         * exists in the table
         */
        if (!$this->isDestructiveDelete()) {
            $this->destroy($aData[$this->tableIdColumn], $oLocale);
        }

        /**
         * Ensure automatic slug generation takes into account locale
         */
        if ($this->isAutoSetSlugs() && empty($aData[$this->tableSlugColumn])) {
            $aData[$this->tableSlugColumn] = $this->generateSlug(
                getFromArray($this->tableLabelColumn, $aData),
                null,
                $oLocale
            );
        }

        $iItemId = parent::create($aData, false);

        if (empty($iItemId)) {
            if (!empty($bCreatedItem)) {
                $oDb->where($this->tableIdColumn, $aData[$this->tableIdColumn]);
                $oDb->delete(parent::getTableName());
            }
            return null;
        } elseif (!$bReturnObject) {
            return $iItemId;
        }

        return $this->getById($iItemId, ['USE_LOCALE' => $oLocale]);
    }
public function create(array $aData = [], $bReturnObject = false, \Nails\Common\Factory\Locale $oLocale = null)
    {
        /** @var Database $oDb */
        $oDb = Factory::service('Database');

        if (empty($oLocale)) {
            throw new ModelException(
                self::class . ': A locale must be defined when creating a localised item'
            );
        }

        $aData['language'] = $oLocale->getLanguage();
        $aData['region']   = $oLocale->getRegion();
        unset($aData['locale']);

        if (empty($aData[$this->tableIdColumn])) {
            $oDb->set($this->tableIdColumn, null);
            $oDb->insert(parent::getTableName());
            $aData[$this->tableIdColumn] = $oDb->insert_id();
            if (empty($aData[$this->tableIdColumn])) {
                throw new ModelException(
                    'Failed to generate parent item for localised object'
                );
            }
            $bCreatedItem = true;
        }

        /**
         * This is to prevent primary key conflicts if a previously deleted item still
         * exists in the table
         */
        if (!$this->isDestructiveDelete()) {
            $this->destroy($aData[$this->tableIdColumn], $oLocale);
        }

        /**
         * Ensure automatic slug generation takes into account locale
         */
        if ($this->isAutoSetSlugs() && empty($aData[$this->tableSlugColumn])) {
            $aData[$this->tableSlugColumn] = $this->generateSlug(
                getFromArray($this->tableLabelColumn, $aData),
                null,
                $oLocale
            );
        }

        $iItemId = parent::create($aData, false);

        if (empty($iItemId)) {
            if (!empty($bCreatedItem)) {
                $oDb->where($this->tableIdColumn, $aData[$this->tableIdColumn]);
                $oDb->delete(parent::getTableName());
            }
            return null;
        } elseif (!$bReturnObject) {
            return $iItemId;
        }

        return $this->getById($iItemId, ['USE_LOCALE' => $oLocale]);
    }
public function create ( array $ aData = [ ] , $ bReturnObject = false , \ Nails \ Common \ Factory \ Locale $ oLocale = null ) { /** @var Database $oDb */ $ oDb = Factory :: service ( 'Database' ) ; if ( empty ( $ oLocale ) ) { throw new ModelException ( self :: class . ': A locale must be defined when creating a localised item' ) ; } $ aData [ 'language' ] = $ oLocale -> getLanguage ( ) ; $ aData [ 'region' ] = $ oLocale -> getRegion ( ) ; unset ( $ aData [ 'locale' ] ) ; if ( empty ( $ aData [ $ this -> tableIdColumn ] ) ) { $ oDb -> set ( $ this -> tableIdColumn , null ) ; $ oDb -> insert ( parent :: getTableName ( ) ) ; $ aData [ $ this -> tableIdColumn ] = $ oDb -> insert_id ( ) ; if ( empty ( $ aData [ $ this -> tableIdColumn ] ) ) { throw new ModelException ( 'Failed to generate parent item for localised object' ) ; } $ bCreatedItem = true ; } /**
         * This is to prevent primary key conflicts if a previously deleted item still
         * exists in the table
         */ if ( ! $ this -> isDestructiveDelete ( ) ) { $ this -> destroy ( $ aData [ $ this -> tableIdColumn ] , $ oLocale ) ; } /**
         * Ensure automatic slug generation takes into account locale
         */ if ( $ this -> isAutoSetSlugs ( ) && empty ( $ aData [ $ this -> tableSlugColumn ] ) ) { $ aData [ $ this -> tableSlugColumn ] = $ this -> generateSlug ( getFromArray ( $ this -> tableLabelColumn , $ aData ) , null , $ oLocale ) ; } $ iItemId = parent :: create ( $ aData , false ) ; if ( empty ( $ iItemId ) ) { if ( ! empty ( $ bCreatedItem ) ) { $ oDb -> where ( $ this -> tableIdColumn , $ aData [ $ this -> tableIdColumn ] ) ; $ oDb -> delete ( parent :: getTableName ( ) ) ; } return null ; } elseif ( ! $ bReturnObject ) { return $ iItemId ; } return $ this -> getById ( $ iItemId , [ 'USE_LOCALE' => $ oLocale ] ) ; }
Create a new localised item

@param array                             $aData         The data array
@param bool                              $bReturnObject Whether to return the item's ID or the object on success
@param \Nails\Common\Factory\Locale|null $oLocale       The locale to create the item in

@return null|int|Resource
@throws FactoryException
@throws ModelException
Create a new localised item