有什么更好的方法?数据转换器?表格活动?表格模型?在持久化之前手动设置所有实体属性?
实体:
<?PHP namespace Acme\DemoBundle\Entity;use Doctrine\ORM\MapPing as ORM;/** * @ORM\Entity * @ORM\table(name="event") */class EventEntity{ /** * @ORM\Column(name="ID",type="integer",nullable=false) * @ORM\ID * @ORM\GeneratedValue(strategy="IDENTITY") */ private $ID; // ... /** * @ORM\Column(name="date_time",type="datetime",nullable=false) */ protected $datetime; public function getID() { return $this->ID; } // ... public function getDateTime() { return $this->datetime; } public function setDateTime(\DateTime $datetime) { $this->datetime = $datetime; }}
FormType:
<?PHP namespace Acme\DemoBundle\Form\Type;use JMS\DIExtraBundle\Annotation as DI;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolverInterface;class EventType extends AbstractType{ public function buildForm(FormBuilderInterface $builder,array $options) { $builder // ... ->add('date','date',[ 'required' => true,'Widget' => 'single_text','format' => 'dd.MM.yyyy' ] ) ->add('time','time',[ 'required' => false,'Widget' => 'single_text' ] ); } public function setDefaultoptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Acme\DemoBundle\Entity\EventEntity' //Acme\DemoBundle\Form\Model\EventModel ? )); } public function getname() { return 'event'; }}解决方法 éhé,这是一个很好的问题.
我会选择最简单,最通用,可重用的解决方案.
我不会为了表单映射而在我的模型上实现方法,但如果它有意义,为什么不简单地使用模型API?
<?PHPclass EventEntity{// assume $this->datetime is initialized and instance of DateTimepublic function setDate(\DateTime $date){ // i don't kNow if this works! $this->datetime->add($this->datetime->diff($date));}public function setTime(\DateTime $date){ $this->datetime->add($this->datetime->diff($date));}}总结
以上是内存溢出为你收集整理的symfony – 将两个表单字段合并为一个实体值全部内容,希望文章能够帮你解决symfony – 将两个表单字段合并为一个实体值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)