Symfony - 从命令文件中检索当前用户
Symfony - Retrieve current user from in a command file
我目前正在处理一个非常古老的 Symfony 2.4 项目。
我创建了一个 CsvImportCommand 文件来创建具有 csv 功能的大量客户。
我需要将当前登录用户设置为我的列的值 'created_by',但我不知道如何获取此信息。
我发现我可以在控制器文件中使用 app.user
或 get('security.context')->getToken()->getUser()
在 twig 模板中获取此信息。
但我真的不知道如何在命令文件中检索此信息。
记住,这是一个 Symfony 2.4 项目。
下面是我的代码,它 returns 一个错误:PHP 解析错误:语法错误,意外的 '$this' (T_VARIABLE)(在我的第二行)
class CsvImportCommand extends ContainerAwareCommand
{
private $user = $this->get('security.context')->getToken()->getUser();
private $username = $user->getUsername();
protected function configure()
{
$this
->setName('csv:import')
->setDescription('Import users from CSV file')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$now = new \DateTime();
$output->writeln('<comment>Start:' . $now->format('d-m-Y G:i:s') . '---</comment>');
$this->import($input, $output);
$now = new \DateTime();
$output->writeln('<comment>End:' . $now->format('d-m-Y G:i:s') . '---</comment>');
}
protected function import(InputInterface $input, Outputinterface $output)
{
$data = $this->get($input, $output);
$em = $this->getContainer()->get('doctrine')->getManager();
$em->getConnection()->getConfiguration()->setSQLLogger(null);
$size = count($data);
$batchSize = 20;
$i = 1;
foreach($data as $row) {
$person = $em->getRepository('MyBundle:Person')->findOneBy(array('firstname'=>$row['firstname'], 'lastname'=>$row['lastname']));
if(!$person){
$person = new Person();
$em->persist($person);
}
$person->setIsPrivate($row['is_private']);
$person->setCivility($row['civility']);
$person->setFirstName($row['firstname']);
$person->setLastName($row['lastname']);
$person->setPhoneHome($row['phone_home']);
$person->setMobileHome($row['mobile_home']);
$person->setEmailPro($row['email_pro']);
$person->setEmailHome($row['email_home']);
$person->setCreatedAt(new \DateTime());
$person->setUpdatedAt(new \DateTime());
$person->setCreatedBy($username);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
$now = new \DateTime();
$output->writeln('of users imported... | ' . $now->format('d-m-Y G:i:s'));
}
$i++;
}
$em->flush();
$em->clear();
}
}
关于您的错误:
您不能在 class 变量中使用 $this。你必须在 __construct 中分配 $user 或者你创建 set-Methods。类似于:setUser.
关于带命令的安全包:
你不能使用 security.context 因为你在这个部分没有用户。
也许你可以伪造它,但这不太好。
您可能需要考虑向您的系统添加一个“用户”来表示此类自动化任务。然后您可以检索自动用户并将其用作您的 createdBy 用户。
我目前正在处理一个非常古老的 Symfony 2.4 项目。
我创建了一个 CsvImportCommand 文件来创建具有 csv 功能的大量客户。
我需要将当前登录用户设置为我的列的值 'created_by',但我不知道如何获取此信息。
我发现我可以在控制器文件中使用 app.user
或 get('security.context')->getToken()->getUser()
在 twig 模板中获取此信息。
但我真的不知道如何在命令文件中检索此信息。
记住,这是一个 Symfony 2.4 项目。
下面是我的代码,它 returns 一个错误:PHP 解析错误:语法错误,意外的 '$this' (T_VARIABLE)(在我的第二行)
class CsvImportCommand extends ContainerAwareCommand
{
private $user = $this->get('security.context')->getToken()->getUser();
private $username = $user->getUsername();
protected function configure()
{
$this
->setName('csv:import')
->setDescription('Import users from CSV file')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$now = new \DateTime();
$output->writeln('<comment>Start:' . $now->format('d-m-Y G:i:s') . '---</comment>');
$this->import($input, $output);
$now = new \DateTime();
$output->writeln('<comment>End:' . $now->format('d-m-Y G:i:s') . '---</comment>');
}
protected function import(InputInterface $input, Outputinterface $output)
{
$data = $this->get($input, $output);
$em = $this->getContainer()->get('doctrine')->getManager();
$em->getConnection()->getConfiguration()->setSQLLogger(null);
$size = count($data);
$batchSize = 20;
$i = 1;
foreach($data as $row) {
$person = $em->getRepository('MyBundle:Person')->findOneBy(array('firstname'=>$row['firstname'], 'lastname'=>$row['lastname']));
if(!$person){
$person = new Person();
$em->persist($person);
}
$person->setIsPrivate($row['is_private']);
$person->setCivility($row['civility']);
$person->setFirstName($row['firstname']);
$person->setLastName($row['lastname']);
$person->setPhoneHome($row['phone_home']);
$person->setMobileHome($row['mobile_home']);
$person->setEmailPro($row['email_pro']);
$person->setEmailHome($row['email_home']);
$person->setCreatedAt(new \DateTime());
$person->setUpdatedAt(new \DateTime());
$person->setCreatedBy($username);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
$now = new \DateTime();
$output->writeln('of users imported... | ' . $now->format('d-m-Y G:i:s'));
}
$i++;
}
$em->flush();
$em->clear();
}
}
关于您的错误:
您不能在 class 变量中使用 $this。你必须在 __construct 中分配 $user 或者你创建 set-Methods。类似于:setUser.
关于带命令的安全包:
你不能使用 security.context 因为你在这个部分没有用户。 也许你可以伪造它,但这不太好。
您可能需要考虑向您的系统添加一个“用户”来表示此类自动化任务。然后您可以检索自动用户并将其用作您的 createdBy 用户。