首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Symfony 2.0在实体内部获得服务

Symfony 2.0是一个开源的PHP框架,用于开发Web应用程序。在Symfony 2.0中,实体是一个普通的PHP类,它可以用于表示应用程序的领域模型。要在实体内部获得服务,可以使用依赖注入的方法。

依赖注入是一种设计模式,它允许将依赖项(例如服务)注入到类中。在Symfony 2.0中,可以使用构造函数或setter方法来实现依赖注入。

以下是一个示例,展示了如何在实体内部获得服务:

代码语言:php
复制
// src/Entity/MyEntity.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class MyEntity
{
    // ...

    /**
     * @var File
     *
     * @Assert\File(
     *     maxSize = "5M",
     *     mimeTypes = {"image/jpeg", "image/png", "image/gif"},
     *     maxSizeMessage = "The maxmimum allowed file size is 5MB.",
     *     mimeTypesMessage = "Only the file types JPG, PNG and GIF are allowed."
     * )
     */
    private $file;

    /**
     * @var string
     */
    private $filename;

    /**
     * @var \Symfony\Component\HttpFoundation\File\UploadedFile
     */
    private $temp;

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;

        if ($this->file) {
            $this->updatedAt = new \DateTime('now');
        }
    }

    /**
     * Get file.
     *
     * @return File
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            $this->filename = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }

        $this->file->move($this->container->getParameter('kernel.root_dir').'/../web/uploads', $this->filename);

        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

    public function getAbsolutePath()
    {
        return null === $this->filename
            ? null
            : $this->container->getParameter('kernel.root_dir').'/../web/uploads/'.$this->filename;
    }

    public function getWebPath()
    {
        return null === $this->filename
            ? null
            : $this->container->getParameter('kernel.root_dir').'/../web/uploads/'.$this->filename;
    }
}

在上面的示例中,我们使用构造函数注入了一个ContainerInterface类型的服务。然后,我们可以在实体的方法中使用这个服务,例如在preUpload()upload()方法中使用$this->container->getParameter()来获取配置参数。

需要注意的是,在Symfony 2.0中,直接在实体中使用服务可能会导致代码变得难以测试和维护。因此,建议使用更高级的设计模式,例如仓库模式或领域服务模式,来管理实体和服务之间的关系。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券