所以我在从子表单获取属性时遇到问题。情景是我已经制作了一个工作表单,其中选择了“雇主”。如果用户是应用程序管理员,他们可以选择所有雇主。如果用户是与多个雇主合作的工人,他们会选择表格中的雇主。否则(如果他们有一个雇主),我想用雇主设置一个隐藏的字段。此外,雇主子表单应该自己处理。
我的策略是在雇主子表单中设置一些属性,但是当我将这些属性添加到工作表中时,它们不会通过(特别是“值”)。
有谁知道我如何传播这些属性?
EmployerType(子表单)
<?php
namespace Agency\Work\WorkBundle\FormType;
use Agency\Work\WorkBundle\Transformer\EmployerTransformer;
use Agency\Work\WorkBundle\Transformer\SnowflakeTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EmployerType extends AbstractType
{
private $options;
private $usor;
private $single;
private $biller;
// public function __construct($usor) {
// $this->usor=$usor;
public function __construct( array $options=array() ) {
$this->usor=$options['usor'];
$this->em=$options['em'];
$this->single= ( 1 == $this->usor->getEmployer()->count() );
}
public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->create( 'employer'
, 'entity'
, array(
'class' => 'Agency\Work\WorkBundle\Entity\Employer'
, 'query_builder' =>
function ( $repository ) {
$qb = $repository->createQueryBuilder( 'b' )
->addOrderBy( 'b.name' );
if ( ! $this->usor->isAdmin() ) {
$qb->join( 'b.usor', 'u' )->andWhere( "u=$this->usor" );
}
return $qb;
}
)
)
;
if ($this->single) {
$builder->setOption('data'
, $this->single?( $this->usor->getEmployer()->first()->getId() ):''
);
$transformer=new EmployerTransformer( $this->em );
$builder->addModelTransformer($transformer);
}
var_dump($builder->getOptions());
}
/**
*
*
* @param object $resolver
*/
public function setDefaultOptions( OptionsResolverInterface $resolver ) {
$resolver->setDefaults( array(
'class' => 'Agency\Work\WorkBundle\Entity\Employer'
, 'label' => 'Employer'
, 'label_attr' => array( 'class' => 'txtl' )
) );
}
public function getParent() {
return $this->single ? 'hidden' : 'entity';
}
/**
*
*
* @return unknown
*/
public function getName() {
return 'employer';
}
}
WorkType(子表单)
<?php
namespace Agency\Work\WorkBundle\FormType;
use Agency\Work\WorkBundle\Transformer\EmployerTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class WorkType extends AbstractType
{
public function buildForm( FormBuilderInterface $builder, array $options ) {
$securityContext = $options['attr']['securitycontext'];
if ( isset( $options['attr']['em'] ) ) { $em = $options['attr']['em'];} else {$em=null;}
$usor = $securityContext->getToken()->getUser();
$builder->add(
$builder->create ( 'employer'
, new EmployerType( array( 'usor' => $usor, 'em'=>$em ) )
)
)
;
$builder
->add( 'workDate'
, null
, array(
'label' => 'End'
, 'label_attr' => array( 'class' => 'txtl' )
, 'attr' => array( 'class' => 'dateEnd' )
)
);
}
/**
*
*
* @param object $resolver
*/
public function setDefaultOptions( OptionsResolverInterface $resolver ) {
$resolver->setDefaults( array(
'data_class' => 'Agency\Work\WorkBundle\Entity\Consent'
) );
}
/**
*
*
* @return unknown
*/
public function getName() {
return 'jobtype';
}
}
答案 0 :(得分:1)
非常有趣的代码格式化,但很难阅读它。乍一看似乎这里的错误
'query_builder' =>
function ( $repository ) {
$qb = $repository->createQueryBuilder( 'b' )
->addOrderBy( 'b.name' );
if ( ! $this->usor->isAdmin() ) {
$qb->join( 'b.usor', 'u' )->andWhere( "u=$this->usor" );
}
return $qb;
}
您使用closure作为回调函数,因此需要使用use
关键字来提供关闭的上下文。
'query_builder' => function ( $repository ) use ($usor) {
$qb = $repository
->createQueryBuilder( 'b' )
->addOrderBy( 'b.name' );
if ( ! $usor->isAdmin() ) {
$qb->andWhere('b.usor = :u')->setParameter('u', $usor);
}
return $qb;
}