php - ZF2 - fetch data using Doctrine 2 in static function -
i'm trying figure out how fetch data database in static function. class looks this:
namespace core class culture { private static $allowedlanguages = array(); public static function getallowedlanguages() { if(empty(self::$allowedlanguages)){ self::$allowedlanguages = $x // should data fetched database } return $langs; } }
in code want able call \core\culture::getallowedlanguages();
problem have how access doctrine 2 repository within static class?
is there elegant way doctrine entitymanager or servicelocator inside function?
first need this:
// use use doctrine\common\annotations\annotationreader; use doctrine\common\annotations\annotationregistry; use doctrine\orm\tools\setup; use doctrine\orm\entitymanager; use doctrine\orm\mapping\driver\annotationdriver; /** * entitymanager */ public static function getentitymanager($module = 'pathinsrcforentity') { $paths = [dirname(__dir__)."/src/$module/entity"]; $isdevmode = true; // test db connection configuration $connectionparams = [ 'driver' => 'pdo_mysql', 'user' => 'root', 'password' => 'root', 'dbname' => 'db_name', ]; $config = setup::createconfiguration($isdevmode); $driver = new annotationdriver(new annotationreader(), $paths); annotationregistry::registerloader('class_exists'); $config->setmetadatadriverimpl($driver); $entitymanager = entitymanager::create($connectionparams, $config); return $entitymanager; }
after call repository
// use use doctrine\orm\mapping\classmetadata; $repository = new repositorynamerepository(\core \common::getentitymanager(), new classmetadata('\path\entity\classname'));
i found solution here: https://samsonasik.wordpress.com/2015/03/24/using-doctrine-data-fixture-for-testing-querybuilder-inside-repository/
Comments
Post a Comment