Php Symfony-从命令文件中检索当前用户

Php Symfony-从命令文件中检索当前用户,php,symfony,Php,Symfony,我目前正在从事一个非常古老的Symfony 2.4项目 我创建了一个CsvImportCommand文件,以创建具有csv功能的大量客户 我需要将当前登录用户设置为我的列“created_by”的值,但我不知道如何获取此信息 我发现我可以通过app.user或控制器文件中的get('security.context')->getToken()->getUser()在细枝模板中获取此信息 但我真的不知道如何在命令文件中检索这些信息 记住,这是一个Symfony 2.4项目 下面是我的代码,它返回一

我目前正在从事一个非常古老的Symfony 2.4项目

我创建了一个CsvImportCommand文件,以创建具有csv功能的大量客户

我需要将当前登录用户设置为我的列“created_by”的值,但我不知道如何获取此信息

我发现我可以通过
app.user
或控制器文件中的
get('security.context')->getToken()->getUser()
在细枝模板中获取此信息

但我真的不知道如何在命令文件中检索这些信息

记住,这是一个Symfony 2.4项目

下面是我的代码,它返回一个错误:PHP解析错误:语法错误,意外的“$this”(T_变量)(在我的第二行)

class CsvImportCommand扩展了ContainerWareCommand
{
private$user=$this->get('security.context')->getToken()->getUser();
私有$username=$user->getUsername();
受保护的函数配置()
{
美元这个
->setName('csv:import')
->setDescription('从CSV文件导入用户')
;
}
受保护的函数执行(InputInterface$input,OutputInterface$output)
{
$now=new\DateTime();
$output->writeln('Start:'。$now->format('d-m-Y G:i:s')。--';
$this->import($input,$output);
$now=new\DateTime();
$output->writeln('End:'。$now->format('d-m-Y G:i:s')。-->);
}
受保护的函数导入(InputInterface$input,Outputinterface$output)
{
$data=$this->get($input,$output);
$em=$this->getContainer()->get('doctrine')->getManager();
$em->getConnection()->getConfiguration()->setSQLLogger(null);
$size=计数($data);
$batchSize=20;
$i=1;
foreach($行数据){
$person=$em->getRepository('MyBundle:person')->findOneBy(数组('firstname'=>$row['firstname'],'lastname'=>$row['lastname']);
如果(!$人){
$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);
如果($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();
}
}

关于您的错误:

不能在类变量中使用$this。必须在_构造或创建集方法中分配$user。类似于:setUser

关于带有命令的安全包:

您无法使用security.context,因为您在此部分中没有用户。
也许你可以伪造它,但这不太好。

你可能想考虑在系统中添加一个“用户”来表示自动任务,例如。然后您可以检索自动用户并将其用作您的createdBy用户。

对于安全包部分,这意味着我永远无法检索当前登录的用户?有什么建议吗?因为我需要这些信息来设置“created_by”列。可能通过调用外部命令?这取决于从何处启动此命令。但是,命令通常是通过cron启动的。你怎么知道的?哦,如果你通过cron启动它,你只知道一个用户。您可以静态添加它。回答以下问题-谁在命令行模式下登录用户?我想您可以添加一个--username参数,如果您觉得非常有野心,甚至可以添加一个--password参数。您有语法错误(初始化属性时不能使用
$this->get()
)和概念错误(从逻辑上讲,执行命令行应用程序时没有用户登录)。
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();
    }
}