У меня есть некоторые проблемы с доступом к данным таблицы.
Id_PostId_Post_ParentPost_ContentТак что, если у поста есть Id_Post_Parent, это пост «комментарий» Id_Post_Parent.
Есть дамп комментария dump(posts[1])https://puu.sh/CIEYd/9771b88b3b.png
А для своего дисплея хотелось бы восстановить "ID_Post: 14", только когда сделаю dump (posts [1] ['idPostParent'] ['Id_Post'])
У меня есть эта ошибка:
"Невозможно получить доступ к ключу "Id_Post" на объекте класса "Proxies\__CG__\App\Entity\Post", который не реализует интерфейс ArrayAccess."
Может ли кто-нибудь помочь мне понять и решить эту проблему? :Икс
Есть моя почтовая сущность:
<?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()
);
}
}




Сущности - это объекты, а не массивы.
Вы можете получить доступ к свойствам, используя $myEntity->getPropertyName();
В вашем случае, чтобы получить идентификатор родителя сообщения, вы можете сделать:
dump($posts[1]->getIdPostParent()->getIdPost());