英文:
Problem with specific entity relationship in Symfony 6.3
问题
我们正在逐步将一个非常旧的应用程序重写为较新的Symfony框架,同时在重写完成之前仍然并行运行旧应用程序。
我正在尝试在Symfony中重新创建旧数据库,到目前为止,大多数实体都没问题。
然而,我在一个特定的关系上遇到了问题。
我有一个名为DIET的实体和另一个名为ModelGallery的实体。
关系看起来像这样:
ModelGallery.item_id = diet.id AND ModelGallery.model = 'DIET'
我正在努力弄清楚如何在实体级别解决这个问题 - 在两个列上执行连接。在Symfony中如何实现这一点?非常感谢任何帮助或指导。
我必须调整现有数据库的Symfony代码。
我在存储库中创建了自定义函数:
public function getDietWithModelGalleries(int $id)
{
$qb = $this->createQueryBuilder('d');
$query = $qb->select('d', 'mg')
->leftJoin('App\Entity\ModelGallery', 'mg', Join::WITH, 'd.id = mg.itemId AND mg.model = :model')
->setParameter('model', 'DIET')
->where('d.id = :id')
->setParameter('id', $id)
->getQuery();
return $query->getResult();
}
但我想在实体级别上实现它。
英文:
We are gradually rewriting a very old application into the newer Symfony framework, while still running the old application in parallel until more of it is rewritten.
I'm trying to recreate the old database in Symfony, and so far, most of the entities are OK.
However, I am facing an issue with one particular relationship.
I have an entity called DIET and another one called ModelGallery.
The relationship looks something like this:
ModelGallery.item_id = diet.id AND ModelGallery.model = 'DIET'
I'm trying to figure out how to resolve this at the entity level - performing a join on two columns. How can I achieve this in Symfony? Any help or guidance is much appreciated.
I must adjust symfony code to existing database.
I create custom function in repository
public function getDietWithModelGalleries(int $id)
{
$qb = $this->createQueryBuilder('d');
$query = $qb->select('d', 'mg')
->leftJoin('App\Entity\ModelGallery', 'mg', Join::WITH, 'd.id = mg.itemId AND mg.model = :model')
->setParameter('model', 'DIET')
->where('d.id = :id')
->setParameter('id', $id)
->getQuery();
return $query->getResult();
}
But I want to make it on Entity-Level
答案1
得分: 3
要在Symfony中定义实体级别的关系,可以使用自定义存储库方法和Doctrine扩展。
首先,从ModelGallery到Diet定义一个多对一的关系:
// ModelGallery实体
/**
* @ORM\ManyToOne(targetEntity="Diet")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="item_id", referencedColumnName="id"),
* @ORM\JoinColumn(name="model", referencedColumnName="model")
* })
*/
private $diet;
这将在item_id和model上创建连接条件。
接下来,为ModelGallery创建一个存储库类,并添加一个自定义方法来检索与某个饮食相关的图库:
// ModelGalleryRepository
public function findByDiet(Diet $diet)
{
return $this->createQueryBuilder('mg')
->andWhere('mg.diet = :diet')
->setParameter('diet', $diet)
->getQuery()
->getResult();
}
然后在控制器或服务中,可以这样做:
$galleries = $galleryRepository->findByDiet($diet);
这将在后台处理连接。确保还将diet属性添加到ModelGallery中以实现双向关联。
英文:
To define this relationship at the entity level in Symfony, you can use a custom repository method and doctrine extensions.
First, define a many-to-one relationship from ModelGallery to Diet:
// ModelGallery entity
/**
* @ORM\ManyToOne(targetEntity="Diet")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="item_id", referencedColumnName="id"),
* @ORM\JoinColumn(name="model", referencedColumnName="model")
* })
*/
private $diet;
This will create the join condition on item_id and model.
Next, create a repository class for ModelGallery and add a custom method to retrieve galleries for a diet:
// ModelGalleryRepository
public function findByDiet(Diet $diet)
{
return $this->createQueryBuilder('mg')
->andWhere('mg.diet = :diet')
->setParameter('diet', $diet)
->getQuery()
->getResult();
}
Then in your controller or service, you can do:
$galleries = $galleryRepository->findByDiet($diet);
This will handle the join behind the scenes. Make sure to also add the diet property to ModelGallery for two-way association.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论