1

I'm sorry if this is a duplicate, but I did not really know how to search for what I want. Consequently I did not find the answer.

So I'm using cakephp and I have a model called 'sponsor' which has many 'banners', 'activities' and 'vacancies'.

In the controller I now have:

public function index()
{
$this->_setRedesign();
$sponsors = $this->Sponsor->find('all', [
  'order'      => [
    'Sponsor.name',
  ],
  'conditions' => [
    'publish_from <'    => date('Y-m-d'),
    'publish_to >'    => date('Y-m-d'),
  ],
]);
$this->set(compact('sponsors'));
}

If I check the variable $sponsors in the cakephp debug, I see for $sponsor: https://i.sstatic.net/4nVYZ.jpg

This is in principle how I want the $sponsors to look like, but what I want is conditioning on the activities for example. So that, for example, sponsor number 1 only has the activities that are in the future (so not all 3 but only 2). Of course I can loop over all the activities of a sponsor in the view and only do something with the ones that are in the future. But I was wondering if there is a controller solution.

Edit: To clarify: I do want all the sponsors (not only the sponsors that have activities in the future), but from one sponsor I only want the activities that take place in the future.

1 Answer 1

0

I found the solution myself. Using the Containablebehavior.

With my code looking like this, I achieved what I wanted:

$sponsors = $this->Sponsor->find('all', [
        'order'      => [
            'Sponsor.name',
        ],
        'conditions' => [
            'publish_from <'    => date('Y-m-d'),
            'publish_to >'    => date('Y-m-d'),
        ],
        'contain' => [
            'Activity' => [
                'conditions' => [
                    'Activity.time_start >'  => date('Y-m-d')
                ]
            ]   
        ]
    ]);

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.