You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Phalcon ORM同表关联:如何获取主运单的子运单集合

Phalcon ORM: 实现主运单与子运单的关联查询

Hey there! Let's sort out how to properly get child shipments for your master shipment using Phalcon ORM.

First off, your basic hasMany configuration is actually correct—it sets up the one-to-many relationship between a master shipment (via id) and its child shipments (via master_id). But we can tweak it to make it more usable and explicit.

Step 1: Refine the Association with an Alias

Add an alias to your hasMany setup so you can easily reference the child shipments later. You can also add some optional but useful configurations like making the query reusable:

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

    public function initialize() {
        parent::initialize();
        $this->hasMany(
            'id', 
            'Shipment', 
            'master_id',
            [
                'alias' => 'childShipments', // 给关联起个清晰的名字
                'reusable' => true, // 让关联查询可复用,提升性能
                'order' => 'hawb ASC' // 可选:按hawb排序子运单
            ]
        );
    }

    // 确保getters/setters存在,Phalcon的魔术方法也会自动处理关联属性
    public function getId() { return $this->id; }
    public function getMasterId() { return $this->master_id; }
    public function getChildShipments() {
        return $this->getRelated('childShipments');
    }
}

Step 2: Get Child Shipments (Lazy Loading)

Once you have a master shipment instance, you can access its child shipments directly via the alias or the getter method. This uses lazy loading—the child shipment query only runs when you first access the property:

// 获取主运单(id=1)
$masterShipment = Shipment::findFirst(1);

// 获取所有子运单集合
$childShipments = $masterShipment->childShipments;
// 或者用getter方法
$childShipments = $masterShipment->getChildShipments();

// 遍历子运单
foreach ($childShipments as $child) {
    echo "子运单HAWB: " . $child->getHawb() . "\n";
}

Step 3: Auto-Load Child Shipments (Eager Loading)

If you want to load the master shipment and its child shipments in a single query (to avoid N+1 query issues), use the with method for eager loading:

// 查询主运单时自动加载子运单
$masterShipment = Shipment::findFirst([
    'conditions' => 'id = :id:',
    'bind' => ['id' => 1],
    'with' => ['childShipments'] // 指定要预加载的关联
]);

// 直接使用子运单,无需额外查询
foreach ($masterShipment->childShipments as $child) {
    echo "子运单ID: " . $child->getId() . "\n";
}

Bonus: Filter Child Shipments Dynamically

You can also filter child shipments when accessing them, without modifying the base association:

$masterShipment = Shipment::findFirst(1);
// 获取状态为active的子运单,并按hawb排序
$activeChildShipments = $masterShipment->getChildShipments([
    'conditions' => 'status = :status:',
    'bind' => ['status' => 'active'],
    'order' => 'hawb DESC'
]);

That's all! With these tweaks, you can easily fetch and manage child shipments directly from your master shipment model.

内容的提问来源于stack exchange,提问作者GeorgeGeorgitsis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:58:20