首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony2嵌入式表单与MongoDB问题

Symfony2嵌入式表单与MongoDB问题
EN

Stack Overflow用户
提问于 2012-03-06 15:57:43
回答 1查看 1.7K关注 0票数 13

我试图将表单类型嵌入到另一种表单类型中:

代码语言:javascript
复制
$builder->add('geolocation', new GeolocationType());

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

代码语言:javascript
复制
if($request->getMethod() == 'POST') {
  $form->bindRequest($request);
}

我知道错误了

可捕获的致命错误:传递给Company\StoreBundle\Document\Place::setGeolocation()的参数1必须是Company\StoreBundle\

\Geolocation的实例,数组给定,调用

/var/www/Company/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php在第392行,并在/var/www/Company/src/Company/StoreBundle/Document/Place.php第43行中定义

data_class是为PlaceType和GeolocationType设置的

这是我的代码:

Place.php

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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';
    }
}

谢谢大家

EN

回答 1

Stack Overflow用户

发布于 2015-12-14 09:06:45

我认为您不应该在setter方法中使用依赖关系--在这种情况下.

因此,与其:

代码语言:javascript
复制
setGeolocation(\Company\StoreBundle\Document\Geolocation $geolocation)

试一试,看看它是否正常工作:

代码语言:javascript
复制
setGeolocation($geolocation)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9587228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档