如何在Doctrine中没有关系的情况下加入?

时间:2017-08-26 23:53:39

标签: php join doctrine-orm doctrine

我有一个实体Log(表log),其成员为resourceTyperesourceId(列resource_logresource_id)。 resourceType例如Order Whatever(针对订单操作,例如状态更改)或Whatever(针对Log相关操作)。 Log和“资源”实体/表之间没有关系(在Doctrine和数据库中都没有)。

现在,我只想选择OrdermyOrderProperty = "someValue"相关的SELECT * FROM `log` JOIN `order` ON `order`.`id` = `log`.`resource_id` AND `log`.`resource_type` = 'order' WHERE `order`.`my_order_property` LIKE '%my_order_property_value%' s $queryBuilder = $this->entityManager->createQueryBuilder(); $queryBuilder->select('l')->from(Log::class, 'l'); $queryBuilder->join('l.order', 'o'); ... $queryBuilder ->where('o.myOrderProperty = :myOrderProperty') ->setParameter('myOrderProperty', $myOrderProperty); 。这意味着:

Log

但代码

Order

不起作用,因为实体order[Semantical Error] line 0, col 102 near 'o WHERE o.myOrderProperty': Error: Class MyNamespace\Log has no association named order 没有任何关系(尽管它有一个属性JOIN):

<tr class=''>
    <td class='lot_column'>
        <div class='lot_number'>
            <span class='lot_digits'>41151</span>
            <br />
            <a href='property_information.pl?lot=41151' class='sd_button blue'><span>View Data</span></a>
        </div>
    </td>
    <td>
        <div class='short_desc'>Michael L. Hanisko, A/I/F for Shirley Isackson -     Winterfield Richfield Unit and Grout Unit (Producing MI/RI) - Clare and Gladwin Counties, Michigan</div>
        <div class='headline'>7 Mo. Av. Cash Flow $15/mo</div>
        <div class='message'></div>
    </td>
    <td class='high_bid'>
        <div class='high_bid'>$810</div>
        <div class='reserve_status'>No&nbsp;Reserve</div>
    </td>
    <td class='lot_close'>
        <span class='closing_date'>8/29/2017 1:35 PM</span>
        <br />
        <a href='bidder_bid.pl?lot=41151' class='sd_button red'><span>Make Bid</span </a>
    </td>

如果Set Lot_Data = EnergyNet.getElementsByClassName("lot_digits") For Each elem In Lot_Data Set Link = elem.getElementsByTagName("tr")(0) i = i + 1 'Copy Data to Excel Worksheets("Sheet1").Activate Range("B" & i) = Link.href Range("C" & i) = Link.innerText Range("D" & i) = Link.ID Next 没有在两个实体之间定义关系?

我知道,我可以使用inheritance。但从语义上讲,它不是一个继承案例。那还有另一种解决问题的方法吗?

1 个答案:

答案 0 :(得分:4)

没有关系的JOIN可以像这样实现:

$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('l')->from(Log::class, 'l');
$queryBuilder->join(
    Order::class,
    'o',
    \Doctrine\ORM\Query\Expr\Join::WITH,
    'o.id = l.resourceId'
);
...
$queryBuilder
    ->where('o.myOrderProperty = :myOrderProperty')
    ->setParameter('myOrderProperty', $myOrderProperty);

唯一的问题是,已加入的实体未添加到主要实体,因此$myLog->getOrder(...)会返回null