Symfony2 Ajax试图访问不相关的属性


Symfony2 Ajax trying to access unrelevant attributes

我正在尝试进行Ajax调用,从收藏夹中添加/删除产品。我想我做的一切都是正确的,但是我得到了一个500错误响应的消息:Impossible to access an attribute ("gallery") on a null variable in MpShopBundle:Frontend:product_details.html.twig at line 37

现在的问题是,属性gallery甚至不应该被使用,在Ajax的任何地方都没有指定,但是我得到这个错误。

这是Ajax脚本:
$(document).ready(function () {
    $(document).on('click', '.favorite', function (e) {
        $this = $(this);
        $.ajax({
            type: 'POST',
            url: 'favorite',
            dataType: 'JSON',
            data: {id: $this.id},  // this is the products id I imagine?
            success: function (data) {
                if(data.success == false){
                    alert('error')
                }else{
                    $('.btn-toolbar').load(" .btn-toolbar");
                }
            }
        });
    });
});

枝:

         <div class="btn-toolbar">
          <div class="btn-group">
            <span class="btn" onclick="window.location.href='mailto:ail@gmail.com;'"><i class="icon-envelope"></i></span>
              <span class="btn" onclick="printPage()" ><i class="icon-print"></i></span>
            {% if favorite is defined %}
                <a href="javascript:void(0)" class="favorite btn"><i class="icon-star-white"></i></a>
              {% else %}
              <a href="javascript:void(0)" class="favorite btn"><i class="icon-star"></i></a>
              {% endif %}
          </div>
        </div>

和控制器:

public function addFavorites(Request $request) {
        $response = new JsonResponse();
        $securityContext = $this->container->get('security.context');
        if (!$securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            $response->setData(array('success'=>false, 'message'=>'Need to be logged in'));
        } else {
            $requestData = $request->request->all();
            $productid     = $requestData['id'];
            $userId = $this->getUser()->getId();
            $em = $this->getDoctrine()->getManager();
            $product = $em->getRepository('MpShopBundle:Product')->find($productid);
            $favorite = $em->getRepository('MpShopBundle:Favorite')->findOneBy(array(
                'userId'=> $userId,
                'productId' => $productid));
            if($favorite) {
                $em->remove($favorite);
                $em->flush();
            } else {
                $favorite = New Favorite();
                $favorite->setUserId($userId);
                $favorite->setProductId($productid);
                $em->persist($favorite);
                $em->flush();
                $response->setData(array('success'=>true,'message'=>'Added to favorites', 'product' => $product));
            }
        }
        return $response;
    }

所以在我的想法中事情应该是这样工作的:用户单击星号,响应将产品id发送给控制器。控制器检查用户是否已登录,如果已登录,则检查用户是否已收藏该产品,如果已收藏,则删除收藏,如果未创建新收藏。如果一切都成功,只刷新按钮。

现在,我的分支中的属性库是我在模板中迭代的第一个循环,它在按钮之前。既然我只是想刷新按钮,为什么他需要画廊属性?:

            <div class="carousel-inner">
              <div class="item active">
              {% for img in product.gallery.galleryHasMedias|slice(0,3) %}
               <a href="{% path img.media, 'reference'%}" data-lightbox="carousel-images"data-lightbox="carousel-images"> <img width="29%" src="{% path img.media, 'reference'%}" alt=""/></a>
              {% endfor %}
              </div>
              <div class="item">
               {% for img in product.gallery.galleryHasMedias|slice(3,6) %}
               <a href="{% path img.media, 'reference'%}" data-lightbox="carousel-images"> <img width="29%" src="{% path img.media, 'reference'%}" alt=""/></a>
               {% endfor %}
              </div>
            </div>
         <div class="btn-toolbar">
          <div class="btn-group">
            <span class="btn" onclick="window.location.href='mailto:katauskasdomas@gmail.com;'"><i class="icon-envelope"></i></span>
              <span class="btn" onclick="printPage()" ><i class="icon-print"></i></span>
            {% if favorite is defined %}
                <a href="javascript:void(0)" class="favorite btn"><i class="icon-star-white"></i></a>
              {% else %}
              <a href="javascript:void(0)" class="favorite btn"><i class="icon-star"></i></a>
              {% endif %}
          </div>
        </div>
路由:

add_favorites:
    pattern:  /favorite
    defaults: { _controller: MpShopBundle:Homepage:addFavorites }

问题在于这一行:

{% for img in product.gallery.galleryHasMedias|slice(0,3) %}

Twig说:Impossible to access an attribute ("gallery") on a null variable in

所以product变量是null(尝试执行product.gallery时出错)。

从您的控制器代码中,似乎没有通过提供的ID找到您的产品。所以可能有几种情况

  1. ID未被传递
  2. 传递无效ID
  3. 没有此ID的产品。