Hello, everybody,
I’m writing this to request some more details on how to use the (pretty advanced) features of CActiveRecord and foreign key DB relations. While I’ve understood the basic details about lazy and eager loading, relations and so on, I find myself having a lot of trouble trying to implement these things in the “real” world, especially in more “convoluted” scenarios.
For example, take a look at the following situation: I’m currently developing an application which manages some clients of an ISP. Each client has multiple subscriptions. Subscriptions can be the actual data services that the ISP is providing, or other, value added, services, such as fixed IP on a link, multiple physical links for a specific service and so on. Data services subscriptions have, on their turn, connections. Scenario: a client wants fixed IPs on 3 of his internet connections. The application will add 3 subscriptions to the value-added service “Fixed IP” and will link them to their specific connections, through a field called dependentObject, related to the “connections” table, which will show why we bill that client for that specific service. After a few months, the client decides to give up one of the fixed IP services. I must program the application to delete that subscription to the “Fixed IP” service pertaining to that particular connection. This is a task which must be done by the Connections model.
What I need to do is, basically, to look through the client’s subscriptions, identify the subscription with the package ID “Fixed IP” and depending on our Connection, and, then, delete it. Again, as a reminder, this has to be done from the Connection model, which “belongs” to a Subscription, which “belongs” to a Client. The only way I can safely do it right now is like this:
$clientSubs = $this->subscription->client->subscriptions;
foreach($clientSubs as $sub)
if (($sub->packageId == $fixedIpServId) && ($sub->dependentObject == $this->id)) {
$sub->delete();
break;
}
I’m absolutely sure that there’s a nicer, more elegant way of doing this using the innate capabilities of CActiveRecord. The question is: what it that way. Bigger question: who can explain in a few lines the “keys” to understanding those innate capabilities ?
Thanks in advance !