opositatest/interest-user-bundle

此包已被废弃,不再维护。没有建议的替代包。
此包最新版本(dev-master)没有提供许可证信息。

用户兴趣,关注与取消关注

安装: 818

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 7

分支: 0

公开问题: 0

类型:symfony-bundle

dev-master 2020-02-17 10:51 UTC

This package is auto-updated.

Last update: 2024-08-26 20:10:52 UTC


README

基本安装...

Composer安装

composer require opositatest/interest-user-bundle

将包添加到AppKernel

# app/AppKernel.php
new Opositatest\InterestUserBundle\OpositatestInterestUserBundle(),

与用户连接

用户实体特例

# src/AppBundle/Entity/User
    ...
    // Add trait
    use \Opositatest\InterestUserBundle\Model\UserTrait {
        __construct as _traitconstructor;
    }
    ...
    // Add construct
    public function __construct()
    {
        $this->_traitconstructor();
        parent::__construct();
    }

在UserAdmin类中添加字段

您需要将followInterests和unfollowInterests添加到UserAdmin类中,因此

# src/AppBundle/Admin/UserAdmin
    protected function configureFormFields(FormMapper $formMapper)
    {
        ...
            ->add('followInterests', 'sonata_type_model', array('multiple' => true, 'by_reference' => false))
            ->add('unfollowInterests', 'sonata_type_model', array('multiple' => true, 'by_reference' => false))
        ...
    }

添加验证函数,因此

    public function validate(ErrorElement $errorElement, $object)
    {
        /** @var SyliusUser $user */
        $user = $object;
        foreach($user->getFollowInterests() as $followInterest) {
            if ($user->existUnfollowInterest($followInterest)) {
                $custom_error = "Interest ".$followInterest." used in follow and unfollow";
                $errorElement->with( 'enabled' )->addViolation( $custom_error )->end();
            }
        }
    }

配置config.yml

# app/config/config.yml
orm:
   resolve_target_entities:
      Opositatest\InterestUserBundle\Model\UserInterface: AppBundle\Entity\User

添加路由

将路由添加到您的项目中。它应该与nelmio api doc兼容

# app/config/routing.yml
opositatest_interestuser_api:
    resource: "@OpositatestInterestUserBundle/Resources/config/routing.yml"
    prefix:   /api/ # Prefix is customizable

API

启用分组功能的注解

# app/config/config.yml
framework:
    # ...
    serializer:
        enable_annotations: true

与SonataAdmin连接(可选)

添加OpositatestInterestUserBundle组

# app/config/config.yml
sonata_admin:
    ...
    dashboard:
        ...
        groups:
            OpositatestInterestUserBundle:
                label: "Intereses de Usuario"
        blocks:
            - { position: right, type: sonata.admin.block.admin_list, settings: { groups: [OpositatestInterestUserBundle] } }

用法

我们有三个功能,"/api/"是可自定义的前缀URL

添加兴趣

为登录用户添加兴趣

POST
/api/interest/{interest}

删除兴趣

从登录用户中删除兴趣

DELETE
/api/interest/{interest}

查看兴趣

返回全球和登录用户的兴趣

GET
/api//interests

测试

您可以使用以下单元测试进行尝试

./vendor/bin/phpunit vendor/opositatest/interest-user-bundle/Opositatest/InterestUserBundle

更多信息

此包管理用户的兴趣,允许添加和删除followInterest和unfollowInterest。

实现了两个重要逻辑

  1. 当添加新的兴趣到followInterest或unfollowInterest时,系统会自动添加子兴趣。这个过程是通过Interest实体进行的,如果使用SonataAdmin,否则通过InterestService服务。这种区别是因为通过API我们需要检查第2点,而通过Sonata,第2点已经通过Sonata自身的一个特殊函数进行了检查。

  2. 在followInterest和unfollowInterest中不能有相同的兴趣,因此,在Sonata中通过验证函数进行检查,而在API中每次添加新兴趣时都会进行检查。