I’ve created an entity with a closure table and a repository alongside. I’m trying a basic example of building a tree but am seeing the error Cannot use object of type AppEntityDiscussionComment as array
when calling getCommentTreeForDiscussion
Entity:
#[GedmoTree(type: 'closure')]
#[GedmoTreeClosure(class: CommentClosure::class)]
#[ORMEntity(repositoryClass: CommentRepository::class)]
#[ORMHasLifecycleCallbacks]
class Comment implements Identifiable
{
use IdentifiableEntity;
use TimestampableEntity;
#[ORMColumn(type: 'text')]
private string $content;
#[ORMManyToOne(targetEntity: User::class)]
#[ORMJoinColumn(nullable: false)]
private User $author;
#[ORMManyToOne(targetEntity: Discussion::class, inversedBy: 'comments')]
#[ORMJoinColumn(nullable: false)]
private Discussion $discussion;
#[GedmoTreeParent]
#[ORMManyToOne(targetEntity: self::class, inversedBy: 'replies')]
#[ORMJoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
private ?Comment $parent = null;
#[ORMOneToMany(targetEntity: self::class, mappedBy: 'parent', cascade: ['persist', 'remove'])]
private Collection $replies;
#[GedmoTreeLevel]
#[ORMColumn(type: 'integer')]
private int $level = 0;
#[ORMManyToMany(targetEntity: User::class)]
#[ORMJoinTable(name: 'comment_likes')]
private Collection $likedBy;
// --- Getters / Setters
Closure:
#[ORMEntity]
class CommentClosure extends AbstractClosure
{
#[ORMManyToOne(targetEntity: Comment::class)]
#[ORMJoinColumn(name: 'ancestor', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
protected $ancestor;
#[ORMManyToOne(targetEntity: Comment::class)]
#[ORMJoinColumn(name: 'descendant', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
protected $descendant;
}
Repository:
class CommentRepository extends ClosureTreeRepository
{
public function __construct(EntityManagerInterface $manager)
{
parent::__construct($manager, $manager->getClassMetadata(Comment::class));
}
public function getCommentTreeForDiscussion(Discussion $discussion): array
{
$roots = $this->findBy(['discussion' => $discussion, 'parent' => null], ['createdAt' => 'ASC']);
return $this->buildTree($roots);
}
}