Symfony的。存储库方法。在查询参数(queryBuilder)中绑定用户

时间:2017-03-15 01:54:59

标签: symfony doctrine-orm

我有一个问题,我是symfony 3.2的新手 我想根据用户登录来查询对象。我听说我需要注入用户信息?但是如何?

存储库

import time

def menu():
    n = raw_input("What do you want the script to do: ").lower()
    if n == "run":
        r = run()
    elif n == "exit":
        exit()
    else:
        print("Unknown command")
    print("Result: " + r)    

def run():
    t = 0
    while t < 60:
        print("This script is doing some stuff")
        time.sleep(1)
        t = t + 1
    return "Some kind of result"

if __name__ == '__main__':
    print("Starting the most useless script ever")
    while True:
        menu()

SubAgentType

public function findAllActiveCategoryByUser(UserInterface $user)
{
    return $this->createQueryBuilder('sc')
        ->andWhere('sc.company_id = :company')
        ->setParameter('company_id', $this->getUser->getCompany->getCompanyId)
        ->orderBy('sc.createdAt', 'ASC');
}

1 个答案:

答案 0 :(得分:-1)

试试这个:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $user = $this->securityContext->getToken()->getUser();

    $builder
        ->add('company', EntityType::class, [
            'placeholder' => 'Choose Company',
            'class' => Company::class,
            'query_builder' => function (CompanyRepository $er) {
                return $er->findAllActiveCompany($user);
            },
            'constraints' => array(new NotBlank(array('message' => 'Company is required.')))
        ])
}

<强>存储库:

public function findAllActiveCategoryByUser(UserInterface $user)
{
    $qb = $this->createQueryBuilder('sc');
    $result = $qb->select('sc')
        ->where('sc.company_id = :company')
        ->setParameter('company', $this->getUser->getCompany->getCompanyId)
        ->orderBy('sc.createdAt', 'ASC')
        ->getQuery();
    return $result->getResult();

}