Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony表单中的自定义查询_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php Symfony表单中的自定义查询

Php Symfony表单中的自定义查询,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,createQueryBuilder查询工作良好,当我尝试使用EntityManager编写自定义查询时,它会显示警告。EntityManager不可用 控制器: $form = $this->createForm(new ContractsType($user, $isAdmin, $securityContext), $contract, array('em' => $em, 'referring_student' => $contract->getReferring

createQueryBuilder查询工作良好,当我尝试使用EntityManager编写自定义查询时,它会显示警告。EntityManager不可用

控制器:

$form = $this->createForm(new ContractsType($user, $isAdmin, $securityContext), $contract, array('em' => $em, 'referring_student' => $contract->getReferringStudent()));
class ContractsType extends AbstractType {

$queryBuilder = function($repo) use ($user) {

return $repo->createQueryBuilder('p')
    ->leftJoin('p.employees', 'e')
    ->where('e.id = :employee_id')
    ->setParameter('employee_id', $user->getId())
    ->orderBy('p.name', 'ASC');
}
  $builder->add('store', 'entity', array(
        'class' => 'C2EducateToolsBundle:Stores',
        'label' => 'Center',
        'query_builder' => $queryBuilder,
        'property' => 'name',
        'empty_value' => 'Select')
    );
    }
合同类型

$form = $this->createForm(new ContractsType($user, $isAdmin, $securityContext), $contract, array('em' => $em, 'referring_student' => $contract->getReferringStudent()));
class ContractsType extends AbstractType {

$queryBuilder = function($repo) use ($user) {

return $repo->createQueryBuilder('p')
    ->leftJoin('p.employees', 'e')
    ->where('e.id = :employee_id')
    ->setParameter('employee_id', $user->getId())
    ->orderBy('p.name', 'ASC');
}
  $builder->add('store', 'entity', array(
        'class' => 'C2EducateToolsBundle:Stores',
        'label' => 'Center',
        'query_builder' => $queryBuilder,
        'property' => 'name',
        'empty_value' => 'Select')
    );
    }
createQueryBuilder工作正常,但我想使用EntityManager编写自定义查询,如何使EntityEquiry在这里可用。这样我就可以运行下面的查询了

$storesQuery = "select store_id as id,ts.name as name from tbl_employees e LEFT JOIN tbl_stores ts ON e.store_id=ts.id where e.id=:employeesId  AND ts.center_type_id!=:centerTypeId AND ts.select_option = :selectOption";
            $em = $this->getDoctrine()->getEntityManager();
            $stmt = $em->getConnection()->prepare($storesQuery);
            $stmt->bindValue('employeesId', $user->getId());
            $stmt->execute();
            $listLocations = $stmt->fetchAll(\PDO::FETCH_ASSOC);
您可以对EntityType表单使用“choices”选项,所以您只需在表单类型和结果集中执行查询,而不是在查询生成器中执行查询,如

$query = ....;
$result = $query->execute();
$builder->add('store', 'entity', array(
    'class' => 'C2EducateToolsBundle:Stores',
    'label' => 'Center',
    'choices' => $result,
    'property' => 'name',
    'empty_value' => 'Select')
);
您可以对EntityType表单使用“choices”选项,所以您只需在表单类型和结果集中执行查询,而不是在查询生成器中执行查询,如

$query = ....;
$result = $query->execute();
$builder->add('store', 'entity', array(
    'class' => 'C2EducateToolsBundle:Stores',
    'label' => 'Center',
    'choices' => $result,
    'property' => 'name',
    'empty_value' => 'Select')
);

您可以通过使

同时,在我们的表单类型中:

// myBundle\Form\AddressType.php
// ...
Class AddressType
{
    protected $em;

    public function __construct(\Doctrine\ORM\EntityManager $em) // Type-hint so nothing funny gets in.
    {
        $this->em = $em; // And store it on our form type during instantiation.
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ... Then use it in here!
    }
}

您可以通过使

同时,在我们的表单类型中:

// myBundle\Form\AddressType.php
// ...
Class AddressType
{
    protected $em;

    public function __construct(\Doctrine\ORM\EntityManager $em) // Type-hint so nothing funny gets in.
    {
        $this->em = $em; // And store it on our form type during instantiation.
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ... Then use it in here!
    }
}

自定义?你能解决你的问题吗?自定义?你能解决你的问题吗?是的,你是对的,我想写查询并传递结果,但实体管理器中的问题在那里不可用,我如何配置它?我以为你已经在使用它了,因为你在选项中传递$em,所以你可以通过$em=$options['em']获得它们;或者你可以像@Rhono answerYes一样在服务中传递em,你是对的,我想写查询并传递结果,但实体管理器中的问题在那里不可用,我如何配置它?我以为你已经在使用它了,因为你在选项中传递$em,所以你可以通过$em=$options['em']获得它们;或者你可以在服务中传递它们,就像@Rhono-answer一样