问题
I'm creating an admin with easy admin bundle, i'm really new to Symfony4.
I've a button "create a category" and when i click on it, I've this error :
Return value of App\Entity\Category::getCreatedAt() must implement interface DateTimeInterface, null returned
Code:
<?php
namespace App\Entity;
use DateInterval;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
use TimestampableTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=8)
*/
private $ref;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="categoryId")
*/
private $products;
/**
* Category constructor.
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getRef(): ?string
{
return $this->ref;
}
/**
* @param string $ref
* @return $this
*/
public function setRef(string $ref): self
{
$this->ref = $ref;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
/**
* @param Product $product
* @return $this
*/
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategoryId($this);
}
return $this;
}
/**
* @param Product $product
* @return $this
*/
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
// set the owning side to null (unless already changed)
if ($product->getCategoryId() === $this) {
$product->setCategoryId(null);
}
}
return $this;
}
/**
* (PHP 5 >=5.5.0)<br/>
* Returns the difference between two DateTime objects
* @link https://secure.php.net/manual/en/datetime.diff.php
* @param DateTimeInterface $datetime2 <p>The date to compare to.</p>
* @param bool $absolute <p>Should the interval be forced to be positive?</p>
* @return DateInterval
* The https://secure.php.net/manual/en/class.dateinterval.php DateInterval} object representing the
* difference between the two dates or <b>FALSE</b> on failure.
*
*/
public function diff($datetime2, $absolute = false)
{
// TODO: Implement diff() method.
}
/**
* (PHP 5 >=5.5.0)<br/>
* Returns date formatted according to given format
* @link https://secure.php.net/manual/en/datetime.format.php
* @param string $format <p>
* Format accepted by {@link https://secure.php.net/manual/en/function.date.php date()}.
* </p>
* @return string
* Returns the formatted date string on success or <b>FALSE</b> on failure.
*
*/
public function format($format)
{
// TODO: Implement format() method.
}
/**
* (PHP 5 >=5.5.0)<br/>
* Returns the timezone offset
* @return int
* Returns the timezone offset in seconds from UTC on success
* or <b>FALSE</b> on failure.
*
*/
public function getOffset()
{
// TODO: Implement getOffset() method.
}
/**
* (PHP 5 >=5.5.0)<br/>
* Gets the Unix timestamp
* @return int
* Returns the Unix timestamp representing the date.
*/
public function getTimestamp()
{
// TODO: Implement getTimestamp() method.
}
/**
* (PHP 5 >=5.5.0)<br/>
* Return time zone relative to given DateTime
* @link https://secure.php.net/manual/en/datetime.gettimezone.php
* @return DateTimeZone
* Returns a {@link https://secure.php.net/manual/en/class.datetimezone.php DateTimeZone} object on success
* or <b>FALSE</b> on failure.
*/
public function getTimezone()
{
// TODO: Implement getTimezone() method.
}
/**
* (PHP 5 >=5.5.0)<br/>
* The __wakeup handler
* @link https://secure.php.net/manual/en/datetime.wakeup.php
* @return void Initializes a DateTime object.
*/
public function __wakeup()
{
// TODO: Implement __wakeup() method.
}
}
My TimestampableTrait
<?php
namespace App\Entity;
/**
* You must add the following comment on all the entities:
* @ORM\HasLifecycleCallbacks()
*/
trait TimestampableTrait
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="createdBy")
* @ORM\JoinColumn(nullable=true)
*/
protected $createdBy;
/**
* @return User|null
*/
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
/**
* @param User|null $createdBy
* @return $this
*/
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
*/
protected $createdAt;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=false)
*/
protected $updatedAt;
/**
* Set the created at value on create
* @ORM\PrePersist()
* @param \DateTimeInterface $createdAt
* @return self
*/
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Set the updated at value on update
* @ORM\PrePersist()
* @ORM\PreUpdate()
* @param \DateTimeInterface $updatedAt
* @return self
*/
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get - Created At
*
* @return \DateTimeInterface
*/
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}
/**
* Get - Updated At
*
* @return \DateTimeInterface|null
*/
public function getUpdatedAt(): \DateTimeInterface
{
return $this->updatedAt;
}
}
I really don't get where is the problem.
来源:https://stackoverflow.com/questions/59427778/symfony-4-must-implement-interface-datetimeinterface-error