访问表中的数据时遇到一些问题。
Id_Post
Id_Post_Parent
Post_Content
因此,如果帖子具有Id_Post_Parent,则该帖子为“评论”帖子Id_Post_Parent
。
有评论dump(posts[1])
的转储
here
仅在我执行dump (posts [1] ['idPostParent'] ['Id_Post'])
时,我才想恢复“ Id_Post:14 ”
我有这个错误:
“ 无法访问键”在类对象上的“ Id_Post”“代理\ __ CG __ \ App \ Entity \ Post”未实现ArrayAccess接口。”
有人可以帮助我理解和解决此问题吗? :x
有我的邮寄实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $Id_Post;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Post")
* @ORM\JoinColumn(name="Id_Parent_Post", referencedColumnName="id_post", nullable=true)
*/
private $Id_Post_Parent;
/**
* @ORM\Column(type="text")
*/
private $Post_Content;
/**
* @ORM\Column(type="datetime")
*/
private $Post_Date_Time;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="User", referencedColumnName="Id_User")
*/
private $Id_User;
public function getIdPost(): ?int
{
return $this->Id_Post;
}
public function setIdPost(int $Id_Post): self
{
$this->Id_Post = $Id_Post;
return $this;
}
public function getPostContent(): ?string
{
return $this->Post_Content;
}
public function setPostContent(string $Post_Content): self
{
$this->Post_Content = $Post_Content;
return $this;
}
public function getPostDateTime(): ?\DateTimeInterface
{
return $this->Post_Date_Time;
}
/**
* Formatte la date en fonction du jour même (différence entre les deux, exemple : Il y a 3 jours)
* @return string l'intervalle formatée
*/
public function getFormattedPostDateTime(): ?string
{
date_default_timezone_set('Europe/Paris');
$format = 'Y-m-d H:i:s';
$datePost = \DateTime::createFromFormat($format, $this->getPostDateTime()->format($format));
$currentDate = new \DateTime();
$interval = date_diff($datePost, $currentDate);
if ($interval->format('%d') > 0) {
return "Il y a " . $interval->format('%d') . (($interval->format('%d') == 1) ? " jour" : " jours");
} else if ($interval->format('%h') > 0) {
return "Il y a " . $interval->format('%h') . (($interval->format('%h') == 1) ? " heure" : " heures");
} else if ($interval->format('%i') > 0) {
return "Il y a " . $interval->format('%i') . (($interval->format('%i') == 1) ? " minute" : " minutes");
} else {
return "Il y a moins d'une minute";
}
}
public function setPostDateTime(\DateTimeInterface $Post_Date_Time): self
{
$this->Post_Date_Time = $Post_Date_Time;
return $this;
}
public function getIdUser(): ?User
{
return $this->Id_User;
}
public function setIdUser(?User $Id_User): self
{
$this->Id_User = $Id_User;
return $this;
}
public function getIdPostParent(): ?self
{
return $this->Id_Post_Parent;
}
public function setIdPostParent(?self $Id_Post_Parent): self
{
$this->Id_Post_Parent = $Id_Post_Parent;
return $this;
}
public function getArray()
{
return array
(
'idPost' => $this->getIdPost(),
'postContent' => $this->getPostContent(),
'postDateTime' => $this->getPostDateTime(),
'formattedPostDateTime' => $this->getFormattedPostDateTime(),
'idUser' => $this->getIdUser(),
'idPostParent' => $this->getIdPostParent()
);
}
}
答案 0 :(得分:0)
实体是对象,而不是数组。
您可以使用$myEntity->getPropertyName();
对于您的情况,要获取帖子的父代ID,可以执行以下操作:
dump($posts[1]->getIdPostParent()->getIdPost());