perfectneeds / content-multi-lang-bundle

此包的最新版本(2.0.23)没有提供许可证信息。

内容包

2.0.23 2024-01-30 21:10 UTC

README

先决条件

  1. Symfony 3.4
  2. PNLocaleBundle
  3. PNMediaBundle
  4. PNServiceBundle

安装

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

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

第一步:使用 composer 下载 PNContentBundle

使用 composer 需求此 Bundle

$ composer require perfectneeds/content-multi-lang-bundle "~1.0"

第二步:在 AppKernel 中启用 Bundle

使用 composer 需求此 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new VM5\EntityTranslationsBundle\VM5EntityTranslationsBundle(),
        new PN\MediaBundle\PNMediaBundle(),
        new \PN\LocaleBundle\PNLocaleBundle(),
        new \PN\ServiceBundle\PNServiceBundle(),
        new \PN\ContentBundle\PNContentBundle(),
        // ...
    );
}

第三步:创建你的 Post 类

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

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

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

注意!

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

在以下部分,你会看到根据你如何存储帖子(Doctrine ORM)的 Post 类的示例。

注意

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

注意!

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

Doctrine ORM Post 类

如果你通过 Doctrine ORM 持久化帖子,那么你的 Post 类应该位于你的 Bundle 的 Entity 命名空间中,并像这样开始

你可以在此类中添加其他实体之间的关系

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

namespace PN\Bundle\ContentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use VM5\EntityTranslationsBundle\Model\Translatable;

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

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

    use PostTrait;
    
    /**
     * @ORM\OneToMany(targetEntity="PN\Bundle\ContentBundle\Entity\Translation\PostTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
     */
    protected $translations;
    
    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}
<?php
// src/PN/Bundle/ContentBundle/Entity/Translation/PostTranslation.php

namespace PN\Bundle\ContentBundle\Entity\Translation;

use Doctrine\ORM\Mapping as ORM;

// DON'T forget the following use statement!!!
use PN\ContentBundle\Entity\Translation\PostTranslation as BasePostTranslation;

/**
 * @ORM\Entity
 * @ORM\Table(name="post_translations")
 */
class PostTranslation extends BasePostTranslation {

    /**
     * @var
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PN\Bundle\ContentBundle\Entity\Post", inversedBy="translations")
     * @ORM\JoinColumn(name="translatable_id", referencedColumnName="id")
     */
    protected $translatable;

}

第四步:创建你的 PostRepository 类

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

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


namespace PN\Bundle\ContentBundle\Repository;

use PN\ContentBundle\Repository\PostRepository as BasePostRepository;

class PostRepository extends BasePostRepository {

}

第五步:配置 PNContentBundle

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

# app/config/config.yml 

doctrine:
   orm:
        # search for the "ResolveTargetEntityListener" class for an article about this
        resolve_target_entities: 
            VM5\EntityTranslationsBundle\Model\Language: PN\LocaleBundle\Entity\Language
            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\ContentBundle\Entity\Post
    # The fully qualified class name (FQCN) of the PostTranslation class which you created in Step 3.
    post_translation_class: PN\Bundle\ContentBundle\Entity\Translation\PostTranslation

第六步:导入 PNContentBundle 路由文件

# app/config/routing.yml 

pn_locale:
    resource: "@PNLocaleBundle/Resources/config/routing.yml"

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

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

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

现在该 Bundle 已配置,最后一步是更新你的数据库模式,因为你已经添加了一个新实体,即你在第 3 步中创建的 Post 类。

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

如何使用 PNContentBundle

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

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

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

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

namespace PN\Bundle\ContentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use VM5\EntityTranslationsBundle\Model\Translatable;
use PN\ContentBundle\Entity\Post as BasePost;
use PN\ContentBundle\Model\PostTrait;

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

    use PostTrait;
    
     /**
     * @ORM\OneToMany(targetEntity="PN\Bundle\ContentBundle\Entity\Translation\PostTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
     */
    protected $translations;
    
    // 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;
use VM5\EntityTranslationsBundle\Model\Translatable;
use PN\LocaleBundle\Model\LocaleTrait;

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

    use DateTimeTrait,
        LocaleTrait;
    ....

    /**
     * @ORM\OneToOne(targetEntity="PN\Bundle\ContentBundle\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\ContentBundle\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\ContentBundle\Form\PostType;

// DON'T forget the following use statement!!!
use PN\ContentBundle\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标准版构建的基本项目中重现它,以便包的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。