简体   繁体   中英

cakephp OR condition not working

I have a table of Events which belongsTo a Meeting. Events hasAndBelongsToMany Groups, Segments, Areas, and Regions. Currently the page displays all events which have at least 1 group, segment, area, and region they are linked to which the user is also linked to. I added a field to the events table, filtered_by_area, which checks whether the Event should only display to certain Areas/Regions or to everyone. I need to change the find so that the events that display are either those where filtered_by_area is 0, or if the event is linked to the same areas/regions as the user. But I can't seem to make it show the unfiltered event. Here is my code:

$conditions = array(
            'conditions' => array(
                'Meeting.id' => $id,
                array(
                'OR' => array(
                    array("Event.filtered_by_area" => 0),
                    array("AreasEvent.area_id IN ('$areas')",
                          "EventsRegion.region_id IN ('$regions')")
                ))
            ),
            'joins' => array(
                array(
                    'table' => 'events',
                    'alias' => 'Event',
                    'type' => 'INNER',
                    'conditions' => array(
                        'Meeting.id = Event.meeting_id'
                    )
                ),
                array(
                    'table' => 'areas_events',
                    'alias' => 'AreasEvent',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Event.id = AreasEvent.event_id',
                    )
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'Area',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Area.id = AreasEvent.area_id',
                    )
                ),
                array(
                    'table' => 'events_regions',
                    'alias' => 'EventsRegion',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Event.id = EventsRegion.event_id',
                    )
                ),
                array(
                    'table' => 'regions',
                    'alias' => 'Region',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Region.id = EventsRegion.region_id',
                    )
                )
            ),
            'contain' => array(
                'Event.Segment',
                'Event.Asset' => array(
                    'Image',
                    'Document',
                    'VimeoVideo',
                    'Link'
                ),
                'Event.Group' => array(
                    'conditions' => array(
                        'Group.name' => $this->group_name
                    )
                ),
                'Event.Area' => array(),
                'Event.Region' => array()
            )
        );

        if (!$this->Meeting->exists()) {
            throw new NotFoundException(__('Invalid meeting'));
        }

        $conditions['contain']['Event.Segment'] = array(
            'conditions' => array(
                'Segment.id' => $segments
            )
        );

        $conditions['contain']['Event.Area'] = array(
            'conditions' => array(
                'Area.id' => $areas
            )
        );

        $conditions['contain']['Event.Region'] = array(
            'conditions' => array(
                'Region.id' => $regions
            )
        );

        if (isset($dates[$day])) {
            $conditions['contain']['Event'] = array(
                'conditions' => array(
                    'date_format(Event.start, "%Y-%m-%d")' => $dates[$day]
                ),
                'order' => 'Event.start asc'
            );
        }
        $meeting = $this->Meeting->find('first', $conditions);

Any help?

this here :

'OR' => array( array("Event.filtered_by_area" => 0), array("AreasEvent.area_id IN ('$areas')", "EventsRegion.region_id IN ('$regions')") ))

remove this, and change to this way :

'contain'=>array( 'Model'=>array('condition'=>$condition) )

Here:

array(
    'OR' => array(
        array("Event.filtered_by_area" => 0),
        array("AreasEvent.area_id IN ('$areas')",
              "EventsRegion.region_id IN ('$regions')")
    ))
)

Remove the array that wraps the 'OR'. It would be like this:

'conditions' => array(
    'Meeting.id' => $id,
    'OR' => array(
        "Event.filtered_by_area" => 0,
        'AND' => array(
            "AreasEvent.area_id" => $areas,
            "EventsRegion.region_id" => $regions
        )
    ))
)

EDIT: Corrected the IN clause. If $areas and $regions are array, Cake will do automatically a IN statement on the query

EDIT 2: Corrected arrays inside OR

EDIT 3: Third edit =_= added AND

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM