propel对象集合将列别名绑定到外部表对象,而不是将其取出

fruv7luv  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(348)

我有两个db表,一个是item,另一个(unit)表示item表可用的所有度量单位。在这个场景中,item表包含一个充当一对多关系的unit表的外键。
使用PropeOrm,我可以像下面这样连接两个表并获得json结果:

$items = ItemQuery::create()
            ->joinWith('Item.Unit')
            ->withColumn("CONCAT (Unit.Name,'-',Unit.Label)", "MUnit")
            ->find()
            ->toJSON();

输出结果为:

{
   "Items":[
      {
         "Id":2,
         "Code":"M000002",
         "Name":"Item 1",
         "Price":234,
         "UnitId":1,
         "Status":true,
         "MUnit":"Kilograms-Kg",
         "Unit":{
            "Id":1,
            "Name":"Kilograms",
            "Label":"Kg",
            "Status":true
         }
      },
      {
         "Id":3,
         "Code":"M000003",
         "Name":"Item 2",
         "Price":100,
         "UnitId":2,
         "Status":true,
         "MUnit":"Meter-Mt",
         "Unit":{
            "Id":2,
            "Name":"Meter",
            "Label":"Mt",
            "Status":true
         }
      }
   ]
}

如您所见,列alias munit从unit对象到父对象。但我真正需要的是将别名保存在unit对象中,如下所示。

...

 "Items":[
      {
         "Id":2,
         "Code":"M000002",
         "Name":"Item 1",
         "Price":234,
         "UnitId":1,
         "Status":true,
         "Unit":{
            "Id":1,
            "Name":"Kilograms",
            "Label":"Kg",
            "Status":true,
            "MUnit":"Kilograms-Kg",// here goes...
         }
      },
       ...

任何实现预期产出的建议都是值得赞赏的。谢谢您。
更新
如果我将代码更改为结果对象集合,那么输出将是:

$items = ItemQuery::create()
            ->joinWith('Item.Unit')
            ->withColumn("CONCAT (Unit.Name,'-',Unit.Label)", "MUnit")
            ->find();

输出:

...

# data: array:2 [▼

    0 => Item {#323 ▼
      #new: false
      #deleted: false
      #modifiedColumns: []
      #virtualColumns: array:1 [▼
        "MUnit" => "Kilograms-Kg" // <--- virtual column
      ]
      #id: 2
      #code: "M000002"
      #name: "Item 1"
      #price: 234.0
      #unit_id: 1
      #status: true
      #aUnit: Unit {#324 ▼
        #new: false
        #deleted: false
        #modifiedColumns: []
        #virtualColumns: []
        #id: 1
        #name: "Kilograms"
        #label: "Kg"
        #status: true
        #collItems: ObjectCollection {#325 ▶}
        #collItemsPartial: true
        #alreadyInSave: false
        #itemsScheduledForDeletion: null
      }

...

所以我真正需要的是 virtualColumn 弹药进入单位物体。

rvpgvaaj

rvpgvaaj1#

以下方法对我有效:

$items = \ItemQuery::create()
            ->joinWith('Item.Unit')
            ->withColumn("CONCAT (Unit.Name,'-',Unit.Label)", "MUnit")
            ->find();
        foreach ($items as $i) {
            $i->getUnit()->setVirtualColumn('Asdf', $i->getVirtualColumn('MUnit'));
        }
        dd($items->toJSON());

结果输出:

...

 "Items":[
      {
         "Id":2,
         "Code":"M000002",
         "Name":"Item 1",
         "Price":234,
         "UnitId":1,
         "MUnit":"Kilograms-Kg",
         "Status":true,
         "Unit":{
            "Id":1,
            "Name":"Kilograms",
            "Label":"Kg",
            "Status":true,
            "Asdf":"Kilograms-Kg",// here goes...
         }
      },
       ...

相关问题