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

时间:2015-08-07 12:05:59

标签: javascript php jquery ajax symfony

我正在尝试进行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发送给控制器。控制器检查用户是否登录,如果是,则检查用户是否已经收藏此产品,如果是,则删除收藏夹,如果没有创建新收藏夹。如果一切顺利,只刷新按钮。

现在我的树枝中的属性库是我在模板中迭代的第一个循环,它位于按钮之前。由于我只是想刷新按钮,为什么他需要gallery属性?:

            <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 }

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

{% 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的产品。