ThinkPHP的关联Model要注意的几点

_link = array(
            'HotelRoomSell' => array(
                'mapping_type' => HAS_MANY,
                "parent_key" => 'rs_id',//parent_key一定是主键,否则会列不出子表的数据
                'foreign_key' => 'hrs_rsid',
                'condition' => " hrs_checkin_date >= '" . $_POST['stime'] . "' and hrs_checkin_date <= '" . $_POST['etime'] . "' "
            
            ),
            'Suppliers' => array(
                'mapping_type' => BELONGS_TO,
                "parent_key" => 's_id',
                'foreign_key' => 'rs_sid',
                'as_fields' => 's_name'
            ),
            'Hotel' => array(
                'mapping_type' => BELONGS_TO,
                "parent_key" => 'h_id',
                'foreign_key' => 'rs_hid',
                'as_fields' => 'h_name'
            ),
            'HotelRoom' => array(
                'mapping_type' => BELONGS_TO,
                "parent_key" => 'hrm_id',
                'foreign_key' => 'rs_rid',
                'as_fields' => 'hrm_title,hrm_issell,hrm_id'
            ),
        );
        parent::__construct($name);
    }

}

HAS_MANY的情况下
//parent_key一定是主键,否则会列不出子表的数据


多个HAS_MANY不能指定as_fields很奇怪


 array(
            'mapping_type' => HAS_MANY,
            "parent_key" => 'hro_id',
            'foreign_key' => 'hroi_hroid',
            'mapping_fields' => 'hroi_nums,hroi_checkin_date,hroi_checkout_date,hroi_price,hroi_is_checkin'
        ),
        'HotelRoomOrderTicket' => array(
            'mapping_type' => HAS_MANY,
            "parent_key" => 'hro_id', //当前表的ID  很坑;不看源码不可能知道的事
            'foreign_key' => 'hrot_hroid', //外表的ID
//              'mapping_fields' => 'hrot_use_date,hrot_nums,hrot_price,hroi_is_checkin'
        //多个HAS_MANY的时候;只能同时存在一个 mapping_fields;否则无法查出数据;
        ),
        'Hotel' => array(
            'mapping_type' => BELONGS_TO,
            "parent_key" => 'h_id',
            'foreign_key' => 'hro_hid',
            'as_fields' => 'h_name'
        ),
        'HotelRoom' => array(
            'mapping_type' => BELONGS_TO,
            "parent_key" => 'hrm_id',
            'foreign_key' => 'hro_rid',
            'as_fields' => 'hrm_title'
        ),
        'AgentUser' => array(
            'mapping_type' => BELONGS_TO,
            "parent_key" => 'id', //外表的ID  BELONGS_TO才有parent_key属性;
            'foreign_key' => 'hro_auid', //当前表的ID
            'as_fields' => 'account,nickname'
        ),
        'HotelRoomOrderMember' => array(
            'mapping_type' => HAS_ONE,
            // "parent_key" => 'hro_id', //HAS_ONE,默认使用主键进行关联,没有这个属性,设置了和没设一个鸟样
            'foreign_key' => 'hrom_hroid',
            'as_fields' => 'hrom_name,hrom_tel'
        ),
    );

}


你可能感兴趣的:(ThinkPHP)