I'm using the json_array in my entity to store the transit cities (Symfony 3.1 project). An extract from my Entity is as follows:
class Travel {
/**
* @var integer
*
* @ORM\Column(name="travel_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $travelId;
/* OTHER VARIABLES HERE */
/**
* @var integer
* @ORM\Column(name="transit_cities", type="json_array", nullable=true)
*/
protected $transitCities;
/* OTHER VARIABLES HERE */
public function __construct() {
$this->transitCities = new ArrayCollection();
}
/* GETTERS AND SETTERS HERE */
}
And in my form, I am using ChoiceType::class with allow_add set to true. Users will have option to add new values and remove the existing ones:
$builder->add('transitCities', CollectionType::class, array(
'entry_type' => null,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'error_bubbling'=> false,
'entry_options' => array(
'attr' => array('class' => 'transit-cities')
),
))
This works pretty well (have not set-up anything special inside the controller) with the default successful form submission. The persisted data have such structure:
[
0 => "City 1",
1 => "City 2",
// and so on
]
where the keys (0 and 1 in this example) are auto generated by Doctrine. But I would like to use my own keys rather than the default index numbers and preferably also add other relevant information, as shown below:
[
{'123' => 'City 1', 'dateTime' => 'Travel-date 1', 'opt'=> 1},
{'45' => 'City 2', 'dateTime' => 'Travel-date 2', 'opt'=> 2},
// and so on
]
123 and 45 could be the FK for city-ids while dateTime and opt could be other related information of each row.
==== DETAILS ADDED ===
Here is my City entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cities
*
* @ORM\Table(name="city")
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\CityRepository")
*/
class City
{
/**
* @var string
* @ORM\Column(name="name", type="string", length=70, nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=32, nullable=false)
*/
private $country;
/**
* @var integer
*
* @ORM\Column(name="city_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $cityId;
/* CONST */
public function __toString() {
return $this->name;
}
/* OTHER PARAMETERS HERE */
/* GETTERS AND SETTERS */
/**
* Set name
*
* @param string $name
*
* @return City
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get cityId
*
* @return integer
*/
public function getCityId()
{
return $this->cityId;
}
}
and I want to make use of the cityId for json_key. Example, if the user chose city "London" with corresponding cityId as say 100 and again city "Amsterdam" with cityId 115, then key-pair in json_array should be like:
{'100' => 'London', 'dateTime' => '2000-01-01', 'opt'=>1}
{'115' => 'Amsterdam', 'dateTime' => '2000-01-02', 'opt'=> 4}
where, dateTime is picked by user with js-date-picker and opt is un-mapped any arbitrary value just for example. Transit-Cities can be added and removed by user as per their choice (is already functioning).
How can this be achieved? Please suggest.