Vichuploader не работает на производственном сервере - PullRequest
0 голосов
/ 27 декабря 2018

У меня проблема с 2 дней!Я пытаюсь загрузить файл с помощью vichuploaderBundle в проект symfony 3.4.

Я уже делал это много раз.Но на этот раз ... Это не работает, и я не понимаю, почему.На моей локальной версии это работает нормально, но на моем производственном сервере это не работает.

Вот сообщение об ошибке: SQLSTATE [23000]: Нарушение ограничения целостности: 1048 Столбец 'image_name' не может бытьnull

Файловая сущность сохраняется (с идентификатором и датой создания, но имя изображения пустое ???) это похоже на отображение vichuploader ???

У меня есть сущность (NoteFrais), и у каждого NoteFrais есть одно отношение с другой сущностью (JustificatifDefraiement).

вот моя сущность JustificatifDefraiement:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * JustificatifDefraiement
 *
 * @ORM\Table(name="justificatif_defraiement")
 * @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository  \JustificatifDefraiementRepository")
 * @vich\Uploadable
 */
class JustificatifDefraiement
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * NOTE: This is not a mapped field of entity metadata, just a simple property.
 *
 * @Vich\UploadableField(mapping="justificatif", fileNameProperty="imageName")
 *
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $imageName;


/**
 * @ORM\Column(type="datetime")
 *
 * @var \DateTime
 */
private $updatedAt;

/**
 * Constructor
 */
public function __construct()
{
    $this->updatedAt = new \DateTime();
}

/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 * of 'UploadedFile' is injected into this setter to trigger the  update. If this
 * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
 * must be able to accept an instance of 'File' as the bundle will inject one here
 * during Doctrine hydration.
 *
 * @param File|UploadedFile $justificatifDefraiement
 * @return JustificatifDefraiement
 */
public function setImageFile(File $justificatifDefraiement = null)
{
    $this->imageFile = $justificatifDefraiement;

    if ($justificatifDefraiement) {
        $this->updatedAt =  new \DateTime();
    }

    return $this;

}

/**
 * @return File|null
 */
public function getImageFile()
{
    return $this->imageFile;
}



/**
 *
 * @param $imageName
 *
 * @return $this
 */
public function setImageName($imageName)
{
    $this->imageName = $imageName;

    return $this;
}

/**
 * @return string|null
 */
public function getImageName()
{
    return $this->imageName;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 *
 * @return JustificatifDefraiement
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

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

Моя форма:

class JustificatifDefraiementType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('imageFile', FileType::class, array(
        //'data_class' => null,
        'label' => false,
        'required' => true,
        'attr' => array(
            'class' => 'NoteFraisBootstrapFileInput',
            'type' => 'file',
            'placeholder' => 'Selectionner un justificatif (jpeg, png, jpg, pdf)',
            'data-preview-file-type' => 'text',
            'data-allowed-file-extensions' => '["jpeg", "png", "jpg", "pdf"]',
        )
    ));
}
/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MKG\MystiBundle\Entity\JustificatifDefraiement'
    ));
}

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

Конфигурация:

parameters:
locale: fr
app.path.logos: /uploads/logos
app.path.imports: /uploads/imports
app.path.justificatifs: /uploads/justificatifs

У меня есть эта связь с другим объектом:

class NoteFrais
{
//.......//
/**
 * @ORM\OneToOne(targetEntity="MKG\MystiBundle\Entity\JustificatifDefraiement", cascade={"persist"})
 * @ORM\JoinColumn(name="justificatif_defraiement_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
 */
private $justificatifDefraiement;
//.......//
}

И noteFraisType:

class NoteFraisType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        //.......//
        ->add('justificatifDefraiement', JustificatifDefraiementType::class, array(
            'required' => false));
}
/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MKG\MystiBundle\Entity\NoteFrais'
    ));
}

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

}

Пожалуйста, помогитея !!

1 Ответ

0 голосов
/ 28 декабря 2018

Когда вы используете VichUploader, вы должны использовать VichFileType внутри вашего FormType

Тогда ваш buildForm ...

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('imageFile', VichFileType::class, array(
        //Options ...
    ));
}
...