【问题标题】:API Platform : No resource found for object of type "AppBundle\Document\article"API 平台:找不到类型为“AppBundle\Document\article”的对象的资源
【发布时间】:2016-04-12 22:40:37
【问题描述】:

我正在使用 symfony2 API 平台从我的 mongodb 数据库生成一个 api,但我收到此错误: hydra:description: "找不到类型为“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; 
    }
}

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

谢谢

【问题讨论】:

    标签: mongodb api symfony doctrine-orm api-platform.com


    【解决方案1】:

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

    目前还没有原生 MongoDB 支持(但这是a work in progress)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 2018-01-03
      • 2013-03-17
      相关资源
      最近更新 更多