繁体   English   中英

教义2一对多/多对一映射

[英]Doctrine 2 Mapping One-to-Many / Many-to-One

我将我的应用程序从doctrine1转换为doctrine2.4,我确实实现了数据库自动映射,但缺少一些关系:

表:产品productid,标题,价格

表:LocationProducts产品编号,产品编号,功能编号,数量

因此每个LocationProducts行都有1个产品,每个产品可以位于几个位置

关于在locationproducts类下的映射,我得到了:

 /**
 * @var \Products
 *
 * @ORM\ManyToOne(targetEntity="Products")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="productid", referencedColumnName="productid")
 * })
 */
private $productid;

在产品模型下,我没有关系,我尝试添加它,但是失败:它说productid映射已经存在

这是我的目标查询:

$qb = $em->createQueryBuilder()
    ->select('p.productid,lp.qty AS totalqty')
    ->from('Products','p')
    ->innerJoin('p.LocationProducts','lp')
    ->setFirstResult( $offset )
    ->setMaxResults( $limit )
    //getDQL
    ->getQuery();

它返回错误:

Class Products has no association named LocationProducts

我想念什么? 谢谢。

您缺少映射的属性注释。

<?php
/**
 * @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
 */
public $phonenumbers;

请参阅此链接中的mappedBy:-http: //docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-onetomany

希望这可以帮助。

干杯!

错误消息指出没有关联,因此您需要添加它。 当前,您的关系是单向的,您不能在p.LocationProducts加入,因为它不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM