Symfony2.8 - 标识符 ID 丢失



我正在开发一个基于symfony2.8的网站,允许用户购买产品。我在尝试处理购物车时一直遇到问题。下面是返回的错误:

查询

MainBundle\Entity\Commande 的标识符 ID 缺失

MainBundle\Entity\Commande:

<?php
namespace MainBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Commande
*
* @ORMTable(name="commande")
* @ORMEntity(repositoryClass="MainBundleRepositoryCommandeRepository")
*/
class Commande
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var bool
*
* @ORMColumn(name="valider", type="boolean")
*/
private $valider;
/**
* @var DateTime
*
* @ORMColumn(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORMManyToOne(targetEntity="UserBundleEntityUser", inversedBy="commandes")
*/
private $user;
/**
* @var int
*
* @ORMColumn(name="reference", type="integer", nullable=true)
*/
private $reference;
/**
* @var array
*
* @ORMColumn(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 UserBundleEntityUser $user
*
* @return Commande
*/
public function setUser(UserBundleEntityUser $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return UserBundleEntityUser
*/
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类,在我的MainBundle\CartController中只有相关方法:

<?php
/**
* Created by PhpStorm.
* User: mac
* Date: 08/05/18
* Time: 21:32
*/
namespace MainBundleController;
use MainBundleEntityCommande;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRedirectResponse;
use UserBundleEntityAddress;
use UserBundleFormTypeAddressType;

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 ('MainBundleEntityCommande', 'id') 
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 403   + 
at EntityManager ->find ('MainBundleEntityCommande', 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 SymfonyComponentSecurityCoreSecurityContext class is deprecated since Symfony 2.6 and will be removed in 3.0. Use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage or SymfonyComponentSecurityCoreAuthorizationAuthorizationChecker 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 SymfonyComponentFormExtensionCsrfCsrfProviderCsrfProviderAdapter class is deprecated since Symfony 2.4 and will be removed in version 3.0. Use the SymfonyComponentSecurityCsrfCsrfTokenManager 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 "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerProfilerListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleFrameworkBundleEventListenerSessionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerFragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundleTranslationTranslationListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerTranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentSecurityHttpFirewall::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundleServicesRedirectionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleAsseticBundleEventListenerRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyBundleFrameworkBundleDataCollectorRouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "EasyCorpBundleEasyAdminBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyComponentHttpKernelDataCollectorRequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerHttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerSecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerTemplateListener::onKernelController". 
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for SymfonyComponentHttpFoundationRequest 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 SymfonyComponentHttpFoundationRequest to your controller parameters to retrieve the request instead. 
INFO - The SymfonyBundleFrameworkBundleControllerController::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 SymfonyBundleFrameworkBundleControllerController::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 SymfonyBundleFrameworkBundleControllerController::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 SymfonyBundleFrameworkBundleControllerController::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 SymfonyComponentHttpFoundationRequest to your controller parameters to retrieve the request instead. 
INFO - The SymfonyBundleFrameworkBundleControllerController::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 DoctrineORMORMException: "The identifier id is missing for a query of MainBundleEntityCommande" at /Applications/MAMP/htdocs/bambou-afrique/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 294 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerProfilerListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleFrameworkBundleEventListenerSessionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerFragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundleTranslationTranslationListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerTranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentSecurityHttpFirewall::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundleServicesRedirectionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleAsseticBundleEventListenerRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyBundleFrameworkBundleDataCollectorRouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "EasyCorpBundleEasyAdminBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyComponentHttpKernelDataCollectorRequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerHttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerSecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerTemplateListener::onKernelController". 

有什么建议吗?

find()方法¹ 将id作为参数,因此以下代码看起来是错误的:

$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));

您必须检查$session->get('commande')是否返回一个整数,并且它对应于Articleid之一。

如果你的代码,commande是一个实体,同时是一个数组的实体的属性。您可能应该重命名对象和变量以避免混淆。

¹ 这是从教义中find方法的捷径

我发现,在CartController::p repareCommandeAction((中,我没有把$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();
}

最新更新