更新产品时出现此错误:(更新类别正常)
Catchable Fatal Error:无法转换类DateTime的对象 串入 C:\ XAMPP \ htdocs中\ PhpProject1 \供应商\ symfony的\ symfony中的\ src \的Symfony \分量\翻译\ Translator.php 第188行
完整堆栈跟踪:
Stack Trace
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php at line 188 -
$this->loadCatalogue($locale);
}
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
}
/**
at ErrorHandler ->handle ('4096', 'Object of class DateTime could not be converted to string', 'C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php', '188', array('id' => 'This value should be blank.', 'parameters' => array('{{ value }}' => object(DateTime)), 'domain' => 'validators', 'locale' => 'en'))
at strtr ('This value should be blank.', array('{{ value }}' => object(DateTime)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php at line 188 +
at Translator ->trans ('This value should be blank.', array('{{ value }}' => object(DateTime)), 'validators')
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 93 +
at ExecutionContext ->addViolation ('This value should be blank.', array('{{ value }}' => object(DateTime)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Constraints\BlankValidator.php at line 30 +
at BlankValidator ->validate (object(DateTime), object(Blank))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 276 +
at ExecutionContext ->executeConstraintValidators (object(DateTime), array(object(Blank)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 241 +
at ExecutionContext ->validateValue (object(DateTime), array(object(Blank)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 110 +
at ValidationVisitor ->visit (object(PropertyMetadata), object(DateTime), 'Default', 'data.created_at')
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\MemberMetadata.php at line 47 +
at MemberMetadata ->accept (object(ValidationVisitor), object(DateTime), 'Default', 'data.created_at', null)
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\ClassMetadata.php at line 114 +
at ClassMetadata ->accept (object(ValidationVisitor), object(Product), 'Default', 'data')
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 162 +
at ValidationVisitor ->validate (object(Product), 'Default', 'data', true, false)
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 227 +
at ExecutionContext ->validate (object(Product), 'data', 'Default', true)
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Validator\Constraints\FormValidator.php at line 60 +
at FormValidator ->validate (object(Form), object(Form))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 276 +
at ExecutionContext ->executeConstraintValidators (object(Form), array(object(Form)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 241 +
at ExecutionContext ->validateValue (object(Form), array(object(Form)))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 110 +
at ValidationVisitor ->visit (object(ClassMetadata), object(Form), 'Default', '')
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\ClassMetadata.php at line 107 +
at ClassMetadata ->accept (object(ValidationVisitor), object(Form), 'Default', '')
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 162 +
at ValidationVisitor ->validate (object(Form), 'Default', '', false, false)
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Validator.php at line 90 +
at Validator ->validate (object(Form))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener.php at line 55 +
at ValidationListener ->validateForm (object(FormEvent))
at call_user_func (array(object(ValidationListener), 'validateForm'), object(FormEvent))
in C:\xampp\htdocs\PhpProject1\app\cache\dev\classes.php at line 1655 +
at EventDispatcher ->doDispatch (array(array(object(ValidationListener), 'validateForm')), 'form.post_bind', object(FormEvent))
in C:\xampp\htdocs\PhpProject1\app\cache\dev\classes.php at line 1588 +
at EventDispatcher ->dispatch ('form.post_bind', object(FormEvent))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\ImmutableEventDispatcher.php at line 42 +
at ImmutableEventDispatcher ->dispatch ('form.post_bind', object(FormEvent))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 624 +
at Form ->submit (object(Request))
in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 638 +
at Form ->bind (object(Request))
in C:\xampp\htdocs\PhpProject1\src\Mobile\BusinessBundle\Controller\ProductController.php at line 132 +
at ProductController ->updateAction (object(Request), '7')
at call_user_func_array (array(object(ProductController), 'updateAction'), array(object(Request), '7'))
in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2770 +
at HttpKernel ->handleRaw (object(Request), '1')
in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2744 +
at HttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2874 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2175 +
at Kernel ->handle (object(Request))
in C:\xampp\htdocs\PhpProject1\web\app_dev.php at line 29 +
这是代码: 的 Product.php
<?php
namespace Mobile\BusinessBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Lib\Functions;
/**
* Product
*/
class Product
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $slug;
/**
* @var float
*/
private $price;
/**
* @var float
*/
private $discount;
/**
* @var string
*/
private $description;
/**
* @var string
*/
private $detail;
/**
* @var string
*/
private $sku;
/**
* @var integer
*/
private $views;
/**
* @var boolean
*/
private $is_active;
/**
* @var \DateTime
*/
private $created_at;
/**
* @var \DateTime
*/
private $updated_at;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $categories;
/**
* Constructor
*/
public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return Product
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set price
*
* @param float $price
* @return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Set discount
*
* @param float $discount
* @return Product
*/
public function setDiscount($discount)
{
$this->discount = $discount;
return $this;
}
/**
* Get discount
*
* @return float
*/
public function getDiscount()
{
return $this->discount;
}
/**
* Set description
*
* @param string $description
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set detail
*
* @param string $detail
* @return Product
*/
public function setDetail($detail)
{
$this->detail = $detail;
return $this;
}
/**
* Get detail
*
* @return string
*/
public function getDetail()
{
return $this->detail;
}
/**
* Set sku
*
* @param string $sku
* @return Product
*/
public function setSku($sku)
{
$this->sku = $sku;
return $this;
}
/**
* Get sku
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Set views
*
* @param integer $views
* @return Product
*/
public function setViews($views)
{
$this->views = $views;
return $this;
}
/**
* Get views
*
* @return integer
*/
public function getViews()
{
return $this->views;
}
/**
* Set is_active
*
* @param boolean $isActive
* @return Product
*/
public function setIsActive($isActive)
{
$this->is_active = $isActive;
return $this;
}
/**
* Get is_active
*
* @return boolean
*/
public function getIsActive()
{
return $this->is_active;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return Product
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return Product
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* Add categories
*
* @param \Mobile\BusinessBundle\Entity\Category $categories
* @return Product
*/
public function addCategorie(\Mobile\BusinessBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* @param \Mobile\BusinessBundle\Entity\Category $categories
*/
public function removeCategorie(\Mobile\BusinessBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->created_at = new \DateTime();
$this->updated_at = new \DateTime();
$this->views = 0;
$this->is_active = true;
$this->slug = Functions::url_friendly($this->name);
}
/**
* @ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updated_at = new \DateTime();
}
public function __toString()
{
return $this->name;
}
}
Product.orm.yml
Mobile\BusinessBundle\Entity\Product:
type: entity
repositoryClass: Mobile\BusinessBundle\Entity\ProductRepository
manyToMany:
categories:
targetEntity: Category
inversedBy: products
table: products
fields:
id:
type: bigint
id: true
generator:
strategy: AUTO
name:
type: string
length: 255
slug:
type: string
length: 255
price:
type: decimal
length: null
discount:
type: decimal
length: null
description:
type: text
length: null
detail:
type: text
length: null
sku:
type: string
length: 45
views:
type: bigint
is_active:
type: boolean
created_at:
type: datetime
updated_at:
type: datetime
lifecycleCallbacks:
prePersist: [onPrePersist]
preUpdate: [onPreUpdate]
ProductType.php
<?php
namespace Mobile\BusinessBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('slug')
->add('price')
->add('discount')
->add('description')
->add('detail')
->add('sku')
->add('views')
->add('is_active')
->add('created_at')
->add('updated_at')
->add('categories')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mobile\BusinessBundle\Entity\Product'
));
}
public function getName()
{
return 'mobile_businessbundle_producttype';
}
}
ProductController.php
<?php
namespace Mobile\BusinessBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mobile\BusinessBundle\Entity\Product;
use Mobile\BusinessBundle\Form\ProductType;
/**
* Product controller.
*
*/
class ProductController extends Controller
{
/**
* Lists all Product entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MobileBusinessBundle:Product')->findAll();
return $this->render('MobileBusinessBundle:Product:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Creates a new Product entity.
*
*/
public function createAction(Request $request)
{
$entity = new Product();
$form = $this->createForm(new ProductType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('product_show', array('id' => $entity->getId())));
}
return $this->render('MobileBusinessBundle:Product:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Displays a form to create a new Product entity.
*
*/
public function newAction()
{
$entity = new Product();
$form = $this->createForm(new ProductType(), $entity);
return $this->render('MobileBusinessBundle:Product:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Finds and displays a Product entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MobileBusinessBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('MobileBusinessBundle:Product:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(), ));
}
/**
* Displays a form to edit an existing Product entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MobileBusinessBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$editForm = $this->createForm(new ProductType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('MobileBusinessBundle:Product:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing Product entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MobileBusinessBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new ProductType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('product_edit', array('id' => $id)));
}
return $this->render('MobileBusinessBundle:Product:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Product entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MobileBusinessBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('product'));
}
/**
* Creates a form to delete a Product entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
edit.html.twig
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Product edit</h1>
<form action="{{ path('product_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
<input type="hidden" name="_method" value="PUT" />
{{ form_widget(edit_form) }}
<p>
<button type="submit">Edit</button>
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('product') }}">
Back to the list
</a>
</li>
<li>
<form action="{{ path('product_delete', { 'id': entity.id }) }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
{{ form_widget(delete_form) }}
<button type="submit">Delete</button>
</form>
</li>
</ul>
{% endblock %}
更新 发生错误是因为我使用created_at:Blank为Product Entity创建了文件validation.yml。 移动\ BusinessBundle \实体\产品: 特性: created_at: - 空白:〜
答案 0 :(得分:2)
验证器中的问题。可能是validators.yml
因为我没有看到实体上的验证注释。您尝试使用NotBlank
验证程序验证DateTime,该验证程序试图向您显示错误。将其更改为NotNull
验证程序。
'C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php', '188', array('id' => 'This value should be blank.', 'parameters' => array('{{ value }}' => object(DateTime)), 'domain' => 'validators', 'locale' => 'en'))
at strtr ('This value should be blank.', array('{{ value }}' => object(DateTime)))