vendredi 29 mai 2015

symfony amazon S3 database

I have project in Symfony 2.6 I i=use amazon S3 service and my photo upload in server amazon. I dump url photo like this: http://ift.tt/1ACvOAj and my question how I write this url in my database and read this photo for my template ?

Entity Photo

class Photo

{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id;

/**
 * @ORM\Column(type="string")
 */
private $title;

/**
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(type="datetime", name="createdAt")
 */
protected $createdAt;

/**
 * @ORM\Column(name="photo_storage")
 * @Assert\File( maxSize="20M")
 */
private $photo;

and I have formtype for this entity

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('photo', 'file', array(
            'label' => 'Photo',
        ))
        ->getForm();
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Photo',
    ));
}

public function getName()
{
    return 'photo';
}

and I my controller

    public function addAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $post = new Photo();

    $form = $this->createForm(new AddPhotoType(), array());


    if ($request->isMethod('POST')) {
        $form->bind($request);
        if ($form->isValid()) {
            $data = $form->getData();
            $url = sprintf(
                '%s/%s',
                $this->container->getParameter('acme_storage.amazon_s3.base_url'),
                $this->getPhotoUploader()->upload($data['photo'])
            );

            dump($url);
            $em->persist($post);
            $em->flush();

            return $this->redirect($this->get('router')->generate('homepage'));
        }
    }

    return array(
        "form" => $form->createView(),
    );
}

/**
 * @return \StorageBundle\Upload\PhotoUploader
 */
protected function getPhotoUploader()
{
    return $this->get('acme_storage.photo_uploader');
}

how to upload photos in amazon s3 (this work) and then set url to the local database ?




Aucun commentaire:

Enregistrer un commentaire