kunicmarko / simple-configuration-bundle
此包已被废弃,不再维护。未建议替代包。
这是一个为 Sonata 管理添加键值存储的 Symfony Bundle。
v1.0.2
2017-11-24 08:06 UTC
Requires
- php: ^7.1
- sonata-project/admin-bundle: ^3.2
- symfony/symfony: ~2.8|~3.0
Requires (Dev)
This package is auto-updated.
Last update: 2022-02-01 13:10:44 UTC
README
此扩展包为 sonata 管理添加键值存储,您还可以轻松扩展扩展包并添加自己的类型。
此扩展包依赖于 SonataAdminBundle
文档
安装
1. 将以下内容添加到 composer.json 中的 require
键
composer require kunicmarko/simple-configuration-bundle
2. 在 app/AppKernel.php
中注册扩展包
$bundles = array(
// ...
new KunicMarko\SimpleConfigurationBundle\SimpleConfigurationBundle(),
);
如果您未使用 auto_mapping,请将其添加到您的 ORM 映射中
# app/config/config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
...
SimpleConfigurationBundle: ~
3. 更新数据库
app/console doctrine:schema:update --force
4. 清除缓存
app/console cache:clear
如何使用
在您的 twig 模板中,您可以调用它如下
{{ simple_configuration.getAll() }}
{{ simple_configuration.getValueFor('name') }}
如果您想在控制器中使用它,您可以这样做
$this->get('simple_configuration.service.configuration')->getAll()
$this->get('simple_configuration.service.configuration')->getValueFor('name')
添加新类型
如果您想添加新类型,您可以这样做
# app/config/config.yml
simple_configuration:
types:
newtype: YourBundle\Entity\NewType
创建新类型
您的新类型必须扩展 AbstractConfigurationType,您还必须指定用于 sonata 列表的模板(可以是 sonata 模板或您自己创建的),以及字段在表单中的渲染方式。
1. 无新列的新类型
namespace YourBundle\Entity;
use KunicMarko\SimpleConfigurationBundle\Entity\AbstractConfigurationType;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class NewType extends AbstractConfigurationType
{
/**
* {@inheritDoc}
*/
public function getTemplate() : string
{
return 'SonataAdminBundle:CRUD:list_string.html.twig';
}
/**
* {@inheritDoc}
*/
public function generateFormField(FormMapper $formMapper) : void
{
$formMapper->add('value', TextType::class, ['required' => false]);
}
}
2. 新列的新类型
如以下示例代码所示,我们添加了新的 $date
字段,唯一必要的是像下面那样覆盖 getValue()
方法,将新字段的 getter 代理到您的 getter。
namespace YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SimpleConfigurationBundle\Entity\AbstractConfigurationType;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
class NewType extends AbstractConfigurationType
{
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date", nullable=true)
*/
private $date;
public function setDate(?\DateTime $date) : self
{
$this->date = $date;
return $this;
}
public function getDate() : ?\DateTime
{
return $this->date;
}
public function getValue() : ?\DateTime
{
return $this->getDate();
}
/**
* {@inheritDoc}
*/
public function getTemplate() : string
{
//return 'SonataAdminBundle:CRUD:list_string.html.twig'; can also be used
return 'SimpleConfigurationBundle:CRUD:list_field_date.html.twig';
}
/**
* {@inheritDoc}
*/
public function generateFormField(FormMapper $formMapper) : void
{
$formMapper->add('date', DateType::class, ['required' => false]);
}
}
不要忘记在添加新字段后更新数据库
app/console doctrine:schema:update --force
其他功能
当包含此扩展包时,您将能够访问我需要的某些 twig 过滤器。
已过时间
在 twig 中,您可以使用 |elapsed
过滤器,并将得到可读的时间,它适用于时间戳或 DateTime 对象。
{{ var|elapsed }}
#outputs "time" ago, 5 days ago, 5 minutes ago, just now, 1 month ago, etc.