我需要什么
我必须构建一个允许输入非常简单的工资单的模块。代表这个的类是 PayStub
和 Detail
,它们是 POPO 和持久化的。此外,Detail
必须与 Fee
保持平衡,而 Fee
又是属于 Tariff
类的类。因此,Detail
包含有关正在支付的Fee
的信息。
让事情稍微复杂一点,用户必须支付的Fee
直接取决于Enrolment
的EducationLevel
,属于Student
,也属于 Person
。
我有什么
PayStub
有:
- 支付 stub 号码
- 付款日期
- 付款方式
- 观察
Detail
有:
- 费用
- 支票,如果有的话
- 支付金额
- 奖学金比例
费用
有:
- 费用数
- 价格
属于 Tariff
类:
- 总金额
- 年份
- 教育程度
问题
当我构建表单时,整个问题就来了。
PayStubType
:
class PayStubType extends AbstractType
{
//I need to pass the years and the DAOs because I need to use them later
public function __construct(School $c, DAOPerson $dao, $years = array(), DAOFee $dc)
{
$this->c = $c;
$this->dao = $dao;
$this->years = $years;
$this->dc = $dc;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$sch = $this->c;
$std = $this->dao->getEnroledStudents($sch)[0];
$builder->add('student', 'entity', array(
'label' => 'Student',
'class' => 'System\SchoolsBundle\Person\Person',
'mapped' => false,
'query_builder' => $this->dao->getEnroledStudents($sch, true)
))->add('payDate', 'datetime', array(
'label' => 'Payment Date',
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'dd/MM/yyyy'
))->add('payMethod', 'choice', array(
'label' => 'Payment Method',
'choices' => EPaymentMethod::$arrPayMethods
))->add('stubNumber', 'text', array(
'label' => 'Pay Stub No.',
'required' => false
))->add('observation', 'textarea', array(
'label' => 'Observations',
'max_length' => 1024,
'required' => false
));
$years = $this->years;
$dc = $this->dc;
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($sch, $std, $years, $dc) {
$form = $event->getForm();
$data = $event->getData();
$stdRole = $std->getInfoContainer()->getRole('STUDENT');
$form->add('details', 'collection', array(
'type' => new DetailType($sch, $std, $years, $dc),
'label' => false,
'allow_add' => true,
'by_reference' => false
));
});
public function getName()
{
return 'paystubtype';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'System\SchoolsBundle\Payments\PayStub'
));
}
private $c;
private $dao;
private $years;
private $dc;
}
详细类型
:
class DetailType extends AbstractType
{
public function __construct(School $c, Student $al, $years = array(), DAOFee $dc)
{
$this->c = $c;
$this->al = $al;
$this->years = array_reverse($years, true);
$this->dc = $dc;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$sch = $this->c;
$list = array(); //List of scholarship percentages
for ($i=0; $i<=100; $i++) {
$list[(string)($i/100)] = $i."%";
}
$pref = min($cole->getSchoolYear(), array_values($this->years)[0]);
$builder->add('ct', 'choice', array(
'label' => false,
'mapped' => false,
'choices' => Fee::$arrFees //A list of possible fees. The only possible values are the enrolment price and one fee per school month. Read after the code for a longer explanation about how this works.
))->add('year', 'choice', array(
'mapped' => false,
'label' => false,
'choices' => $this->years, //Years that have tariffs registered
'preferred_choices' => array($pref) //The minimum between the current school year and the last year where tariffs were registered
))->add('cheque', 'entity', array(
'label' => false,
'class' => 'System\SchoolsBundle\Payments\Cheque',
'property' => 'numberAndBank',
'required' => false,
'empty_value' => 'Select Cheque',
'query_builder' => function(EntityRepository $er) use ($sch) {
return $er->createQueryBuilder('u')
->where('u.school = ?1')
->orderBy('u.number')
->setParameter(1, $sch);
}
))->add('amount', 'text', array(
'label' => false,
))->add('scholarshipPerc', 'choice', array(
'label' => false,
'choices' => $list
));
// From here on, it gets blurry. Read below for more.
}
public function getName()
{
return 'detailtype';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'System\SchoolsBundle\Payments\Detail'
));
}
private $c;
private $al;
private $years;
private $dc;
}
关于费用选择字段,我没有使用实体类型,因为它会涉及 AJAX 查询,每次更新学生或年份时,我都必须重新加载集合类型的原型(prototype),加载新的年的费用,并添加我不确定能否正常工作的极其复杂的事件。所以我决定使用通用列表,稍后再进行处理。
不过,这里是事情变得复杂的地方。我正在研究这些事件,并决定 PRE_SUBMIT
事件能够捕获年份和费用,所以我只是查询它并将其添加到对象中。但是,事件不处理映射的数据类型,所以我必须将这个新信息传递给另一个事件,我决定这样做:
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) use ($sch, $al) {
$form = $event->getForm();
$data = $event->getData();
if (array_key_exists('ct', $data) == true && array_key_exists('year', $data) == true) {
$et = $al->getEnrolmentBySchool($sch, $data['year'])->getEducationLevel();
$year = $data['year'];
$feeNumber = $data['ct'];
$form->add('fee', 'entity', array(
'label' => false,
'class' => 'System\SchoolsBundle\Payments\Fee',
'property' => 'feeName',
'query_builder' => function(EntityRepository $er) use ($et, $year, $feeNumber) {
return $er->createQueryBuilder('u')
->innerJoin('u.tariff', 'a')
->innerJoin('a.edType', 'et')
->where('et = ?1')
->andWhere('a.year = ?2')
->andWhere('u.feeNumber = ?3')
->orderBy('u.feeNumber')
->setParameter(1, $et)
->setParameter(2, $year)
->setParameter(3, $feeNumber);
}
));
}
});
我在想我可以稍后接收数据,手动映射它,然后丢弃它(即从表单中删除它)。但是,POST_SUBMIT
事件没有正确接收对象,无论是表单(空值)还是对象(同样的问题)。
我是否正确地处理了这个问题?有没有更简洁的方法来做到这一点,或者我可以用这种方式解决这个问题吗?
最佳答案
我想提出一些考虑:
首先是代码,我觉得你的 Form 里面的逻辑开始有点复杂,最好避免在同一个类中有太多的代码行,所以我想考虑根据 SRP(单一职责原则)进行重构).
第二件事是我真的不知道你的方法是否正确。
我可以假设它是,也许使用 PRE_SUBMIT 可以促进事情,但我想更多地关注你的架构。
我的意思是,我们需要更多地了解费用问题。
如果您认为获取该值会消耗时间并且还会减慢表单的提交过程(必须尽可能快),
可能值得考虑一些替代方案:
- 将表单拆分为 2 个子表单(例如 2 个页面,第 1 步和第 2 步);
- 重组模型数据,例如创建另一个表,或者更好的 View 或可以快速获取值的 Api;
- 稍后使用 cron 作业计算该值。
希望此观点对您有所帮助。
关于php - Symfony2 - 在事件之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472854/