PHP-Doctrine2: Items - Shops - ItemsAtShops - how to conveniently implement using Doctrine2?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 13:32:15

I struggle with this kind of scenario in Doctrine, as well. Rails had spoiled me with their has_many :through => relationship which makes this sort of thing trivial.

You are correct, you do need three entities: Shops, Items, ItemsAtShops using dual ManyToOne relationships.

I would assume that ItemsAtShop would look like:

class ItemsAtShop 
{        
   private $shop;
   private $items;
   private $quantity;
}

As far a querying goes, you'll need to rock the joins:

$queryBulder->select('ias')
            ->from(ItemsAtShop, 'ias')
            ->leftJoin('ias.Item', 'i')
            ->leftJoin('ias.Shop', 's')
            ->where('s.id = :shop_id')
            ->andWhere('i.price <= :my_price')
            ->orderBy('i.price', 'DESC');

Remember, when using DQL, you're usually querying entire Entity objects, which will fetch their relationships on their own. This should result in a collection of ItemsAtShop you can iterate over.

I might take some fidgeting to figure out the correct query. I'll often start with a SQL query I know works, and translate it into DQL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!