在 laminas 框架上连接实体管理器(原则)的问题
Problem connecting entitymanager(doctrine) on laminas framework
请告诉我如何在 laminas 框架上获取实体管理器
控制器:
public function signupAction()
{
$user = new Users();
$user->setUsername('Test');
$user->setEmail('test@mail.ru');
$user->setNumber('+79168415532');
// this one i have persist() and flush()
}
创建工厂
class CategoryControllerFactory implements FactoryInterface
{
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, ?array $options = null)
{
$entityManger = $container->get(\Doctrine\ORM\EntityManager::class);
return new CategoryController($entityManger);
}
}
要详细说明@Aleksey Blossom 的回答,您需要创建一个工厂,从中注入实体管理器。
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Doctrine\ORM\EntityManager;
use YourModule\Controller\CategoryController;
class CategoryControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
$entityManger = $container->get(EntityManager::class);
return new CategoryController($entityManger);
}
}
并在您的控制器中
use Laminas\Mvc\Controller\AbstractActionController;
use Doctrine\ORM\EntityManager;
class CategoryController extends AbstractActionController
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function signupAction()
{
$user = new Users();
$user->setUsername('Test');
$user->setEmail('test@mail.ru');
$user->setNumber('+79168415532');
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
作为旁注,我还会考虑将您的业务逻辑放入模型中。
请告诉我如何在 laminas 框架上获取实体管理器
控制器:
public function signupAction()
{
$user = new Users();
$user->setUsername('Test');
$user->setEmail('test@mail.ru');
$user->setNumber('+79168415532');
// this one i have persist() and flush()
}
创建工厂
class CategoryControllerFactory implements FactoryInterface
{
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, ?array $options = null)
{
$entityManger = $container->get(\Doctrine\ORM\EntityManager::class);
return new CategoryController($entityManger);
}
}
要详细说明@Aleksey Blossom 的回答,您需要创建一个工厂,从中注入实体管理器。
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Doctrine\ORM\EntityManager;
use YourModule\Controller\CategoryController;
class CategoryControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
$entityManger = $container->get(EntityManager::class);
return new CategoryController($entityManger);
}
}
并在您的控制器中
use Laminas\Mvc\Controller\AbstractActionController;
use Doctrine\ORM\EntityManager;
class CategoryController extends AbstractActionController
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function signupAction()
{
$user = new Users();
$user->setUsername('Test');
$user->setEmail('test@mail.ru');
$user->setNumber('+79168415532');
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
作为旁注,我还会考虑将您的业务逻辑放入模型中。