R
R
Roman Gor2017-03-01 11:04:53
symfony
Roman Gor, 2017-03-01 11:04:53

Symfony Form does not map DocumentType. How? What for? Why?

I have three documents in the holder:
TravelBike\LocationBundle\Document\Track

<?php

namespace TravelBike\LocationBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * Class Track
 * @package TravelBike\LocationBundle\Document
 */
class Track
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var int
     */
    protected $user;

    /**
     * @var string
     */
    protected $name;

    /**
     * @var ArrayCollection|Point
     */
    protected $points;

    /**
     * Track constructor.
     */
    public function __construct()
    {
        $this->points = new ArrayCollection();
    }

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

    /**
     * Set user
     *
     * @param int $user
     * @return $this
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * Get user
     *
     * @return int $user
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }


    /**
     * Add point
     *
     * @param Point $point
     */
    public function addPoint(Point $point)
    {
        $this->points[] = $point;
    }

    /**
     * Remove point
     *
     * @param Point $point
     */
    public function removePoint(Point $point)
    {
        $this->points->removeElement($point);
    }

    /**
     * Get points
     *
     * @return \Doctrine\Common\Collections\Collection $points
     */
    public function getPoints()
    {
        return $this->points;
    }
}

TravelBike\LocationBundle\Document\Track:
    type: document
    fields:
        id:
            id:  true
        user:
            type: int
        name:
            type: string
    embedMany:
        points:
            targetDocument: TravelBike\LocationBundle\Document\Point

TravelBike\LocationBundle\Document\Point
<?php
namespace TravelBike\LocationBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * Class Point
 * @package TravelBike\LocationBundle\Document
 */
class Point
{
    /**
     * @var Coordinates
     */
    protected $coordinates;

    /**
     * @var string
     */
    protected $time;

    /**
     * Set coordinates
     *
     * @param Coordinates $coordinates
     * @return $this
     */
    public function setCoordinates(Coordinates $coordinates)
    {
        $this->coordinates = $coordinates;
        return $this;
    }

    /**
     * Get coordinates
     *
     * @return Coordinates $coordinates
     */
    public function getCoordinates()
    {
        return $this->coordinates;
    }

    /**
     * Set time
     *
     * @param string $time
     * @return $this
     */
    public function setTime($time)
    {
        $this->time = $time;
        return $this;
    }

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

TravelBike\LocationBundle\Document\Point:
    type: embeddedDocument
    fields:
        time:
            type: string
    embedOne:
        coordinates:
            targetDocument: TravelBike\LocationBundle\Document\Coordinates
    indexes:
        coordinates:
            keys:
                coordinates: 2d

TravelBike\LocationBundle\Document\Coordinates
<?php
namespace TravelBike\LocationBundle\Document;

/**
 * Class Coordinates
 * @package TravelBike\LocationBundle\Document
 */
class Coordinates
{
    /**
     * @var float
     */
    public $x;

    /**
     * @var float
     */
    public $y;

    /**
     * Set x
     *
     * @param float $x
     * @return $this
     */
    public function setX($x)
    {
        $this->x = $x;
        return $this;
    }

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

    /**
     * Set y
     *
     * @param float $y
     * @return $this
     */
    public function setY($y)
    {
        $this->y = $y;
        return $this;
    }

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

TravelBike\LocationBundle\Document\Coordinates:
    type: embeddedDocument
    fields:
        x:
            type: float
        y:
            type: float

As you can see, there is a Track->Point->Coordinates nesting here .
I send a request to create a form with the following data:
{
  "travel_bike_location_form_track": {
    "name": "japan",
    "user": 3,
    "points": [
      {
        "time": 123,
        "coordinates": {
          "x": 123.2,
          "y": 21321.2
        }
      },
      {
        "time": 1233,
        "coordinates": {
          "x": 123.2,
          "y": 21321.2
        }
      }
    ]
  }
}

I get a response:
{
  "track": {
    "id": "58b67f7d6d90b93f454a6dd6",
    "user": 3,
    "name": "japan",
    "points": [
      {
        "coordinates": null,
        "time": "123"
      },
      {
        "coordinates": null,
        "time": "1233"
      }
    ]
  }
}

Question:
why isn't Coordinates ("coordinates": null) mapped?
The form:
<?php
/**
 * Created by PhpStorm.
 * User: romangorbatko
 * Date: 3/1/17
 * Time: 6:33 AM
 */

namespace TravelBike\LocationBundle\Form\Type;

use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TravelBike\LocationBundle\Document\Coordinates;
use TravelBike\LocationBundle\Document\Point;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

/**
 * Class PointFormType
 * @package TravelBike\LocationBundle\Form\Type
 */
class PointFormType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('time', TextType::class)
            ->add('coordinates', DocumentType::class, [
                'class' => Coordinates::class
            ])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('data_class', Point::class);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'travel_bike_location_form_point';
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question