perfectneeds/dynamic-form-single-lang-bundle

该软件包最新版本(dev-master)没有提供许可信息。

内容包

dev-master 2020-04-29 13:21 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:16 UTC


README

先决条件

  1. Symfony 3.4
  2. PNMediaBundle
  3. PNServiceBundle

安装

安装是一个快速(我保证!)7步过程

  1. 使用composer下载PNDynamicFormBundle
  2. 在AppKernel中启用Bundle
  3. 创建你的Post类
  4. 创建你的PostRepository类
  5. 配置PNDynamicFormBundle
  6. 导入PNDynamicFormBundle路由
  7. 更新你的数据库模式

第1步:使用composer下载PNDynamicFormBundle

使用composer要求这个bundle

$ composer require perfectneeds/dynamic-form-single-lang-bundle "~1.0"

第2步:在AppKernel中启用Bundle

使用composer要求这个bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new PN\MediaBundle\PNMediaBundle(),
        new \PN\ServiceBundle\PNServiceBundle(),
        new \PN\DynamicFormBundle\PNDynamicFormBundle(),
        // ...
    );
}

第3步:创建你的Post类

此bundle的目标是将一些Post类持久化到数据库中。因此,你的第一个任务是创建应用中的Post类。这个类可以看起来和表现成你想要的任何样子:添加任何你认为有用的属性或方法。这是你的Post类。

此bundle提供了一些基础类,这些类已经映射了大多数字段,以便更容易地创建你的实体。以下是使用方法:

  1. 扩展基础Post类(如果你使用任何Doctrine变体,则来自Entity文件夹)
  2. 映射id字段。它必须是受保护的,因为它是从父类继承的。

注意!

当你从bundle提供的映射超类扩展时,不要重新定义其他字段的映射,因为这些字段由bundle提供。

在以下部分中,您将看到根据您如何存储帖子(Doctrine ORM),您的Post类应该是什么样子的示例。

注意

文档使用名为DynamicFormBundle的bundle。然而,你可以当然地将你的帖子类放在你想要的任何bundle中。

注意!

如果你在Post类中重写了__construct()方法,请确保调用parent::__construct(),因为基础Post类依赖于它来初始化一些字段。

Doctrine ORM Post类

如果你通过Doctrine ORM持久化你的帖子,那么你的Post类应该位于你的bundle的Entity命名空间中,如下所示以开始

(你可以在这个类中添加所有其他实体之间的关系)

<?php
// src/PN/Bundle/DynamicFormBundle/Entity/Post.php

namespace PN\Bundle\DynamicFormBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

// DON'T forget the following use statement!!!
use PN\DynamicFormBundle\Entity\Post as BasePost;
use PN\DynamicFormBundle\Model\PostTrait;

 /**
 * Post
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="PN\Bundle\DynamicFormBundle\Repository\PostRepository")
 */
class Post extends BasePost {

    use PostTrait;
    
    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

第4步:创建你的PostRepository类

你可以使用这个Repository添加任何自定义方法

<?php
// src/PN/Bundle/DynamicFormBundle/Repository/PostRepository.php


namespace PN\Bundle\DynamicFormBundle\Repository;

use PN\DynamicFormBundle\Repository\PostRepository as BasePostRepository;

class PostRepository extends BasePostRepository {

}

第5步:配置PNDynamicFormBundle

根据你使用的数据存储类型,将以下配置添加到你的config.yml文件中。

# app/config/config.yml 

doctrine:
   orm:
        # search for the "ResolveTargetEntityListener" class for an article about this
        resolve_target_entities: 
            PN\MediaBundle\Entity\Image: PN\Bundle\MediaBundle\Entity\Image
            PN\MediaBundle\Entity\Document: PN\Bundle\MediaBundle\Entity\Document

pn_content:
    # The fully qualified class name (FQCN) of the Post class which you created in Step 3.
    post_class: PN\Bundle\DynamicFormBundle\Entity\Post

第6步:导入PNDynamicFormBundle路由文件

# app/config/routing.yml 

pn_media:
    resource: "@PNMediaBundle/Resources/config/routing.yml"

pn_content:
    resource: "@PNDynamicFormBundle/Resources/config/routing.yml"

第7步:更新你的数据库模式

现在,当bundle配置完成后,你需要更新你的数据库模式,因为你添加了一个新的实体,即你在第3步中创建的Post类。

$ php bin/console doctrine:schema:update --force

如何使用PNDynamicFormBundle

  1. 使用Doctrine ORM中的实体使用Post
  2. 在表单类型中使用Post
  3. 如何添加自定义字段 例如,摘要、描述等 ...
  4. 在控制器中使用Post
  5. 在类似show.html.twig的详情页面中使用Post

1. 使用Doctrine ORM中的实体使用Post

首先,您需要添加一个与需要使用的Post类的实体之间的关联,在src/PN/Bundle/DynamicFormBundle/Entity/Post.php中。例如:博主、产品等...
Post.php

<?php
// src/PN/Bundle/DynamicFormBundle/Entity/Post.php

namespace PN\Bundle\DynamicFormBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use PN\DynamicFormBundle\Entity\Post as BasePost;
use PN\DynamicFormBundle\Model\PostTrait;

/**
 * Post
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="PN\Bundle\CMSBundle\Repository\PostRepository")
 */
class Post extends BasePost {

    use PostTrait;
        
    // Add here your own relations
    
    /**
     * @ORM\OneToOne(targetEntity="\PN\Bundle\CMSBundle\Entity\DynamicPage", mappedBy="post")
     */
    protected $dynamicPage;
    
    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

DynamicPage.php

<?php

namespace PN\Bundle\CMSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use PN\ServiceBundle\Model\DateTimeTrait;

/**
 * DynamicPage
 *
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="dynamic_page")
 * @ORM\Entity(repositoryClass="PN\Bundle\CMSBundle\Repository\DynamicPageRepository")
 */
class DynamicPage {

    use DateTimeTrait;
    ....

    /**
     * @ORM\OneToOne(targetEntity="PN\Bundle\DynamicFormBundle\Entity\Post", inversedBy="dynamicPage", cascade={"persist", "remove" })
     */
    protected $post;
    
    ....
}

2. 在表单类型中使用Post

您需要将Post类型添加到任何表单类型中,以使用这个神奇的工具

DynamicPageType.php

<?php

namespace PN\Bundle\CMSBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

// DON'T forget the following use statement!!!
use PN\DynamicFormBundle\Form\PostType;


class DynamicPageType extends AbstractType {

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('post', PostType::class)
                ......
                ;
    }
    .....
}

3. 如何添加自定义字段

如果您需要为任何表单类型添加自定义字段

例如,在DyncamicPageType.php中添加一个shortDescription字段

<?php

namespace PN\Bundle\CMSBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use PN\DynamicFormBundle\Form\PostType;

// DON'T forget the following use statement!!!
use PN\DynamicFormBundle\Form\Model\PostTypeModel;


class DynamicPageType extends AbstractType {

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $postTypeModel = new PostTypeModel();
        $postTypeModel->add("description", "descriptionsss");
        $postTypeModel->add("brief", "Brief");
        
        /** documentation
         * @param string $name field_name (must not contain any spaces or special characters)
         * @param string $label field_label
         * @param array $options field_options
         */
        $postTypeModel->add({field_name}, {field_label}, {field_options});
    
        $builder
                ->add('post', PostType::class, [
                    //  DON'T forget the following statement!!!
                    "attributes" => $postTypeModel
                ])
                ......
                ;
    }
    .....
}

报告问题或功能请求

问题和功能请求在Github问题跟踪器中进行跟踪。

在报告错误时,最好在Symfony标准版本构建的基本项目中重现错误,以便扩展包的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。