gpt4 book ai didi

symfony - Doctrine 2 : OneToMany Relation, 按外键排序

转载 作者:行者123 更新时间:2023-12-02 01:12:33 25 4
gpt4 key购买 nike

我有三个实体( ProfileProfileValueValue )。
ProfileProfileValue 具有一对多关系,与 Value 具有多对一关系实体。

是否有可能得到相关的ProfileValues来自 Profile , 按 value id 排序?

如果我添加 orderby非外键的注释,例如 ProfileValue 中的启用字段, 有用。但是对于外键,它失败并显示消息 "inrecognized field" 。有什么想法吗?

/**
*
* @ORM\Table(name="profile")
* @ORM\Entity
*/
class Profile {

/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var array $profileCValues
* @ORM\OneToMany(targetEntity="ABC\XYZBundle\Entity\ProfileValue", mappedBy="profile", cascade={"persist"})
* @ORM\OrderBy({"value" = "ASC"})
*/
private $profileValues;

这里是 ProfileValue实体:

/**
* ABC\XYZBundle\Entity\ProfileValue
*
* @ORM\Table(name="profile_value", indexes={@ORM\Index(columns={"profile_id"}), @ORM\Index(columns={"value_id"}) })
* @ORM\Entity
*/
class ProfileValue {
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var integer $enabled
*
* @ORM\Column(name="enabled", type="boolean", length=1, nullable=true)
*/
private $enabled;

/**
* @var ABC\XYZBundle\Entity\Profile
* @ORM\ManyToOne(targetEntity="ABC\XYZBundle\Entity\Profile", inversedBy="profileValues")
* @ORM\JoinColumn(name="profile_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $profile;

/**
* @var ABC\XYZBundle\Entity\Value
* @ORM\ManyToOne(targetEntity="ABC\XYZBundle\Entity\Value")
* @ORM\JoinColumn(name="value_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $value;
}

最佳答案

有同样的问题,通过添加一个带外键的新字段解决了:

/**
* @var integer $valueId
*
* @ORM\Column(name="value_id", type="integer")
*/
private $valueId;

然后您就可以毫无问题地订购了:

/**
* @var array $profileCValues
* @ORM\OneToMany(targetEntity="ABC\XYZBundle\Entity\ProfileValue", mappedBy="profile", cascade={"persist"})
* @ORM\OrderBy({"valueId" = "ASC"})
*/
private $profileValues;

关于symfony - Doctrine 2 : OneToMany Relation, 按外键排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15759267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com