dongpai9986 2018-06-01 09:22
浏览 109
已采纳

Phalcon ORM字段与同一表中的另一个字段有关

I have the following table in my mysql DB

mysql

I have a model Shipment and i am using Phalcon's ORM to interact with my DB. The master_id field in this table is actually the id of another raw, but in the same table, as you can see in the picture. The id=1 is the Master Shipment and id=2 and id=3 are Subshipments.

My model right now looks like this

class Shipment extends Model
{
    protected $id;
    protected $hawb;
    protected $master_id;

    public function initialize()
    {
        parent::initialize();

        $this->hasMany(
            'id',
            'Shipment',
            'master_id'
        );
    }
    //setters and getters are here
}

When I am using $shipment = ShipmentModel::findFirst($id); in my Controller, i can see the master_id of the Shipment.

What i want to have is, call another function from my Shipment model in order to retrieve all the SubShipments as a Collection of Shipment models, or at least an array with Shipment models.

Or even better, if the ShipmentModel::findFirst($id); could populate the SubShipments automatically (if there are any) would be the best!

I don't really know if the $this->hasMany that I have is correct over there, so if someone could show me how to proceed I would be grateful :)

  • 写回答

1条回答 默认 最新

  • doulang9521 2018-06-01 10:12
    关注

    You need to provide full namespace, not just class name - to solve this Model 'Shipment' could not be loaded. Best is to add alias:

    $this->hasMany(
        'id',
        Shipment::class,
        'master_id',
        ['alias' => 'subShipments'])
    );
    

    And then just use:

    Shipment::findFirst()->getSubShipments() you can add such method if you want and do return $this->getRelated('subShipments')

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)