On May 3, 6:43 pm, Rob Wilkerson <r...@robwilkerson.org> wrote:
> Hey, Joshua -
>
> This is the first solution I've seen/found that looked like it had any
> hope, but I'm not even a little bit familiar with the association
> syntax you're using. I already have, in my Account model:
>
> public $hasMany = array (
>         'Campaign',
>         'Alert' => array ( 'foreignKey' => 'entity_id' )
> );
>
> It looks like you're doing something slightly different, though, that
> I've never seen. Can you point me to a resource that can help me
> understand it better? Is there some additional functionality added by
> defining a specific name for the Alert model join?
>
> Thanks for your input. I'd love to be able to pull all alerts for any
> account and its child items in one simple step. This looks like it
> might get me there, but I'd like to understand more about what you're
> suggesting.

So frustrating. I feel like I'm dancing all around a final solution,
but nothing quite works all the way. Maybe more code will help. As a
reminder, what I'd like to do is pull all of the Alert records that
are related to an Account or any Campaign that belongs to a given
account. I'd like to order the Alerts returned in descending order of
created date.

Here are the associations defined in my Account and Campaign models:

ACCOUNT:
public $hasMany = array (
        'Campaign',
        'Alert' => array ( 'foreignKey' => 'entity_id' )
);

CAMPAIGN:
public $hasMany = array(
        'Alert' => array ( 'foreignKey' => 'entity_id' )
);

ALERT:
public $belongsTo = array (
        'AlertTemplate',
        'Account'  => array ( 'foreignKey' => 'entity_id' ),
        'Campaign' => array ( 'foreignKey' => 'entity_id' )
);

And then my find, being executed from my Account model, looks like
this:

public function getAlerts ( $account_id ) {
        return $this->find (
                'all',
                array (
                        'conditions' => array ( 'Account.id' => $account_id ),
                        'contain' => array (
                                'Alert',
                                'Campaign' => 'Alert'
                        )
                )
        );
}

Finally, for anyone who's still with me, here is the output:

Array
(
    [0] => Array
        (
            [Account] => Array
                (
                    [id] => 49fca3a4-306c-4a12-9a43-a93ecbdd56cb
                    [email] => t...@robwilkerson.otherinbox.com
                    [firstname] => Rob
                    [lastname] => Wilkerson
                    [company_name] => Advertising.com
                    [business_url] => http://test.com
                    [obi_instrument_id] => 1001001
                    [ace_advertiser_id] =>
                    [adcom_advertiser_name] =>
                    [address1] => 1020 Hull Street
                    [address2] =>
                    [city] => Baltimore
                    [postal_code] => 00000
                    [lu_province_id] => MD
                    [industry_types_id] => acb923ef-235a-11de-86d3-
bee43f8d046d
                    [work_phone] => 4103353353
                    [approved_by] =>
                    [threshold_id] => SLAB_250
                    [threshold_hit_count] => 0
                    [last_bill_balance] => 0.00
                    [last_bill_date] => 1241293732
                    [lu_status_id] => AC_WAITING_APPROVAL
                    [privacy_policy_approved] => 0
                    [created] => 1241293732
                    [updated] => 1241293732
                )
            [Alert] => Array
                (
                    [0] => Array
                        (
                            [id] => 49fca3a4-00b4-4e22-be3e-
a93ecbdd56cb
                            [alert_template_id] => WELCOME
                            [entity_id] => 49fca3a4-306c-4a12-9a43-
a93ecbdd56cb
                            [valid_from] =>
                            [valid_to] => 1243885732
                            [replacement_keys] =>
                            [active] => 1
                            [created] => 1241293732
                            [updated] => 1241293732
                        )
                )
            [Campaign] => Array
                (
                    [0] => Array
                        (
                            [id] => 49fca3ab-23fc-4b2b-8820-
a93ecbdd56cb
                            [account_id] => 49fca3a4-306c-4a12-9a43-
a93ecbdd56cb
                            [campaign_name] => My Cherry Red Campaign
                            [adcom_campaign_name] =>
                            [campaign_type] => CPM
                            [lu_channel_id] => 1
                            [daily_budget] => 10.00
                            [cpm] => 1.00
                            [bidding_value] =>
                            [start_date] =>
                            [pause_date] =>
                            [end_date] =>
                            [ace_campaign_id] =>
                            [adcom_campaign_san] =>
                            [lu_terms_condition_id] => 68b0d9a4-
bff5-102b-a990-0015c5f11f45
                            [lu_status_id] => CA_WAITING_APPROVAL
                            [created] => 1241293739
                            [updated] => 1241293739
                            [active] => 1
                            [Alert] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 49f5b7c9-e97c-446b-
ae79-a51acbdd56cb
                                            [alert_template_id] =>
CAMPAIGN_CREATED
                                            [entity_id] =>
49fca3ab-23fc-4b2b-8820-a93ecbdd56cb
                                            [valid_from] =>
                                            [valid_to] =>
                                            [replacement_keys] =>
                                            [active] => 1
                                            [created] => 1241293732
                                            [updated] => 1241293732
                                        )
                                )
                        )
                )
        )
)

As you can see, things get nested fairly deeply and in a way that
could be unpredictable if the models get deeper. Ideally, I'd like to
pull either the alerts as the top level with the associated models
nested (1 level deep) or the models at the top level with the
associated alerts nested (again, 1 level deep).

It seems like Joshua's solution - or some bastardization thereof -
will get me there, but I'm just not getting it. Any remedial help
anyone can offer by way of tweaking my code above would really be
appreciated. I'd hate to have to do this in multiple database calls
(one to get a set of entity ids and another to pull the alerts for
those ids) or by adding a bunch of tables/models to my schema.

Thanks again.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to