【发布时间】:2015-08-07 12:05:59
【问题描述】:
我正在尝试进行 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 }
【问题讨论】:
-
你能显示 product_details.html.twig 第 37 行吗?
MpShopBundle:Frontend:product_details.html.twig at line 37 -
@Ricardo 我确实展示了它。它的最后一个代码表。行:{% for img in product.gallery.galleryHasMedias|slice(0,3) %} 是第 37 行
-
product.gallery 显然等于 null,您可以在开始 for 循环之前检查它。或者您可以检查为什么 product.gallery != null
-
不,产品库不为空,模板中的图像和所有内容都正确显示。只有当我开始 ajax 调用时,我才会收到此错误
-
你声明这个 url:'favorite',在你的 ajax 中,它对我来说就像一个 route_name,它真的是一个 url 吗?
标签: javascript php jquery ajax symfony