如何访问一个外部表的方法呢? (symfony的)

问题描述 投票:-1回答:1

我有一些问题访问表的数据。

  1. 我有一个表“邮报”其中有: Id_Post Id_Post_Parent Post_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 \ __ CG __ \软件\实体\邮报“没有实现了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()
        );
    }
}
java arrays symfony object authorization
1个回答
0
投票

实体对象,而不是数组。

您可以使用$myEntity->getPropertyName();访问属性

在你的情况下,获得那个职位父ID,你可以这样做:

dump($posts[1]->getIdPostParent()->getIdPost());
© www.soinside.com 2019 - 2024. All rights reserved.