中
我一直在尝试向用户添加多辆车,但我总是会遇到此错误,我一直在网上搜索答案已有2天了,但我仍然卡住了P.S。:我是Symfony and Doctrine
的新手<?php
namespace CarsBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpFoundationRequest;
use DoctrineORMEntityRepository;
class CarsController extends Controller
{
public function addCarsAction($iduser,$carName,$carPrice,$carVIN)
{
$cars = new cars();
$cars->setPic('')
->setName($carName)
->setModle(Null)
->setPrice($carPrice)
->setVin($carVIN)
->setIdUser($iduser);
//$this->forward('acme.controller.tagclass:ParseWebsite',array($categoryid));
$em1 = $this->forward('app.cars_controller')
->getDoctrine()->getManager()
->getRepository('OBCarsTest1Bundle:cars')
->persist($cars)
->flush();
}
public function addCarsToUserAction()
{
$this->addCarsAction(1,'bmw',45000,'o102384');
//$this->addCarsAction(1,'mercedes',53000,'o224384');
//$this->addCarsAction(1,'nisan',25000,'o686495');
//$this->addCarsAction(2,'golf',44000,'o659826');
//$this->addCarsAction(2,'toyota',23000,'o255325');
//test if it works
$repository = $this->getDoctrine()
->getRepository('OBCarsTest1Bundle:cars');
$cars = $repository->findOneById(3);
return new Response('Created cars : n '.$cars);
}
}
您对它的思考只是使用: $em1 = $this->getDoctrine()->getManager()
代替
$em1 = $this->forward('app.cars_controller')->getDoctrine()->getManager()
并且为了持久性,假设您有一个propper orm映射您只需要添加: $em1->persist($cars); $em1->flush();
,它将其保存在数据库