因此,我正在努力迈出第一步,使客户实体像这样
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Customer
*
* @ORM\Table(name="customer")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CustomerRepository")
*/
class Customer
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="text")
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="address", type="text")
*/
private $address;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="customer")
*/
private $user;
/**
* @ORM\OneToOne(targetEntity="Cart", mappedBy="user", cascade={"persist"})
*/
protected $cart;
// public function __construct()
// {
// parent::__construct();
// // your own logic
// }
// other properties and methods
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Customer
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* @param string $address
*
* @return Customer
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set cart
*
* @param \AppBundle\Entity\Cart $cart
* @return User
*/
public function setCart(\AppBundle\Entity\Cart $cart = null)
{
$this->cart = $cart;
return $this;
}
/**
* Get cart
*
* @return \AppBundle\Entity\Cart
*/
public function getCart()
{
return $this->cart;
}
// public function __construct()
// {
// parent::__construct();
// $this->addRole("ROLE_CUSTOMER");
// }
}
当我访问本地主机/客户时,我收到了这样的错误消息
执行'SELECT t0.id AS id_1,t0.name时发生异常 AS名称_2,t0.address AS地址_3,t0.user_id AS user_id_4,t5.id AS id_6,t5.total_price AS total_price_7,t5.quantity AS数量_8, t5.user_id AS user_id_9来自客户t0左加入购物车t5开 t5.user_id = t10.id':
SQLSTATE [42P01]:未定义表:7错误:缺少FROM子句条目 表“ t10”的第1行:..._ 9来自客户t0左联接购物车t5开启 t5.user_id = t10.id ^
我确实在stackoverflow中打开了一些线程,但是其中许多仍然使我困惑,无法找到解决此问题的明确答案,该主题主要讨论的是postgres而不是实体。
这是我的客户经理
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\User;
use AppBundle\Entity\Customer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class CustomerController extends Controller
{
/**
* @Route("customer/new", name="new_address")
*/
public function newAction(Request $request)
{
$user = new Customer();
$form = $this->createFormBuilder($user)
->add('name', TextType::class)
->add('address', TextType::class)
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$doct = $this->getDoctrine()->getManager();
$doct->persist($user);
$doct->flush();
return $this->redirectToRoute('address_list');
} else {
return $this->render('customer/new.html.twig', array(
'form' => $form->createView(),
));
}
}
/**
* @Route("/customer", name="address_list")
*/
public function displayAction()
{
$user = $this->getDoctrine()
->getRepository('AppBundle:Customer')
->findAll();
return $this->render('customer/display.html.twig', array('data' => $user));
}
/**
* @Route("/customer/update/{id}", name="address_update")
*/
public function updateAction($id, Request $request)
{
$doct = $this->getDoctrine()->getManager();
$user = $doct->getRepository('AppBundle:Customer')->find($id);
if (!$user) {
throw $this->createNotFoundException(
'No customer found for id '.$id
);
}
$form = $this->createFormBuilder($user)
->add('address', TextType::class)
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$doct = $this->getDoctrine()->getManager();
$doct->persist($user);
$doct->flush();
return $this->redirectToRoute('address_display');
}
return $this->render('customer/new.html.twig', array(
'form' => $form->createView(),
));
}
/**
* @Route("customer/delete/{id}", name="customer_delete")
*/
public function deleteAction($id) {
$doct = $this->getDoctrine()->getManager();
$user = $doct->getRepository('AppBundle:Customer')->find($id);
if (!$user) {
throw $this->createNotfoundException('No customer found for id ',$id);
}
$doct->remove($user);
$doct->flush();
return $this->redirectToRoute('address_display');
}
}
我希望输出结果可以再次访问客户显示操作路由器,并显示来自客户控制器的所有数据。
答案 0 :(得分:0)
我不知道为什么这可以解决此问题,所以我尝试取消注释此行
public function __construct()
{
parent::__construct();
$this->addRole("ROLE_CUSTOMER");
}
它解决了我可以打开并访问localhost / customer的问题