我正在做一个用户可以添加联系人的应用程序。目前我还没有用户。当我切换到添加表格的人时,我有错误。谁先前工作。
当我删除或评论detailsAction和indexAction时,表单有效,当我再次恢复2操作时,表单不起作用。
错误:
无法访问空变量的属性(“名称”)。
我的控制器如下:
class MainController extends Controller
{
/**
* @Route("/", name="index")
*
* @return Response
*/
public function indexAction()
{
$entityManager= $this->getDoctrine()->getManager();
$personaldatas = $entityManager->getRepository(Personaldata::class)->findAll();
return $this->render("Main/index.html.twig",["personaldatas" => $personaldatas]);
}
/**
* @Route("/{id}", name="details")
*
* @param $id
*/
public function detailsAction($id)
{
$entityManager= $this->getDoctrine()->getManager();
$personaldata = $entityManager->getRepository(Personaldata::class)->findOneBy(["id"=> $id]);
return $this->render("Main/details.html.twig",["personaldata" => $personaldata]);
}
/**
* @Route("/add", name="add")
*
* @return Response
*/
public function AddAction(Request $request)
{
$personaldata = new Personaldata();
$form = $this->createFormBuilder($personaldata)
->add("name", TextType::class)
->add("surname", TextType::class)
->add("email", TextType::class)
->add("phonenumber",TextType::class)
->add("dateofbirth",DateType::class)
->add("submit",SubmitType::class)
->getForm();
$personaldata
->setCreatedAt(new \DateTime())
->setUpdatedAt(new \DateTime());
if($request->isMethod("post"))
{
$form->handleRequest($request);
$personaldata = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($personaldata);
$entityManager->flush();
return $this->redirectToRoute("index");
}
return $this->render("Main/add.html.twig",["form" => $form->createView()]);
}
我的观点如下:
{% extends "base.html.twig" %}
{% block body %}
{{ personaldata.name }}
{{ personaldata.surname }}
{{ personaldata.dateofbirth}}
{{ personaldata.phonenumber}}
{{ personaldata.email }}
{% endblock %}
任何人都知道错误是什么?
错误:
Impossible to access an attribute ("name") on a null variable
Twig_Error_Runtime
in app\Resources\views\Main\details.html.twig (line 5)
{% extends "base.html.twig" %} {% block body %} {{ personaldata.name }}
{{ personaldata.surname }} {{ personaldata.dateofbirth}} {{
personaldata.phonenumber}} {{ personaldata.email }}
答案 0 :(得分:0)
您需要检查是否有定义的数据以避免此类错误。
在PHP方面,确保存储库中存在实际检索到的数据package simplearray;
import java.util.*;
public class SimpleArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();
String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];
int[] waitingTime = new int[numberOfProcesses];
for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();
System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();
System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}
System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");
int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {
waitingTime[k] = waitingTime[k] + CPUburst[k];
temp = waitingTime[k];
System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k] + " | " + waitingTime[k] + " | ");
}
}
并从您的twig文件中,您可以使用defined函数
public function detailsAction($id)
{
$entityManager= $this->getDoctrine()->getManager();
$personaldata = $entityManager->getRepository(Personaldata::class)->findOneBy(["id"=> $id]);
if (count($personaldata) == 0) {
$personaldata = null;
}
return $this->render("Main/details.html.twig",["personaldata" => $personaldata]);
}