Symfony2.8 - 缺少标识符 id
Symfony2.8 - The identifier id is missing
我正在开发一个基于 symfony2.8 的网站,允许用户购买产品。我在处理购物车时一直遇到问题。这是返回的错误:
The identifier id is missing for a query of MainBundle\Entity\Commande
MainBundle\Entity\Commande:
<?php
namespace MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Commande
*
* @ORM\Table(name="commande")
* @ORM\Entity(repositoryClass="MainBundle\Repository\CommandeRepository")
*/
class Commande
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var bool
*
* @ORM\Column(name="valider", type="boolean")
*/
private $valider;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="commandes")
*/
private $user;
/**
* @var int
*
* @ORM\Column(name="reference", type="integer", nullable=true)
*/
private $reference;
/**
* @var array
*
* @ORM\Column(name="commande", type="array", nullable=true)
*/
private $commande;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set valider
*
* @param boolean $valider
*
* @return Commande
*/
public function setValider($valider)
{
$this->valider = $valider;
return $this;
}
/**
* Get valider
*
* @return bool
*/
public function getValider()
{
return $this->valider;
}
/**
* Set date
*
* @param \DateTime $date
*
* @return Commande
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set reference
*
* @param integer $reference
*
* @return Commande
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* @return int
*/
public function getReference()
{
return $this->reference;
}
/**
* Set products
*
* @param array $products
*
* @return Commande
*/
public function setProducts($products)
{
$this->products = $products;
return $this;
}
/**
* Get products
*
* @return array
*/
public function getProducts()
{
return $this->products;
}
/**
* Set user
*
* @param \UserBundle\Entity\User $user
*
* @return Commande
*/
public function setUser(\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set commande
*
* @param array $commande
*
* @return Commande
*/
public function setCommande($commande)
{
$this->commande = $commande;
return $this;
}
/**
* Get commande
*
* @return array
*/
public function getCommande()
{
return $this->commande;
}
}
这里有 CartController class,只有我的 MainBundle\CartController 中的相关方法:
<?php
/**
* Created by PhpStorm.
* User: mac
* Date: 08/05/18
* Time: 21:32
*/
namespace MainBundle\Controller;
use MainBundle\Entity\Commande;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use UserBundle\Entity\Address;
use UserBundle\Form\Type\AddressType;
class CartController extends Controller
{
public function setShippingOnSession()
{
$session = $this->getRequest()->getSession();
if (!$session->has('address')) $session->set('address', array());
$address = $session->get('address');
if ($this->getRequest()->request->get('shipping') != null) {
$address['shipping'] = $this->getRequest()->request->get('shipping');
if ($this->getRequest()->request->get('billing') != null) {
$address['billing'] = $this->get('request')->request->get('billing');
}
} else {
return $this->redirect($this->generateUrl('validation'));
}
$session->set('address', $address);
return $this->redirect($this->generateUrl('validation'));
}
public function prepareCommandeAction()
{
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getManager();
if (!$session->has('commande')) {
$commande = new Commande();
dump($commande);die();
$commande->setDate(new \DateTime());
$commande->setUser($this->container->get('security.context')->getToken()->getUser());
$commande->setValider(0);
$commande->setReference(0);
$commande->setCommande($this->facture());
$em->persist($commande);
$session->set('commande', $commande->getId());
} else {
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
}
$em->flush();
return $commande->getId();
}
/**
*
* @Route("/validation/{id}", defaults={"id":0}, name="validation")
*/
public function validationAction()
{
$session = $this->get('request')->getSession();
$em = $this->getDoctrine()->getManager();
if ($this->get('request')->getMethod() == "POST")
$this->setShippingOnSession();
$prepareCommande = $this->prepareCommandeAction();
//$prepareCommande = $this->forward('MainBundle:Commandes:prepareCommande');
$commande = $em->getRepository('MainBundle:Commande')->find($prepareCommande);
/*$address = $session->get('address');
$products = $em->getRepository('MainBundle:Product')->findArray(array_keys($session->get('cart')));
$shipping = $em->getRepository('UserBundle:Address')->find($address['shipping']);
if ($address['billing'])
$billing = $em->getRepository('UserBundle:Address')->find($address['billing']);
else
$billing = null;*/
//dump($commande);die();
return $this->render('MainBundle:Cart:validation.html.twig', array(
'commande' => $commande
));
}
public function facture()
{
$em = $this->getDoctrine()->getManager();
$session = $this->getRequest()->getSession();
$generator = $this->container->get('security.secure_random');
$address = $session->get('address');
$cart = $session->get('cart');
$commande = array();
$totalHT = 0;
$totalTTC = 0;
$billing = $em->getRepository('UserBundle:Address')->find($address['billing']);
$shipping = $em->getRepository('UserBundle:Address')->find($address['shipping']);
$products = $em->getRepository('MainBundle:Product')->findArray(array_keys($session->get('cart')));
foreach ($products as $product) {
$prixHT = $product->getPrice() * $cart[$product->getId()];
$prixTTC = $product->getPrice() * $cart[$product->getId()] * (1 + $product->getTva()->getValeur()/100);
$totalHT += $prixHT;
$totalTTC += $prixTTC;
if (!isset($commande['tva']['%'.$product->getTva()->getValeur()])) {
$commande['tva']['%'.$product->getTva()->getValeur()] = round($prixTTC - $prixHT, 2);
} else {
$commande['tva']['%'.$product->getTva()->getValeur()] += round($prixTTC - $prixHT, 2);
}
$commande['products'][$product->getId()] = array(
'title' => $product->getTitle(),
'quantity' => $cart[$product->getId()],
'prixHT' => round($product->getPrice(), 2),
'prixTTC' => round($product->getPrice() * (1 + $product->getTva()->getValeur()/100) , 2)
);
}
$commande['billing'] = array(
"firstname" => $billing->getFirstname(),
"lastname" => $billing->getLastname(),
"streetnumber" => $billing->getStreetnumber(),
"streetextension" => $billing->getStreetextension(),
"streettype" => $billing->getStreettype(),
"streetname" => $billing->getStreetname(),
"zipcode" => $billing->getZipcode(),
"city" => $billing->getCity(),
"country" => $billing->getCountry()
);
$commande['shipping'] = array(
"firstname" => $shipping->getFirstname(),
"lastname" => $shipping->getLastname(),
"streetnumber" => $shipping->getStreetnumber(),
"streetextension" => $shipping->getStreetextension(),
"streettype" => $shipping->getStreettype(),
"streetname" => $shipping->getStreetname(),
"zipcode" => $shipping->getZipcode(),
"city" => $shipping->getCity(),
"country" => $shipping->getCountry()
);
$commande['prixHT'] = round($totalHT, 2);
$commande['prixTTC'] = round($totalTTC, 2);
$commande['token'] = bin2hex($generator->nextBytes(20));
return $commande;
}
}
这是调用 validationAction() 方法的分支代码:
<form action="{{ path('validation') }}" method="POST">
<h4>Adresse de livraison</h4>
{% for address in currentUser.addresses %}
<label class="radio">
<input type="radio" name="shipping" value="{{ address.id }}" {% if loop.index0 == 0 %} checked="checked"{% endif %} />
{{ address.streetnumber }} {% if address.streetextension is defined and address.streetextension is not null %}{{ address.streetextension }}{% endif %} {{ address.streettype }} {{ address.streetname }}, {{ address.zipcode }} {{ address.city }} - {{ address.country }}<a href="{{ path('deleteAddress', {'id':address.id}) }}"><i class="glyphicon glyphicon-trash"></i></a>
</label>
{% endfor %}
<br><br>
<h4>Adresse de facturation</h4>
{% for address in currentUser.addresses %}
<label class="radio">
<input type="radio" name="billing" value="{{ address.id }}" {% if loop.index0 == 0 %} checked="checked"{% endif %} />
{{ address.streetnumber }} {% if address.streetextension is defined and address.streetextension is not null %}{{ address.streetextension }}{% endif %} {{ address.streettype }} {{ address.streetname }}, {{ address.zipcode }} {{ address.city }} - {{ address.country }}<a href="{{ path('deleteAddress', {'id':address.id}) }}"><i class="glyphicon glyphicon-trash"></i></a>
</label>
{% endfor %}
<button class="btn btn-primary">Valider mes adresses</button>
</form>
堆栈跟踪信息:
堆栈跟踪
in vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php at line 294 -
*/
public static function missingIdentifierField($className, $fieldName)
{
return new self("The identifier $fieldName is missing for a query of " . $className);
}
/**
at ORMException ::missingIdentifierField ('MainBundle\Entity\Commande', 'id')
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 403 +
at EntityManager ->find ('MainBundle\Entity\Commande', null, null, null)
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php at line 154 +
at EntityRepository ->find (null)
in src/MainBundle/Controller/CartController.php at line 182 +
at CartController ->prepareCommandeAction ()
in src/MainBundle/Controller/CartController.php at line 206 +
at CartController ->validationAction ()
at call_user_func_array (array(object(CartController), 'validationAction'), array())
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 135 +
at HttpKernel ->handleRaw (object(Request), '1')
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 57 +
at HttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php at line 67 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php at line 183 +
at Kernel ->handle (object(Request))
in web/app_dev.php at line 30 +
Logs - 1 error
INFO - The "security.context" service is deprecated since Symfony 2.6 and will be removed in 3.0.
INFO - The Symfony\Component\Security\Core\SecurityContext class is deprecated since Symfony 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead.
INFO - Passing a string as the $source argument of Twig_Environment::tokenize() is deprecated since version 1.27. Pass a Twig_Source instance instead.
INFO - The Twig_Filter_Method class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.
INFO - The Twig_Filter class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_month" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_day" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "get_schedule" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "phone_number" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "pdfcase" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "substr" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "_method" requirement of route "fos_user_security_check" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "_method" requirement of route "fos_user_resetting_send_email" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - Matched route "validation".
INFO - The "form.csrf_provider" service is deprecated since Symfony 2.4 and will be removed in 3.0. Use the "security.csrf.token_manager" service instead.
INFO - The Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter class is deprecated since Symfony 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.
DEBUG - Read existing security token from the session.
DEBUG - SELECT t0.username AS username_1, t0.username_canonical AS username_canonical_2, t0.email AS email_3, t0.email_canonical AS email_canonical_4, t0.enabled AS enabled_5, t0.salt AS salt_6, t0.password AS password_7, t0.last_login AS last_login_8, t0.confirmation_token AS confirmation_token_9, t0.password_requested_at AS password_requested_at_10, t0.roles AS roles_11, t0.id AS id_12, t0.maidenname AS maidenname_13, t0.gender AS gender_14, t0.firstname AS firstname_15, t0.lastname AS lastname_16, t0.birthday AS birthday_17, t0.birthcity AS birthcity_18, t0.birthzipcode AS birthzipcode_19, t0.birthcountry AS birthcountry_20, t0.nationality AS nationality_21, t0.phone AS phone_22, t0.passwordUncrypted AS passwordUncrypted_23, t0.debug AS debug_24 FROM user t0 WHERE t0.id = ? LIMIT 1
DEBUG - User was reloaded from a user provider.
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
CRITICAL - Uncaught PHP Exception Doctrine\ORM\ORMException: "The identifier id is missing for a query of MainBundle\Entity\Commande" at /Applications/MAMP/htdocs/bambou-afrique/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 294
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
有什么建议吗?
find()
方法¹ 将 id
作为参数,因此以下代码看起来是错误的:
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
您必须检查 $session->get('commande')
返回的是一个整数,并且它对应于 Article
的 id
之一。
如果您的代码,commande
是一个实体,同时该实体的 属性 是一个数组。您可能应该重命名对象和变量以避免混淆。
的快捷方式
我发现,在 CartController::prepareCommandeAction() 中,我没有把 $em->flush()
放在正确的位置。这就是 prepareCommandeAction() 总是返回 null 的原因,因为 $commande->getId() 等于 null。以下是我应该做的:
public function prepareCommandeAction()
{
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getManager();
if (!$session->has('commande')) {
$commande = new Commande();
dump($commande);die();
$commande->setDate(new \DateTime());
$commande->setUser($this->container->get('security.context')->getToken()->getUser());
$commande->setValider(0);
$commande->setReference(0);
$commande->setCommande($this->facture());
$em->persist($commande);
$em->flush();
$session->set('commande', $commande->getId());
} else {
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
}
return $commande->getId();
}
我正在开发一个基于 symfony2.8 的网站,允许用户购买产品。我在处理购物车时一直遇到问题。这是返回的错误:
The identifier id is missing for a query of MainBundle\Entity\Commande
MainBundle\Entity\Commande:
<?php
namespace MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Commande
*
* @ORM\Table(name="commande")
* @ORM\Entity(repositoryClass="MainBundle\Repository\CommandeRepository")
*/
class Commande
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var bool
*
* @ORM\Column(name="valider", type="boolean")
*/
private $valider;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="commandes")
*/
private $user;
/**
* @var int
*
* @ORM\Column(name="reference", type="integer", nullable=true)
*/
private $reference;
/**
* @var array
*
* @ORM\Column(name="commande", type="array", nullable=true)
*/
private $commande;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set valider
*
* @param boolean $valider
*
* @return Commande
*/
public function setValider($valider)
{
$this->valider = $valider;
return $this;
}
/**
* Get valider
*
* @return bool
*/
public function getValider()
{
return $this->valider;
}
/**
* Set date
*
* @param \DateTime $date
*
* @return Commande
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set reference
*
* @param integer $reference
*
* @return Commande
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* @return int
*/
public function getReference()
{
return $this->reference;
}
/**
* Set products
*
* @param array $products
*
* @return Commande
*/
public function setProducts($products)
{
$this->products = $products;
return $this;
}
/**
* Get products
*
* @return array
*/
public function getProducts()
{
return $this->products;
}
/**
* Set user
*
* @param \UserBundle\Entity\User $user
*
* @return Commande
*/
public function setUser(\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set commande
*
* @param array $commande
*
* @return Commande
*/
public function setCommande($commande)
{
$this->commande = $commande;
return $this;
}
/**
* Get commande
*
* @return array
*/
public function getCommande()
{
return $this->commande;
}
}
这里有 CartController class,只有我的 MainBundle\CartController 中的相关方法:
<?php
/**
* Created by PhpStorm.
* User: mac
* Date: 08/05/18
* Time: 21:32
*/
namespace MainBundle\Controller;
use MainBundle\Entity\Commande;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use UserBundle\Entity\Address;
use UserBundle\Form\Type\AddressType;
class CartController extends Controller
{
public function setShippingOnSession()
{
$session = $this->getRequest()->getSession();
if (!$session->has('address')) $session->set('address', array());
$address = $session->get('address');
if ($this->getRequest()->request->get('shipping') != null) {
$address['shipping'] = $this->getRequest()->request->get('shipping');
if ($this->getRequest()->request->get('billing') != null) {
$address['billing'] = $this->get('request')->request->get('billing');
}
} else {
return $this->redirect($this->generateUrl('validation'));
}
$session->set('address', $address);
return $this->redirect($this->generateUrl('validation'));
}
public function prepareCommandeAction()
{
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getManager();
if (!$session->has('commande')) {
$commande = new Commande();
dump($commande);die();
$commande->setDate(new \DateTime());
$commande->setUser($this->container->get('security.context')->getToken()->getUser());
$commande->setValider(0);
$commande->setReference(0);
$commande->setCommande($this->facture());
$em->persist($commande);
$session->set('commande', $commande->getId());
} else {
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
}
$em->flush();
return $commande->getId();
}
/**
*
* @Route("/validation/{id}", defaults={"id":0}, name="validation")
*/
public function validationAction()
{
$session = $this->get('request')->getSession();
$em = $this->getDoctrine()->getManager();
if ($this->get('request')->getMethod() == "POST")
$this->setShippingOnSession();
$prepareCommande = $this->prepareCommandeAction();
//$prepareCommande = $this->forward('MainBundle:Commandes:prepareCommande');
$commande = $em->getRepository('MainBundle:Commande')->find($prepareCommande);
/*$address = $session->get('address');
$products = $em->getRepository('MainBundle:Product')->findArray(array_keys($session->get('cart')));
$shipping = $em->getRepository('UserBundle:Address')->find($address['shipping']);
if ($address['billing'])
$billing = $em->getRepository('UserBundle:Address')->find($address['billing']);
else
$billing = null;*/
//dump($commande);die();
return $this->render('MainBundle:Cart:validation.html.twig', array(
'commande' => $commande
));
}
public function facture()
{
$em = $this->getDoctrine()->getManager();
$session = $this->getRequest()->getSession();
$generator = $this->container->get('security.secure_random');
$address = $session->get('address');
$cart = $session->get('cart');
$commande = array();
$totalHT = 0;
$totalTTC = 0;
$billing = $em->getRepository('UserBundle:Address')->find($address['billing']);
$shipping = $em->getRepository('UserBundle:Address')->find($address['shipping']);
$products = $em->getRepository('MainBundle:Product')->findArray(array_keys($session->get('cart')));
foreach ($products as $product) {
$prixHT = $product->getPrice() * $cart[$product->getId()];
$prixTTC = $product->getPrice() * $cart[$product->getId()] * (1 + $product->getTva()->getValeur()/100);
$totalHT += $prixHT;
$totalTTC += $prixTTC;
if (!isset($commande['tva']['%'.$product->getTva()->getValeur()])) {
$commande['tva']['%'.$product->getTva()->getValeur()] = round($prixTTC - $prixHT, 2);
} else {
$commande['tva']['%'.$product->getTva()->getValeur()] += round($prixTTC - $prixHT, 2);
}
$commande['products'][$product->getId()] = array(
'title' => $product->getTitle(),
'quantity' => $cart[$product->getId()],
'prixHT' => round($product->getPrice(), 2),
'prixTTC' => round($product->getPrice() * (1 + $product->getTva()->getValeur()/100) , 2)
);
}
$commande['billing'] = array(
"firstname" => $billing->getFirstname(),
"lastname" => $billing->getLastname(),
"streetnumber" => $billing->getStreetnumber(),
"streetextension" => $billing->getStreetextension(),
"streettype" => $billing->getStreettype(),
"streetname" => $billing->getStreetname(),
"zipcode" => $billing->getZipcode(),
"city" => $billing->getCity(),
"country" => $billing->getCountry()
);
$commande['shipping'] = array(
"firstname" => $shipping->getFirstname(),
"lastname" => $shipping->getLastname(),
"streetnumber" => $shipping->getStreetnumber(),
"streetextension" => $shipping->getStreetextension(),
"streettype" => $shipping->getStreettype(),
"streetname" => $shipping->getStreetname(),
"zipcode" => $shipping->getZipcode(),
"city" => $shipping->getCity(),
"country" => $shipping->getCountry()
);
$commande['prixHT'] = round($totalHT, 2);
$commande['prixTTC'] = round($totalTTC, 2);
$commande['token'] = bin2hex($generator->nextBytes(20));
return $commande;
}
}
这是调用 validationAction() 方法的分支代码:
<form action="{{ path('validation') }}" method="POST">
<h4>Adresse de livraison</h4>
{% for address in currentUser.addresses %}
<label class="radio">
<input type="radio" name="shipping" value="{{ address.id }}" {% if loop.index0 == 0 %} checked="checked"{% endif %} />
{{ address.streetnumber }} {% if address.streetextension is defined and address.streetextension is not null %}{{ address.streetextension }}{% endif %} {{ address.streettype }} {{ address.streetname }}, {{ address.zipcode }} {{ address.city }} - {{ address.country }}<a href="{{ path('deleteAddress', {'id':address.id}) }}"><i class="glyphicon glyphicon-trash"></i></a>
</label>
{% endfor %}
<br><br>
<h4>Adresse de facturation</h4>
{% for address in currentUser.addresses %}
<label class="radio">
<input type="radio" name="billing" value="{{ address.id }}" {% if loop.index0 == 0 %} checked="checked"{% endif %} />
{{ address.streetnumber }} {% if address.streetextension is defined and address.streetextension is not null %}{{ address.streetextension }}{% endif %} {{ address.streettype }} {{ address.streetname }}, {{ address.zipcode }} {{ address.city }} - {{ address.country }}<a href="{{ path('deleteAddress', {'id':address.id}) }}"><i class="glyphicon glyphicon-trash"></i></a>
</label>
{% endfor %}
<button class="btn btn-primary">Valider mes adresses</button>
</form>
堆栈跟踪信息:
堆栈跟踪
in vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php at line 294 -
*/
public static function missingIdentifierField($className, $fieldName)
{
return new self("The identifier $fieldName is missing for a query of " . $className);
}
/**
at ORMException ::missingIdentifierField ('MainBundle\Entity\Commande', 'id')
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 403 +
at EntityManager ->find ('MainBundle\Entity\Commande', null, null, null)
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php at line 154 +
at EntityRepository ->find (null)
in src/MainBundle/Controller/CartController.php at line 182 +
at CartController ->prepareCommandeAction ()
in src/MainBundle/Controller/CartController.php at line 206 +
at CartController ->validationAction ()
at call_user_func_array (array(object(CartController), 'validationAction'), array())
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 135 +
at HttpKernel ->handleRaw (object(Request), '1')
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 57 +
at HttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php at line 67 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php at line 183 +
at Kernel ->handle (object(Request))
in web/app_dev.php at line 30 +
Logs - 1 error
INFO - The "security.context" service is deprecated since Symfony 2.6 and will be removed in 3.0.
INFO - The Symfony\Component\Security\Core\SecurityContext class is deprecated since Symfony 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead.
INFO - Passing a string as the $source argument of Twig_Environment::tokenize() is deprecated since version 1.27. Pass a Twig_Source instance instead.
INFO - The Twig_Filter_Method class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.
INFO - The Twig_Filter class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_month" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_day" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "get_schedule" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "phone_number" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "pdfcase" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - Using an instance of "Twig_Filter_Method" for filter "substr" is deprecated since version 1.21. Use Twig_SimpleFilter instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "_method" requirement of route "fos_user_security_check" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "_method" requirement of route "fos_user_resetting_send_email" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.
INFO - Matched route "validation".
INFO - The "form.csrf_provider" service is deprecated since Symfony 2.4 and will be removed in 3.0. Use the "security.csrf.token_manager" service instead.
INFO - The Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter class is deprecated since Symfony 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.
DEBUG - Read existing security token from the session.
DEBUG - SELECT t0.username AS username_1, t0.username_canonical AS username_canonical_2, t0.email AS email_3, t0.email_canonical AS email_canonical_4, t0.enabled AS enabled_5, t0.salt AS salt_6, t0.password AS password_7, t0.last_login AS last_login_8, t0.confirmation_token AS confirmation_token_9, t0.password_requested_at AS password_requested_at_10, t0.roles AS roles_11, t0.id AS id_12, t0.maidenname AS maidenname_13, t0.gender AS gender_14, t0.firstname AS firstname_15, t0.lastname AS lastname_16, t0.birthday AS birthday_17, t0.birthcity AS birthcity_18, t0.birthzipcode AS birthzipcode_19, t0.birthcountry AS birthcountry_20, t0.nationality AS nationality_21, t0.phone AS phone_22, t0.passwordUncrypted AS passwordUncrypted_23, t0.debug AS debug_24 FROM user t0 WHERE t0.id = ? LIMIT 1
DEBUG - User was reloaded from a user provider.
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.
CRITICAL - Uncaught PHP Exception Doctrine\ORM\ORMException: "The identifier id is missing for a query of MainBundle\Entity\Commande" at /Applications/MAMP/htdocs/bambou-afrique/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 294
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
有什么建议吗?
find()
方法¹ 将 id
作为参数,因此以下代码看起来是错误的:
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
您必须检查 $session->get('commande')
返回的是一个整数,并且它对应于 Article
的 id
之一。
如果您的代码,commande
是一个实体,同时该实体的 属性 是一个数组。您可能应该重命名对象和变量以避免混淆。
我发现,在 CartController::prepareCommandeAction() 中,我没有把 $em->flush()
放在正确的位置。这就是 prepareCommandeAction() 总是返回 null 的原因,因为 $commande->getId() 等于 null。以下是我应该做的:
public function prepareCommandeAction()
{
$session = $this->getRequest()->getSession();
$em = $this->getDoctrine()->getManager();
if (!$session->has('commande')) {
$commande = new Commande();
dump($commande);die();
$commande->setDate(new \DateTime());
$commande->setUser($this->container->get('security.context')->getToken()->getUser());
$commande->setValider(0);
$commande->setReference(0);
$commande->setCommande($this->facture());
$em->persist($commande);
$em->flush();
$session->set('commande', $commande->getId());
} else {
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
}
return $commande->getId();
}