【问题标题】:How to stop array from being overwritten?如何阻止数组被覆盖?
【发布时间】:2016-02-05 18:26:03
【问题描述】:

我正在使用 Symfony2,版本 2.7。但是任何人都应该能够回答这个问题,因为它与 symfony 并不完全相关。

我有一个功能。我希望该函数将新项目(一旦从表单中单击)添加到数组中。相反,我的数组不断被单击的新项目覆盖。

我尝试了几个 foreach 循环,但无法正确完成。请,任何帮助表示赞赏。

以下是相关功能。

  /**
     * Displays Bought Items
     *
     * @Route("/buy/{id}", name="item_buy")
     * @Method("GET")
     * @Template()
     */
    public function buyAction(Request $request, $id)
    {
        $session = $request->getSession();

        $cart[] = $id;

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

        $entity = $em->getRepository('AppBundle:Item')->find($id);

        $entities = $em->getRepository('AppBundle:Item')->findAll();

            $session->set('cart', $cart);

            if (!$entity) {
                throw $this->createNotFoundException('No Item ');
            } else {
                $cart[$entity->getId()] = $entity->getName();
            }
       $session->set('cart', $cart);

    return array(
        'cart'     => $cart,
        'entity'   => $entity,
        'entities' => $entities,
    );
}

它是如何在 twig 中使用的:

{% extends '::base.html.twig' %}

{% block body -%}
    <h1>Items Bought...</h1>

    <table class="record_properties">
        <h3>You Bought...</h3>
        {# {% if entity is defined %} #}
            {% for key, cartValue in cart %}
                <tr>
                    <td>{{ key }}: {{ cartValue }}</td>
                    {{ dump(entity.name) }}
                    {{ dump(cart) }}
                </tr>
            {% endfor %}
        {# {% endif %} #}


        <tbody>
        {% for entity in entities %}
            <tr>
                <td><a href="{{ path('item_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
                <td>{{ entity.name }}</td>
                <td>
                <ul>
                    <li>
                        <a href="{{ path('item_buy', { 'id': entity.id }) }}">Buy</a>
                    </li>
                </ul>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>


     <ul class="record_actions">
    <li>
        <a href="{{ path('item') }}">
            Back to the list
        </a>
    </li>
{% endblock %}

【问题讨论】:

    标签: php arrays symfony


    【解决方案1】:

    也许我错了,但我猜这行有问题:

    $cart[] = $id;
    

    您每次都在这里初始化新数组。如果我是 wright 你应该从session 得到这个数组。

    试试

    $cart = $this-&gt;get('session')-&gt;get('cart', []);

    【讨论】:

    • 顺便说一句,将$id 添加到数组$cart[] = $id; 的原因是什么?这行$cart[$entity-&gt;getId()] = $entity-&gt;getName(); 应该够用了。
    • 这只是为了演示分配给什么 id,我会将您的代码片段放在哪里?
    • 尝试替换$cart[] = $id; => $cart = $this-&gt;get('session')-&gt;get('cart', []);
    • 确实存储了新值,但现在它们不会在开始一个全新的会话后消失
    • 很遗憾,我不明白你的答案:/ 我现在快速测试一下 - 我的控制器操作:$cart = $this-&gt;get('session')-&gt;get('cart', []); $cart[] = time(); $session-&gt;set('cart', $cart); return $this-&gt;render('BleMainBundle:Default:index.html.twig', ['cart' =&gt; $cart]); 和模板:{{ dump(cart) }} 每次刷新都会向数组添加新元素。
    【解决方案2】:

    为了获得更好的代码格式,我将在此处回复您的最后一条评论。

    上面你正在写关于添加新元素的文章,我帮助你解决了这个问题。现在(如果我理解正确的话)您在将元素添加到您在会话中保存的数组时遇到问题。我很惊讶你的惊喜。如果你现在想从你在会话中保存的数组中删除一些东西,你必须实现它。清理购物车并不难 你应该这样写:

    public function clearCartAction(Request $request)
    {
       $session->set('cart', array()); //set empty array
    
       //return something here.. 
    }
    

    像这样从购物车中删除单个对象(非常简单的实现):

    public function removeAction(Request $request, $id)
    {
        $session = $request->getSession();
    
        $em = $this->getDoctrine()->getManager();
    
        $cart = $session->get('cart', array());
    
        if (isset($cart[$id]) {
            unset($cart[$id]);
        }
    
        $session->set('cart', $cart);
    
        //return something here
    }
    

    我可以看到你有很多非常基本的问题——你肯定需要大量学习和学习编程。

    顺便说一句。我想我帮助您解决了主题中描述的问题 - 所以您应该将我的评论标记为有帮助,并将主题标记为已解决。

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多