php – Symfony2嵌入式表单和MongoDB问题

前端之家收集整理的这篇文章主要介绍了php – Symfony2嵌入式表单和MongoDB问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试将一个表单类型嵌入另一个表单类型:
  1. $builder->add('geolocation',new GeolocationType());

但是当我尝试绑定请求到表单

  1. if($request->getMethod() == 'POST') {
  2. $form->bindRequest($request);
  3. }

我得到错误

Catchable Fatal Error: Argument 1 passed to Company\StoreBundle\Document\Place::setGeolocation()
must be an instance of Company\StoreBundle\Document\Geolocation,array given,called in
/var/www/Company/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.PHP on line 392 and
defined in /var/www/Company/src/Company/StoreBundle/Document/Place.PHP line 43

data_class是为PlaceType和GeolocationType设置的

这是我的代码

Place.PHP

  1. namespace Company\StoreBundle\Document;
  2.  
  3. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  4.  
  5. /**
  6. * @MongoDB\Document(collection="places")
  7. */
  8. class Place
  9. {
  10. /**
  11. * @MongoDB\Id
  12. */
  13. private $id;
  14.  
  15. /**
  16. * @MongoDB\String
  17. */
  18. private $title;
  19.  
  20. /**
  21. * @MongoDB\EmbedOne(targetDocument="Geolocation")
  22. */
  23. private $geolocation;
  24.  
  25.  
  26. /**
  27. * Get id
  28. *
  29. * @return id $id
  30. */
  31. public function getId()
  32. {
  33. return $this->id;
  34. }
  35.  
  36. /**
  37. * Set geolocation
  38. *
  39. * @param Company\StoreBundle\Document\Geolocation $geolocation
  40. */
  41. public function setGeolocation(\Company\StoreBundle\Document\Geolocation $geolocation)
  42. {
  43. $this->geolocation = $geolocation;
  44. }
  45.  
  46. /**
  47. * Get geolocation
  48. *
  49. * @return Company\StoreBundle\Document\Geolocation $geolocation
  50. */
  51. public function getGeolocation()
  52. {
  53. return $this->geolocation;
  54. }
  55.  
  56. /**
  57. * Set title
  58. *
  59. * @param string $title
  60. */
  61. public function setTitle($title)
  62. {
  63. $this->title = $title;
  64. }
  65.  
  66. /**
  67. * Get title
  68. *
  69. * @return string $title
  70. */
  71. public function getTitle()
  72. {
  73. return $this->title;
  74. }
  75. }

Geolocation.PHP

  1. namespace Company\StoreBundle\Document;
  2.  
  3. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  4.  
  5. /**
  6. * @MongoDB\EmbeddedDocument
  7. */
  8. class Geolocation
  9. {
  10. /**
  11. * @MongoDB\Id
  12. */
  13. private $id;
  14.  
  15. /**
  16. * @MongoDB\Float
  17. */
  18. private $latitude;
  19.  
  20. /**
  21. * @MongoDB\Float
  22. */
  23. private $longitude;
  24.  
  25.  
  26. /**
  27. * Get id
  28. *
  29. * @return id $id
  30. */
  31. public function getId()
  32. {
  33. return $this->id;
  34. }
  35.  
  36. /**
  37. * Set latitude
  38. *
  39. * @param float $latitude
  40. */
  41. public function setLatitude($latitude)
  42. {
  43. $this->latitude = $latitude;
  44. }
  45.  
  46. /**
  47. * Get latitude
  48. *
  49. * @return float $latitude
  50. */
  51. public function getLatitude()
  52. {
  53. return $this->latitude;
  54. }
  55.  
  56. /**
  57. * Set longitude
  58. *
  59. * @param float $longitude
  60. */
  61. public function setLongitude($longitude)
  62. {
  63. $this->longitude = $longitude;
  64. }
  65.  
  66. /**
  67. * Get longitude
  68. *
  69. * @return float $longitude
  70. */
  71. public function getLongitude()
  72. {
  73. return $this->longitude;
  74. }
  75. }

PlaceType.PHP

  1. namespace Company\AdminBundle\Form\Type;
  2.  
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilder;
  5. use Company\AdminBundle\Form\Type\GeolocationType;
  6.  
  7. class PlaceType extends AbstractType
  8. {
  9. public function buildForm(FormBuilder $builder,array $options)
  10. {
  11. $builder->add('title');
  12.  
  13. $builder->add('geolocation',new GeolocationType());
  14.  
  15. }
  16.  
  17. public function getName()
  18. {
  19. return 'place';
  20. }
  21.  
  22. public function getDefaultOptions(array $options)
  23. {
  24. return array(
  25. 'data_class' => 'Company\StoreBundle\Document\Place',);
  26. }
  27. }

GeolocationType.PHP

  1. namespace Company\AdminBundle\Form\Type;
  2.  
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilder;
  5.  
  6. class GeolocationType extends AbstractType
  7. {
  8. public function buildForm(FormBuilder $builder,array $options)
  9. {
  10. $builder
  11. ->add('latitude')
  12. ->add('longitude');
  13. }
  14.  
  15. public function getDefaultOptions(array $options)
  16. {
  17. return array('data_class' => 'Company\StoreBundle\Document\Geolocation');
  18. }
  19.  
  20. public function getName()
  21. {
  22. return 'geolocation';
  23. }
  24. }

谢谢你们

在这种情况下,我觉得你不应该在你的setter方法中使用依赖关系
所以代替:
  1. setGeolocation(\Company\StoreBundle\Document\Geolocation $geolocation)

尝试这样看看它是否正常工作:

  1. setGeolocation($geolocation)

猜你在找的PHP相关文章