src/Kunstmaan/MenuBundle/Service/MenuService.php
public function makeSureMenusExist()
    {
        $locales = array_unique($this->getLocales());
        $required = array();

        foreach ($this->menuNames as $name) {
            $required[$name] = $locales;
        }

        $menuObjects = $this->em->getRepository($this->menuEntityClass)->findAll();

        foreach ($menuObjects as $menu) {
            if (array_key_exists($menu->getName(), $required)) {
                $index = array_search($menu->getLocale(), $required[$menu->getName()]);
                if ($index !== false) {
                    unset($required[$menu->getName()][$index]);
                }
            }
        }

        foreach ($required as $name => $locales) {
            foreach ($locales as $locale) {
                $className = $this->menuEntityClass;
                $menu = new $className();
                $menu->setName($name);
                $menu->setLocale($locale);
                $this->em->persist($menu);
            }
        }

        $this->em->flush();
    }
public function makeSureMenusExist()
    {
        $locales = array_unique($this->getLocales());
        $required = array();

        foreach ($this->menuNames as $name) {
            $required[$name] = $locales;
        }

        $menuObjects = $this->em->getRepository($this->menuEntityClass)->findAll();

        foreach ($menuObjects as $menu) {
            if (array_key_exists($menu->getName(), $required)) {
                $index = array_search($menu->getLocale(), $required[$menu->getName()]);
                if ($index !== false) {
                    unset($required[$menu->getName()][$index]);
                }
            }
        }

        foreach ($required as $name => $locales) {
            foreach ($locales as $locale) {
                $className = $this->menuEntityClass;
                $menu = new $className();
                $menu->setName($name);
                $menu->setLocale($locale);
                $this->em->persist($menu);
            }
        }

        $this->em->flush();
    }
public function makeSureMenusExist ( ) { $ locales = array_unique ( $ this -> getLocales ( ) ) ; $ required = array ( ) ; foreach ( $ this -> menuNames as $ name ) { $ required [ $ name ] = $ locales ; } $ menuObjects = $ this -> em -> getRepository ( $ this -> menuEntityClass ) -> findAll ( ) ; foreach ( $ menuObjects as $ menu ) { if ( array_key_exists ( $ menu -> getName ( ) , $ required ) ) { $ index = array_search ( $ menu -> getLocale ( ) , $ required [ $ menu -> getName ( ) ] ) ; if ( $ index !== false ) { unset ( $ required [ $ menu -> getName ( ) ] [ $ index ] ) ; } } } foreach ( $ required as $ name => $ locales ) { foreach ( $ locales as $ locale ) { $ className = $ this -> menuEntityClass ; $ menu = new $ className ( ) ; $ menu -> setName ( $ name ) ; $ menu -> setLocale ( $ locale ) ; $ this -> em -> persist ( $ menu ) ; } } $ this -> em -> flush ( ) ; }
Make sure the menu objects exist in the database for each locale.
Make sure the menu objects exist in the database for each locale .