Archives by date

You are browsing the site archives by date.

【Symfony2.3】GedmoのTimestampableを使うときはリスナーの設定をお忘れなく。

services.yml

services:
    gedmo.listener.timestampable:
        class: Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

こんなんで詰まるなんて。

 

【Linux】よく使うCRON設定メモ。

0 */1 * * * 毎時 0分に 1時間おきに実行
0,10,20,30,40,50 * * * * 毎時0,10,20,30,40,50分に実行(10分おきに実行)
*/5 * * * * 5分おきに実行
23 6 * * * 6時23分に実行
55 23 * * * 23時55分に実行

 

【Symfony2.3】Gedmoのsoftdeleteableを使う際の注意。

またかよ。って感じだけども。
下記もymlで定義してあげないとだめっぽい。

doctrine:
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        entity_managers:
           default:
               auto_mapping: true
               mappings:
                   StofDoctrineExtensionsBundle: ~
               filters:
                   softdeleteable:
                       class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                       enabled: true

stof_doctrine_extensions:
    default_locale: ja_JP
    orm:
        default:
            #timestampもやるならココ 
            timestampable: true
            softdeleteable: true

 

【Symfony2.3】画像バイナリデータのブラウザ出力。

雰囲気こんな感じ。

/**
 * @Route("/media/{id}.{_format}", name="hoge_fuga_admin_media_show", defaults={"_format" = "json"})
 * @View(serializerEnableMaxDepthChecks=true, serializerGroups={"adminShow"})
 * @ParamConverter("entity", class="HogeFugaBundle:Image")
 * @Method("GET")
 */
public function showAction(Image $entity, Request $request, $id)
{
    $path = $this->getStragePath($entity->getPath()).'/'.$entity->getPath();
    
    $finfo = new \finfo(FILEINFO_MIME_TYPE);
    
    $response = new Response();
    $response->headers->set('Content-type', $finfo->file($path));
    $response->sendHeaders();
    $response->setContent(readfile($path));
     
    return $response;
}

ちなみにgetStragePath()の実装はこんな感じ。
※upload_root_dirをparameters.ymlで定義しとく。

/**
 * create dir and get file path by file name
 *
 * @param unknown $fileName
 * @throws \Symfony\Component\HttpFoundation\File\Exception\UploadException
 * @return string
 */
private function getStragePath($fileName)
{
    $fs = new Filesystem();
    $path = substr($fileName, 0, 1).'/'.substr($fileName, 0, 2);
    $dir = $this->get('kernel')->getRootDir().$this->container->getParameter('upload_root_dir').'/'.$path;
    if (!$fs->exists($dir)) {
        try {
            $fs->mkdir($dir, 0777);
        } catch (IOException $e) {
            throw new \Symfony\Component\HttpFoundation\File\Exception\UploadException('Cannot create directory.');
        }
    }
    return $dir;
}

ちなみにImageエンティティはこんな感じ。

namespace Hoge\FugaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Image
 *
 * @ORM\Table(name="images")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 * @ORM\Entity(repositoryClass="Hoge\FugaBundle\Repository\ImageRepository")
 */
class Image
{
    
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Groups({"adminIndex", "adminShow"})
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @JMS\Groups({"adminIndex", "adminShow"})
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="path", type="string", length=255)
     * @JMS\Groups({"adminIndex", "adminShow"})
     */
    private $path;

    /**
     * @var integer
     *
     * @ORM\Column(name="size", type="integer")
     * @JMS\Groups({"adminIndex", "adminShow"})
     */
    private $size;

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

    /**
     * Set name
     *
     * @param string $name
     * @return Image
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return Image
     */
    public function setPath($path)
    {
        $this->path = $path;
    
        return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set size
     *
     * @param integer $size
     * @return Image
     */
    public function setSize($size)
    {
        $this->size = $size;
    
        return $this;
    }

    /**
     * Get size
     *
     * @return integer 
     */
    public function getSize()
    {
        return $this->size;
    }

}