1

我正在使用 symfony2 API 平台从我的 mongodb 数据库生成 api,但我收到此错误: hydra:description: "No resource found for object of type "AppBundle\Document\article"",

我有两个 mongo db 文档用户和文章,每个用户都有多篇文章,所以我试图列出所有用户及其所有文章标题,这样我就可以得到这样的东西:

{
@id: "/user/53edb6200cf2400d584c2617",
@type: "user",
class: "de.freelancer.mongo.domain.User",
email: "feer@de.com",
displayname: "feer",
withnewsletter: false,
language: "de_DE",
active: true,
admin: false,
articles : ['article1','article2','article3']
}

这是我的代码:

用户文档

<?php
namespace AppBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Validator\Constraints as Assert;

/**
@MongoDB\Document
@MongoDB\Document(collection="user")
/
class User{

    /*
    @MongoDB\Id(strategy="AUTO") */ 
    private $id;
    /**

    @MongoDB\ReferenceMany(targetDocument="articles", mappedBy="user") */
    private $articles;

    public function __construct()
    {
        $this->articles = new ArrayCollection();
    }

    /**
    */
    public function getArticles()
    {
        return $this->articles;
    }
}

文章文件

<?php

namespace AppBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Validator\Constraints as Assert;

/**

@MongoDB\Document
@MongoDB\Document(collection="article")
/
class article
{
    /*
    @MongoDB\Id(strategy="AUTO") */ 
    private $id;

    /**
    @MongoDB\ReferenceOne(targetDocument="user", inversedBy="articles") */
     private $user;

    /**
     */ 
    public function setUser(userProfile $user) 
    { 
        $this->user = $user;
        return $this; 
     }

    /**
     */ 
    public function getUser() {
        return $this->user; 
    }
}

谁能帮我获取用户文章列表

谢谢

4

1 回答 1

0

API 平台管理的每个类都需要用@ApiPlatform\Core\Annotation\ApiResource注解(或 YAML 或 XML 中的等价物)进行标记。

而且还没有原生 MongoDB 支持(但这是一项正在进行的工作)。

您仍然可以使用自定义数据提供者自己添加 MongoDB 支持,但如果您是 API 平台的新手,您应该尝试使用 Doctrine ORM 并首先遵循官方教程:https ://api-platform.com/docs /分配/

于 2017-07-17T22:07:07.963 回答