Symfony2 simple media bundle, (*1)
Installation
To install this bundle please follow the next steps:, (*2)
First add the dependencies to your composer.json
file:, (*3)
"require": {
...
"idci/simple-media-bundle": "dev-master"
},
Then install the bundle with the command:, (*4)
php composer update
Enable the bundle in your application kernel:, (*5)
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new IDCI\Bundle\SimpleMediaBundle\IDCISimpleMediaBundle(),
);
}
Include a resource in your config.yml
, (*6)
imports:
....
- { resource: @IDCISimpleMediaBundle/Resources/config/config.yml }
Now the Bundle is installed., (*7)
Configure your database parameters in the app/config/parameters.yml
then run, (*8)
php app/console doctrine:schema:update --force
To associate a media with an object, simply implements MediaAssociableInterface
, (*9)
<?php
...
use IDCI\Bundle\SimpleMediaBundle\Entity\MediaAssociableInterface;
/**
* Object
*/
class MyObject implements MediaAssociableInterface
{
...
Then when you wish to upload and associate a media, simply call the idci_simplemedia.manager
service to create a form and process it as explain below:, (*10)
// This classic form creation
// $form = $this->createForm(new MyObjectType(), $myObject);
// Now work like this:
$form = $this->get('idci_simplemedia.manager')->createForm(
new MyObjectType(),
$myObject,
array('provider' => 'file')
);
As you can see, the third parameter allow you to choose a provider.
For the moment only the file provider is ready to use., (*11)
To save and associate a media with your object, call the processForm
function like this:, (*12)
if ($this->getRequest()->isMethod('POST')) {
$form->bind($this->getRequest());
if ($form->isValid()) {
$myObject = $this->get('idci_simplemedia.manager')->processForm($form);
return $this->redirect($this->generateUrl(...));
}
}
Controller
In the following exemples, $obj
has to be an instance of a class which implement
the MediaAssociableInterface
. So to retrieve a set of media:, (*13)
// Related to an object
$medias = $this->get('idci_simplemedia.manager')->getMedias($obj);
// Related to an object filter on tags
$medias = $this->get('idci_simplemedia.manager')->getMedias($obj, array('tag1', 'tag2'));
// Related to tags
$medias = $this->get('idci_simplemedia.manager')->getMedias(null, array('tag1', 'tag2'));
To remove all medias in association with your object $obj
, use the removeAssociatedMedias
function available throw the service like this:, (*14)
$this->get('idci_simplemedia.manager')->removeAssociatedMedias($obj);
$this->getDoctrine()->getManager()->flush();
To remove just one media:, (*15)
$this->get('idci_simplemedia.manager')->removeMedia($media);
$em->flush();
VIEW
To display media in a twig template, (*16)
{% for media in medias(object) %}
{% endfor %}
{% for media in medias(object, ['tag']) %}
{% endfor %}
{% for media in medias(null, ['tag1', 'tag2']) %}
{% endfor %}
Controller
In the following exemples, $obj
has to be an instance of a class which implement
the MediaAssociableInterface
. So to retrieve a set of media:, (*17)
// All tags
$tags = $this->get('idci_simplemedia.manager')->getTags();
// Related to an object
$tags = $this->get('idci_simplemedia.manager')->getTags($obj);
VIEW
To retrieve tags in a twig template, (*18)
{% for tag in medias_tags %}
- {{ tag }}
{% endfor %}
{% for tag in medias_tags(object) %}
- {{ tag }}
{% endfor %}