Hi.
I using in my app some data about IPv4 adresses and Networks.
They stored in DB as integers. Simple it looks like
Networks:
id (PK)
addr (UNSIGNED INT)
mask (UNSIGNED INT) //11111111111111111111111100000000 (in binary)
mask_prefix (int) // 24 in decimal
Ips:
id (PK)
addr (UNSIGNED INT)
And logicaly (and mathematicaly) they are in a strait relations between each other:
Network mask says how many left bits (of 32 max) is fixed in network adresses, so if ip address is compare to network address in fixed network mask part it relates to network, otherwise - it not
here sample SQL conditions for that relation:
thru prefix (calculates minimal and maximal addresses in network)
(networks
.addr
<= net_ips
.addr
) AND ((networks
.addr
+ POWER(2,(32-networks
.mask_prefix
)) > net_ips
.addr
))
thru binary mask
(networks
.addr
& networks
.mask
) = (net_ips
.addr
& networks
.mask
)
so
in math logic that tables already related to each other as Parent-Child, just without a keys, and i know how to use it in raw SQL
but
i heavily need to make activeRecord realations with all tasty features of it as joinWith, eager loading and relation itself
if i do Networks to IP relation like this
return NetIps::find()->where([āANDā,
[ā>=ā,āaddrā,$this->addr],
[ā<ā,āaddrā,$this->addr+$this->capacity] //capacity calculates from mask
])->all();
It works as relation, it give me IPs, but i cant use joinWith with that relation (it says that it is no relation at all)
if i do like that
return new ActiveQuery(NetIps::className(),[
ālinkā=>[],
āonā=>"(networks
.addr
<= net_ips
.addr
) AND ((networks
.addr
+ POWER(2,(32-networks
.mask
)) > net_ips
.addr
))",
āmultipleā=>true,
]);
then i see it bulds correct SQL join and request all correctly, but it not works as simple relation. it returns activeQuery class instead of NetIps forked from ActiveRecord
I need somehow declare a relation but without keys link
Help me please, give me at least a direction