I have a CollectionType that I'm trying to use to insert several strings into an entity MotorsAdsFile
(The image entity) and have them related to the current entity through it's id
. I use Blueimp and each time an image is uploaded a create a new widget like this
scripts.js
$('.file-uploader').bind('fileuploaddone', function(e, data) {
$.each(data.files, function(index, file) {
$('.file-uploader').addAnotherWidget(file.name);
});
});
// $('.add-another-collection-widget').click(function(e) {
$.fn.addAnotherWidget = function(fileName) {
// var list = $($(this).attr('data-list'));
var list = $(this);
// Try to find the counter of the list
var counter = list.data('widget-counter') | list.children().length;
// If the counter does not exist, use the length of the list
if (!counter) {
counter = list.children().length;
}
// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data(' widget-counter', counter);
// create a new list element and add it to the list
var newElem = $(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
// $('.file-uploader').find('#listing_images_'+counter).attr('value', 'asdf');
var input = counter-1;
$('#listing_images_'+input).val(fileName);
};
So I create the new widget and make its value the filename. My CollectionType which is within the ListingType
form (the main entity that the images are supposed to be attached to looks like this
ListingType.php
->add('images', CollectionType::class, [
'label' => 'Drop files here',
// 'existingFiles' => $options['existingFiles'],
// 'editId' => $options['editId'],
'entry_type' => BlueimpType::class,
'prototype' => true,
'allow_add' => true,
'attr' => [
'class' => 'file-uploader',
'data-widget-tags' => '<li></li>',
],
'allow_delete' => true,
'entry_options' => [
'attr' => ['class' => ''],
],
])
And Blueimp, a custom type I created is
BlueimpType.php
<?php
namespace DirectoryPlatform\FrontBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use AdamQuaile\Bundle\FieldsetBundle\Form\FieldsetType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
class BlueimpType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
// default form options
'existingFiles' => '',
'listing_id' => '',
'editId' => '',
'data_class' => 'DirectoryPlatform\AppBundle\Entity\MotorsAdsFile',
));
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['existingFiles'] = $options['existingFiles'] ?? [];
$view->vars['editId'] = $options['editId'] ?? [];
}
public function getParent()
{
return TextType::class;
}
}
The OneToMany
and corresponding methods are in the Listing.php
entity
Listing.php
/**
* @ORM\OneToMany(targetEntity="MotorsAdsFile", mappedBy="listing", cascade={"persist"}, orphanRemoval=true)
*/
private $images;
/**
* @param mixed $images
*/
public function setImages($images)
{
$this->images = $images;
}
/**
* @return mixed
*/
public function getImages()
{
return $this->images;
}
And in MotorsAdsFile.php
(the image entity) I have a ManyToOne
for the image
MotorsAdsFile.php (Image entity)
<?php
namespace DirectoryPlatform\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="DirectoryPlatform\AppBundle\Repository\MotorsAdsFileRepository")
* @ORM\Table(name="motorsadsfile")
*/
class MotorsAdsFile
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="Listing", inversedBy="images")
* @ORM\JoinColumn(name="listing_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $listing;
/**
* @ORM\Column(type="string", length=255, name="images")
*/
protected $image;
/**
* @return mixed
*/
public function getImage()
{
return $this->image;
}
/**
* @param mixed $image
*/
public function setImage($image)
{
$this->image= $image;
}
/**
* @return mixed
*/
public function getListing()
{
return $this->listing;
}
/**
* @param Listing $listing
*/
public function setListing(Listing $listing)
{
$this->listing = $listing;
}
}
I've tried such things as using the data class as, not in BlueimpType.php
but as a part of CollectionType
, but I get Cannot read index "1" from object of type "Directory Platform\AppBundle\Entity\MotorsAdFile" because it doesn't implement \ArrayAccess.
and using TextType
to no avail. I'm not sure where exactly I'm supposed to implement the data_class
, in CollectionType
or in BlueimpType
. If I set it as the default option in BlueimpType
I get an error that's The object could not be created
but if I set it in CollectionType
I get the above error about ArrayAccess
.