根据文档,api-platform默认情况下将急切的加载资源。
但是,在默认配置上,我所有与关系(主要是典型的Many2One
关系(对资源的查询都将这些属性与对象IRI而不是序列化对象填充。
,例如,对于此实体:
/**
* @ORMEntity(repositoryClass="AppRepositoryBoostLeadContactActionRepository")
* @ORMTable(name="BoostLeadContactActions")
*/
class BoostLeadContactAction {
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMManyToOne(targetEntity="AppEntityBoostLead", inversedBy="contacts")
* @ORMJoinColumn(nullable=false)
*/
private $boostLead;
/**
* @ORMManyToOne(targetEntity="AppEntityContactChannel", fetch="EAGER")
* @ORMJoinColumn(nullable=false, referencedColumnName="Id")
*/
private $channel;
// getters/setters/extra properties removed for brevity
}
然后我们有相应的ContactChannel
:
/**
* ContactChannel
*
* @ORMTable(name="ContactChannels")
* @ORMEntity
*/
class ContactChannel {
/**
* @var int
*
* @ORMColumn(name="Id", type="smallint", nullable=false, options={"unsigned"=true})
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="Name", type="string", length=64, nullable=false)
*/
private $name = '';
}
即使不需要理论上,我已经在关系上设置了fetch="EAGER"
。我的配置是默认值,但以防万一我实际上在api_platform.yaml
中写了这一点:
eager_loading:
# To enable or disable eager loading.
enabled: true
# Fetch only partial data according to serialization groups.
# If enabled, Doctrine ORM entities will not work as expected if any of the other fields are used.
fetch_partial: false
# Max number of joined relations before EagerLoading throws a RuntimeException.
max_joins: 30
# Force join on every relation.
# If disabled, it will only join relations having the EAGER fetch mode.
force_eager: true
debug:config api_platform
的结果确认应用了正确的配置:
Current configuration for extension with alias "api_platform"
=============================================================
api_platform:
title: FooBar API
description: 'FooBar API, only for authenticated use'
version: 0.8.0
name_converter: null
path_segment_name_generator: api_platform.path_segment_name_generator.underscore
eager_loading:
enabled: true
fetch_partial: false
max_joins: 30
force_eager: true
但是结果将是:
{
"@context": "/api/contexts/BoostLeadContactAction",
"@id": "/api/boost_lead_contact_actions/9",
"@type": "BoostLeadContactAction",
"id": 9,
"boostLead": "/api/boost_leads/30",
"channel": "/api/contact_channels/1",
"direction": "outgoing",
"successful": true,
"type": "/api/lead_contact_attempt_reasons/1",
"notes": "2",
"createdAt": "2019-05-16T10:27:33+00:00",
"updatedAt": "2019-05-16T10:27:33+00:00"
}
" boostlead","通道"one_answers"类型"应该是实际的实体,急切地加载,但仅返回虹膜。我确认执行的SQL查询不包括任何类型的join
。
有趣的是,这似乎是与其他用户相反的麻烦。我希望我有他们的问题。
什么可以阻止这些关系急切地加载?关系否则可以使用(我可以使用学说进行其他查询,或创建自定义序列化组,并且将包括相关属性(。
默认情况下,使用可递送的虹膜显示相关关联。查看执行的语句,您不应该看到明确的JOIN
查询,而是相关关联的其他SELECT
语句。
如果您想要关联对象的JSON表示形式。您需要为相关关联中所需的属性指定Serialization
@Groups
。这将导致SELECT
语句添加在JOIN
中以检索要序列化的相关数据。
有关更多详细信息,请参见https://api-platform.com/docs/core/serialization/#embedding-relations
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ApiResource(normalizationContext={ "groups": {"boost"} })
* @ORMEntity()
* @ORMTable(name="BoostLeadContactActions")
*/
class BoostLeadContactAction {
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
* @Groups({"boost"})
*/
private $id;
/**
* @ORMManyToOne(targetEntity="AppEntityBoostLead", inversedBy="contacts")
* @ORMJoinColumn(nullable=false)
* @Groups({"boost"})
*/
private $boostLead;
/**
* @ORMManyToOne(targetEntity="AppEntityContactChannel", fetch="EAGER")
* @ORMJoinColumn(nullable=false, referencedColumnName="Id")
* @Groups({"boost"})
*/
private $channel;
// getters/setters/extra properties removed for brevity
}
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ApiResource()
* @ORMTable(name="ContactChannels")
* @ORMEntity
*/
class ContactChannel {
/**
* @var int
* @ORMColumn(name="Id", type="smallint", nullable=false, options={"unsigned"=true})
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
* @Groups({"boost"})
*/
private $id;
/**
* @var string
* @ORMColumn(name="Name", type="string", length=64, nullable=false)
* @Groups({"boost"})
*/
private $name = '';
}
应该导致检索归一化值
{
"@context": "/api/contexts/BoostLeadContactAction",
"@id": "/api/boost_lead_contact_actions/9",
"@type": "BoostLeadContactAction",
"id": 9,
"boostLead": "/api/boost_leads/30",
"channel": {
"@id": "/api/contact_channels/1",
"@type": "ContactChannel",
"id": "1",
"name": "Test"
},
"direction": "outgoing",
"successful": true,
"type": "/api/lead_contact_attempt_reasons/1",
"notes": "2",
"createdAt": "2019-05-16T10:27:33+00:00",
"updatedAt": "2019-05-16T10:27:33+00:00"
}