2012-03-06 809 views
13

trato de integrar un tipo de formulario en otro tipo de formulario:Symfony2 forma embebida y emisión MongoDB

$builder->add('geolocation', new GeolocationType()); 

Sin embargo cuando intento y obligar a la solicitud de la forma

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

me sale el error

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

el data_class se establece ya sea para el PlaceType y la Geol ocationType

Aquí está mi código:

Place.php

namespace Company\StoreBundle\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 

/** 
* @MongoDB\Document(collection="places") 
*/ 
class Place 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    private $id; 

    /** 
    * @MongoDB\String 
    */ 
    private $title; 

    /** 
    * @MongoDB\EmbedOne(targetDocument="Geolocation") 
    */ 
    private $geolocation; 


    /** 
    * Get id 
    * 
    * @return id $id 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set geolocation 
    * 
    * @param Company\StoreBundle\Document\Geolocation $geolocation 
    */ 
    public function setGeolocation(\Company\StoreBundle\Document\Geolocation $geolocation) 
    { 
     $this->geolocation = $geolocation; 
    } 

    /** 
    * Get geolocation 
    * 
    * @return Company\StoreBundle\Document\Geolocation $geolocation 
    */ 
    public function getGeolocation() 
    { 
     return $this->geolocation; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 

    /** 
    * Get title 
    * 
    * @return string $title 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 
} 

Geolocation.php

namespace Company\StoreBundle\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 

/** 
* @MongoDB\EmbeddedDocument 
*/ 
class Geolocation 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    private $id; 

    /** 
    * @MongoDB\Float 
    */ 
    private $latitude; 

    /** 
    * @MongoDB\Float 
    */ 
    private $longitude; 


    /** 
    * Get id 
    * 
    * @return id $id 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set latitude 
    * 
    * @param float $latitude 
    */ 
    public function setLatitude($latitude) 
    { 
     $this->latitude = $latitude; 
    } 

    /** 
    * Get latitude 
    * 
    * @return float $latitude 
    */ 
    public function getLatitude() 
    { 
     return $this->latitude; 
    } 

    /** 
    * Set longitude 
    * 
    * @param float $longitude 
    */ 
    public function setLongitude($longitude) 
    { 
     $this->longitude = $longitude; 
    } 

    /** 
    * Get longitude 
    * 
    * @return float $longitude 
    */ 
    public function getLongitude() 
    { 
     return $this->longitude; 
    } 
} 

PlaceType.php

namespace Company\AdminBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use Company\AdminBundle\Form\Type\GeolocationType; 

class PlaceType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('title'); 

     $builder->add('geolocation', new GeolocationType()); 

    } 

    public function getName() 
    { 
     return 'place'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
       'data_class' => 'Company\StoreBundle\Document\Place', 
     ); 
    } 
} 

GeolocationType.php

namespace Company\AdminBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class GeolocationType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('latitude') 
      ->add('longitude'); 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array('data_class' => 'Company\StoreBundle\Document\Geolocation'); 
    } 

    public function getName() 
    { 
     return 'geolocation'; 
    } 
} 

Gracias a todos

+2

¿Alguna vez resolvió esto? –

Respuesta

3

creo que no se debe utilizar la dependencia en su método de selección en este caso ...
Así que en lugar de:

setGeolocation(\Company\StoreBundle\Document\Geolocation $geolocation) 

probar esto y ver si funciona correctamente:

setGeolocation($geolocation)