【问题标题】:How to implement Sylius OrderBundle in an existing Symfony2 application如何在现有 Symfony2 应用程序中实现 Sylius OrderBundle
【发布时间】:2016-03-09 15:52:26
【问题描述】:

我需要在我的 symfony2 应用程序中实现 syliusOrderBundle,我已经从他们的官方网站 http://docs.sylius.org/en/latest/bundles/SyliusOrderBundle/installation.html 一遍又一遍地阅读文档 我最终安装并启用了以下捆绑包

      new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
        new Sylius\Bundle\MoneyBundle\SyliusMoneyBundle(),
        new Sylius\Bundle\OrderBundle\SyliusOrderBundle(),
        new Sylius\Bundle\CartBundle\SyliusCartBundle(),
        new Sylius\Bundle\ProductBundle\SyliusProductBundle(),
        new Sylius\Bundle\ArchetypeBundle\SyliusArchetypeBundle(),
        new Sylius\Bundle\AttributeBundle\SyliusAttributeBundle(),
        new Sylius\Bundle\AssociationBundle\SyliusAssociationBundle(),
        new Sylius\Bundle\VariationBundle\SyliusVariationBundle(),

由于安全原因,问题是安装不必要的捆绑包时不舒服,并且 sylius 文档没有帮助。我所需要的只是能够将产品添加到订单中,如果您之前使用过它,请您帮忙。谢谢

【问题讨论】:

  • 您只需要Orders 还是需要ProductsOrders? Sylius 并没有太多解耦,而是尝试对其架构进行分层,因此一些组件和捆绑包将相互依赖。例如,Products 是受 Archetypes 约束的对象,这些对象定义了对象具有的 AttributesVariations
  • @adam 我只需要订单我有我的自定义产品实体可以满足我的目的,但订单文档要求我应该实现产品接口docs.sylius.org/en/latest/bundles/SyliusOrderBundle/…
  • 你需要做的就是让你的 Product 实体实现 ProductInterface 并在 Symfony 设置中配置它。` // src/App/AppBundle/Entity/Product.php 命名空间 App\AppBundle\Entity;使用 Sylius\Component\Product\Model\ProductInterface;类 Product 实现 ProductInterface { public function getName() { return $this->name; } }` 现在,您甚至不必将您的产品模型映射到订单项。这一切都是自动完成的。这就是关于实体的全部内容。
  • 哦,废话,那个特定页面已经过时了(2014 年 5 月):/ - 当时 Order 和 Product 是同一个捆绑包的一部分,但现在它们已经解耦了。如果你给我一点时间,我会写一个关于如何在当前版本(0.17)中同时使用 Order 组件和 OrderBundle 的答案......并且可能会向 Sylius-Docs 提交拉取请求
  • @Adam 那将是一个非常棒的人,我会很高兴过去 3 天我一直在执行这项任务,这真的很令人沮丧。谢谢我在等待

标签: php symfony sylius


【解决方案1】:

首先,您只需要sylius/order-bundle 0.17 版(在撰写本文时)

$ composer require sylius/order-bundle "^0.17"

然后将必要的捆绑包添加到app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // Bundles you've already registered go here.

        // The following bundles are dependencies of Sylius ResourceBundle.
        new FOS\RestBundle\FOSRestBundle(),
        new JMS\SerializerBundle\JMSSerializerBundle($this),
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),

        // The following Sylius bundles are dependencies of Sylius OrderBundle 
        new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
        new Sylius\Bundle\MoneyBundle\SyliusMoneyBundle(),
        new Sylius\Bundle\SequenceBundle\SyliusSequenceBundle(),

        new Sylius\Bundle\OrderBundle\SyliusOrderBundle(),

        // Doctrine bundle MUST be the last bundle registered.

        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
    ];

    //....

    return $bundles;
}

现在要将 OrderBundle 与您自己的实体一起使用,您需要创建一个 OrderOrderItem 实体来扩展 Sylius 提供的实体。

对于Order,我们将添加一个字段来捕获访问者的电子邮件地址。您可以捕获当前用户,或任何您想确定谁下订单的用户。

<?php

namespace AppBundle\Entity;

use Sylius\Component\Order\Model\Order as SyliusOrder;

class Order extends SyliusOrder
{
    /**
     * @var string
     */
    private $email;

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }
}

出于本演示的目的,我们假设访问者想要订购音乐曲目以供下载。

否则,这可以是任何东西。在 Sylius 应用程序中,它是一个产品。在某些应用中,它可能不是产品,而是订阅或服务等。

所以对于OrderItem,我们将添加一个字段来捕获访问者将订购的下载。

<?php

namespace AppBundle\Entity;

use Sylius\Component\Order\Model\OrderItem as SyliusOrderItem;

class OrderItem extends SyliusOrderItem
{
    /**
     * @var Download
     */
    private $download;

    /**
     * @return Download
     */
    public function getDownload()
    {
        return $this->download;
    }

    /**
     * @param Download $download
     */
    public function setDownload(Download $download)
    {
        $this->download = $download;
    }
}

现在您有了实体,您可以添加 Doctrine 映射。在这个例子中,我们使用的是 XML(丑陋,但它会验证配置),但它可以是 YAML 或注释。

src/AppBundle/Resources/config/doctrine/Order.orm.xml:

<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="AppBundle\Entity\Order" table="app_order">
    <field name="email" column="email" type="string" nullable="true" />

    <one-to-many field="items" target-entity="Sylius\Component\Order\Model\OrderItemInterface" mapped-by="order" orphan-removal="true">
        <cascade>
            <cascade-all/>
        </cascade>
    </one-to-many>

    <one-to-many field="adjustments" target-entity="Sylius\Component\Order\Model\AdjustmentInterface" mapped-by="order" orphan-removal="true">
        <cascade>
            <cascade-all/>
        </cascade>
    </one-to-many>
</entity>

src/AppBundle/Resources/config/doctrine/OrderItem.orm.xml:

<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
         http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="AppBundle\Entity\OrderItem" table="app_order_item">
        <many-to-one field="download" target-entity="AppBundle\Entity\Download">
            <join-column name="download_id" referenced-column-name="id" nullable="false" />
        </many-to-one>

    </entity>

</doctrine-mapping>

然后我们需要让 Sylius OrderBundle 知道我们的新实体,这样它就不会使用默认实体。

我们还想为我们的Order 实体生成订单号,这是由 Sylius SequenceBundle 完成的。

app/config/config.yml中,添加这个配置:

sylius_sequence:
    generators:
        AppBundle\Entity\Order: sylius.sequence.sequential_number_generator

sylius_order:
    resources:
        order:
            classes:
                model: AppBundle\Entity\Order
        order_item:
            classes:
                model: AppBundle\Entity\OrderItem

然后更新您的数据库架构:

$ php app/console doctrine:schema:update --dump-sql --force

现在要在持久化新实体时实际生成订单号并将其分配给Order,我们需要注册一个侦听器。

app/config/services.yml 或您的捆绑包的 Resources/config/services.yml 中,添加以下配置:

parameters:
  sylius.model.sequence.class: Sylius\Component\Sequence\Model\Sequence

services:
  app.order_number_listener:
    class: Sylius\Bundle\OrderBundle\EventListener\OrderNumberListener
    arguments:
      - "@sylius.sequence.doctrine_number_listener"
    tags:
      - { name: kernel.event_listener, event: app.download_ordered, method: generateOrderNumber }

app.download_ordered 事件的名称在这里很重要,您可以随意命名,但必须在创建新订单时发送。

这是一个创建新订单的示例,我们在其中调度 app.download_ordered 事件。

    use AppBundle\Entity\Download;
    use AppBundle\Entity\OrderItem;
    use AppBundle\Form\OrderType;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration as Framework;
    use AppBundle\Entity\Order;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\EventDispatcher\GenericEvent;
    use Symfony\Component\HttpFoundation\Request;

    const STATE_BEGIN    = 'begin';
    const STATE_COMPLETE = 'complete';

    /**
     * @Framework\Route("/begin-order-for-download/{id}", name="begin_order_for_download")
     */
    public function beginOrderForDownloadAction(Request $request, $id)
    {
        $download = $this->getDoctrine()->getRepository(Download::class)->findOneBy(['id' => $id]);

        $order = new Order();
        $form = $this->createForm(new OrderType(), $order);

        if ('POST' === $request->getMethod()) {
            $order->setState(self::STATE_BEGIN);

            $orderItem = new OrderItem();
            $orderItem->setDownload($download);
            $orderItem->setOrder($order);
            $orderItem->setUnitPrice(59);
            // $orderItem->setImmutable(true); // Need to verify how this affects behavior.

            $this->get('event_dispatcher')->dispatch('app.download_ordered', new GenericEvent($order));

            $form->handleRequest($request);

            $em = $this->getDoctrine()->getManager();

            if ($form->isValid()) {
                $order->setState(self::STATE_COMPLETE);
                $this->addFlash('order.state', self::STATE_COMPLETE);

                $em->persist($order);
                $em->flush();

                return $this->redirectToRoute('complete_order', [
                    'id' => $order->getId(),
                ]);
            }
        }

        return $this->render('AppBundle::begin_order_for_download.html.twig', [
            'form'     => $form->createView(),
            'download' => $download,
        ]);
    }

OrderType 表单看起来像这样,为了演示目的而保持简单。根据您的需要进行编辑。

    <?php

    namespace AppBundle\Form;

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

    class OrderType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function getName()
        {
            return 'order';
        }

        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('email', 'repeated', [
                'type'            => 'email',
                'first_options'   => ['label' => 'Email'],
                'second_options'  => ['label' => 'Repeat Email'],
                'invalid_message' => 'The email fields must match.',
            ]);
            $builder->add('proceed', 'submit', ['attr' => ['class' => 'button']]);
        }
    }

如果表单是有效的,我们的订单将被持久化并且监听器为其分配一个订单号,然后我们将重定向complete_order 路由。您可以在此处执行诸如提供付款详细信息之类的操作,或者在这种情况下,提供下载歌曲的链接。

就是这样。你可以在https://github.com/adamelso/orda看到完整工作示例的代码

【讨论】:

  • 对此我真的很感激。感谢您抽出宝贵时间让我试一试,再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 2015-10-30
  • 2021-04-28
  • 1970-01-01
  • 2015-11-20
  • 2013-02-03
  • 2019-11-30
  • 2017-01-19
相关资源
最近更新 更多