0

I need help to solve a problem with a CakePHP 1.1 project, and a findAll query.

Here is the query:

$events = $this->EventCategory->findAll(null, null, array("EventCategory.name" => "ASC"));

And this is a example of the result array:

    [1] => Array
    (
        [EventCategory] => Array
            (
                [id] => 1
                [name] => Agencias
                [date] => 2009-12-15 16:07:08
            )

        [EventSubcategory] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [event_category_id] => 1
                        [name] => Agencias de marketing promocional
                        [date] => 2009-12-15 16:09:51
                        [Event] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 1
                                        [event_subcategory_id] => 2

The problem, is, I need to filter by Event.id = X, in Cake 1.2+ i use the contain and filter by the associated table, but in 1.1 I dont find documentation for make this.

PS: The project is too big for migrate the version.

1 Answer 1

0

Have you tried adding Event.id to your conditions? If you have recursive set correctly (which it looks like you have) this might work.

If you want to be sure, then it might be worth adding Event to your EventCategory model too.

var $hasMany = array(
  'EventSubcategory' => array(
    'className' => 'EventSubcategory',
    'foreignKey' => 'event_category_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => ''
  ),
  'Event' => array(
    'className' => 'Event',
    'foreignKey' => false,
    'dependent' => false,
    'conditions' => 'Event.event_subcategory_id = EventSubcategory.id',
    'fields' => '',
    'order' => ''
  )
);

Make sure Event is added to the $hasMany after EventSubcategory.

2
  • I try your HasMany, but give me a "Unknown column 'Event.event_category_id' in 'field lis", my association is IDENTICAL but not have the "'Event' => array(.." in the HasMany, and no.. the condicion Event.d = X dont work ("Unknown column 'Event.id' in 'where.:")
    – Zenth
    Sep 27, 2011 at 14:18
  • @Zenth not sure where you're getting Event.event_category_id from, is that a typo in your associations? Also, either reduce 'recursive' to 0 or 1, or better still rename 'Event' => array(... above to something like 'EventCategoryEvent' => array(... So that there is no clash with Event coming from EventSubcategory's association with Event.
    – ianmjones
    Sep 27, 2011 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.