加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 09:33:32 所属栏目:PHP 来源:互联网
导读:我尝试将一个表单类型嵌入另一个表单类型: $builder-add(geolocation, new GeolocationType()); 但是当我尝试绑定请求到表单 if($request-getMethod() == POST) { $form-bindRequest($request);} 我得到错误 Catchable Fatal Error: Argument 1

我尝试将一个表单类型嵌入另一个表单类型:

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

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

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

我得到错误

Catchable Fatal Error: Argument 1 passed to CompanyStoreBundleDocumentPlace::setGeolocation()
must be an instance of CompanyStoreBundleDocumentGeolocation,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

namespace CompanyStoreBundleDocument;

use DoctrineODMMongoDBMappingAnnotations as MongoDB;

/**
 * @MongoDBDocument(collection="places")
 */
class Place
{
    /**
     * @MongoDBId
     */
    private $id;

    /**
     * @MongoDBString
     */
    private $title;

    /**
     * @MongoDBEmbedOne(targetDocument="Geolocation")
     */
    private $geolocation;


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

    /**
     * Set geolocation
     *
     * @param CompanyStoreBundleDocumentGeolocation $geolocation
     */
    public function setGeolocation(CompanyStoreBundleDocumentGeolocation $geolocation)
    {
        $this->geolocation = $geolocation;
    }

    /**
     * Get geolocation
     *
     * @return CompanyStoreBundleDocumentGeolocation $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 CompanyStoreBundleDocument;

use DoctrineODMMongoDBMappingAnnotations as MongoDB;

/**
 * @MongoDBEmbeddedDocument
 */
class Geolocation
{
    /**
     * @MongoDBId
     */
    private $id;

    /**
     * @MongoDBFloat
     */
    private $latitude;

    /**
     * @MongoDBFloat
     */
    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 CompanyAdminBundleFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;
use CompanyAdminBundleFormTypeGeolocationType;

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' => 'CompanyStoreBundleDocumentPlace',);
    }
}

GeolocationType.php

namespace CompanyAdminBundleFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;

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' => 'CompanyStoreBundleDocumentGeolocation');
    }

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

谢谢你们

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

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

setGeolocation($geolocation)

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读