symfony3 Коллекция форм добавить массив prob

Я хочу сохранить массив в БД, как этот урок.

class Itineraire
{

/**
* @ORM\GeneratedValue
* @ORM\Id
* @ORM\Column(type="integer")
*/private $id ;
/**
* @ORM\ManyToOne(targetEntity="Croisiere")
*/

public $croisiere;/**
* @ORM\Column(type="integer",nullable=true)
*/
private $jour ;
/**
* @ORM\Column(type="string",length=255,nullable=true)
*/

class Croisiere
{

/**
* @ORM\GeneratedValue
* @ORM\Id
* @ORM\Column(type="integer")
*/

private $id ;

/**
* @ORM\ManyToOne(targetEntity="Navire",inversedBy="Croisiere")
* @ORM\JoinColumn(name="id_navire", referencedColumnName="id")
*/

public $id_navire;/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Itineraire", mappedBy="Croisiere" )
* @ORM\OrderBy({"id"="ASC"})
*/

protected $days ;formType =
->add('days',CollectionType::class, array('entry_type' =>                    ItineraireType::class,
'allow_add'    => true,
'by_reference' => false,
"prototype"=>true,
"prototype_data"=>new Itineraire()

действие

public function addCruiseAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) ) {

return $this->redirectToRoute('fos_user_security_login');
}
$id =  $this->getUser()->getId();

$croisiere = new Croisiere();
$em=$this->getDoctrine()->getManager();
$ProfileCroisiere = $em->getRepository('GstaycarBundle:ProfileCroisiere')->findOneBy(array('id_user' => $id ));$tab = new ArrayCollection();

// Create an ArrayCollection of the current Tag objects in the database/*$day1 = new Itineraire();

$croisiere->getDays()->add($day1);
$day2 = new Itineraire();

$croisiere->getDays()->add($day2);*/$form = $this->createForm(CroisiereType::class,$croisiere);$form->handleRequest($request);

if($form->isSubmitted() ) {
if ($form->isValid()){
var_dump($croisiere);
$croisiere->setIdProfile($ProfileCroisiere);

$em->persist($croisiere);

foreach ($tab as $day) {$em->persist( $day);

}

//$em->persist( $day2);
//$em->persist( $day1);
$em->flush();
//return $this->redirect($this->generateUrl('show_ship',array('msg'=>"add successful")));
}
}

**twig**

<ul class="days" data-prototype="{{            form_widget(form.days.vars.prototype)|e }}">
{% for f in form.days %}
{{ form_widget(f) }}
{% endfor %}
</ul>
</div>

Script Jquery
<script>
var $collectionHolder;// setup an "add a tag" link
var $addTagLink = $('<button href="#"class="add_tag_link">Add a       tag</button>');
var $newLinkLi = $('<li></li>').append($addTagLink);
var index=0;
jQuery(document).ready(function() {
// Get the ul that holds the collection of days
$collectionHolder = $('ul.days');

// add the "add a tag" anchor and li to the days ul
$collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index',   $collectionHolder.find(':input').length);

$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();

// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});

function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');

// get the new index
var index = $collectionHolder.data('index');

// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);

// increase the index with one for the next item
$collectionHolder.data('index', index + 1);

// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}

</script>

<script>
var $collectionHolder;// setup an "add a tag" link
var $addTagLink = $('<button href="#"class="add_tag_link">Add a       tag</button>');
var $newLinkLi = $('<li></li>').append($addTagLink);
var index=0;
jQuery(document).ready(function() {
// Get the ul that holds the collection of days
$collectionHolder = $('ul.days');

// add the "add a tag" anchor and li to the days ul
$collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);

$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();

// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});

function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');

// get the new index
var index = $collectionHolder.data('index');

// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);

// increase the index with one for the next item
$collectionHolder.data('index', index + 1);

// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}

это мнение
это то, что я хочу сохранить в базе данных в виде массива

 [1]: https://i.stack.imgur.com/igvfN.png

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector