无法获得工作的ManyToOne ApiSubresource relaction。错误。StateFieldPathExpression或SingleValuedAssociationField预期。

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

我已经开始在API平台上做一个小项目,检查它是如何工作的。

不幸的是,在开始的时候,我遇到了一个问题,我不能使 @ApiSubresource ManyToOne 关系,因为它会引发一个错误

"[语义错误]第0行,第129行,靠近'user = :cur'。错误:无效的路径表达式。 Invalid PathExpression. StateFieldPathExpression或SingleValuedAssociationField预期。"

我的类看起来是这样的。

/**
 * @ApiResource(
 *     collectionOperations={"post"},
 *     itemOperations={
 *     "get"={ "security"="is_granted('VIEW_AS_OWNER_USER', object)", }
 *     },
 *     normalizationContext={"groups"={"operation:read"}},
 *     denormalizationContext={"groups"={"operation:write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="`user`")
 */
class User implements UserInterface
{
    /* rest of the entity declaration omitted */
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Band", inversedBy="users")
     * @ApiSubresource()
     */
    private $band;
}
/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\BandRepository")
 * @ORM\Table(name="band")
 */
class Band
{
    /* rest of the entity declaration omitted */

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="band")
     */
    private $users;

}

仔细研究了一下Symfony Parser,它似乎试图执行这样的SQL:

SELECT o FROM App\Entity\Band o WHERE o IN(SELECT IDENTITY(id_a1.band) FROM App\Entity\User id_a1 WHERE id_a1.id = :id_p1) AND o.Users = :current_user。

有了这两个参数。enter image description here

我找到了一些建议,但每一个都是针对Symfony的手动查询。有一些自动查询的建立,至少对我来说,是很基础的水平。

你知道我做错了什么吗?还是框架坏了?

php symfony api-platform.com
1个回答
1
投票

使用UUID作为主键不是一个好主意。你的数据库所做的所有比较都会花费更长的时间来比较字符串而不是整数,所以连接的效率会降低。你应该使用一个整数作为主键。UUID是用来避免有人通过改变id来尝试URL,并测试所有id。然后,你可以简单地使用uuid作为另一个属性。你只需要将Api-property设置为 "UUID "即可。identifier 为true,API-Platform将使用它而不是主密钥。这个建议也许可以解决你的问题,但我不确定。

我认为你应该等待API-Platform团队合并。此拉请求

Doctrine使用序列来产生自动增量。所以数据库永远不会尝试插入两个具有相同自动增量键的记录。序列的创建就是为了避免这个问题。

UUID是一个小的性能问题,如果你添加一个索引,如果你只通过这个uuid搜索你的实体。如果你做其他事情,比如与另一个实体进行内部连接,这将是一个很大的性能问题。

© www.soinside.com 2019 - 2024. All rights reserved.