1

Nobody seems to have a problem with that so either I'm doing it wrong or no one ever tried:

I have a model "Infocenter" which has many "InfocenterArticle"s. To fetch data including the related stuff I attached the Containable behavior to both.

This worked well until now that I attached a "HasImageAttachment" behavior implemented by myself. The problem is that on contained models the callbacks of my behavior don't get called.

My Models:

class Infocenter extends AppModel {
    ...
    $actsAs = array('HasImageAttachment', 'Containable');
    $hasMany = array('InfocenterArticle');
    ...
}

class InfocenterArticle extends AppModel {
    ...
    $actsAs = array('Containable');
    $belongsTo = array('Infocenter');
    ...
}

In my Controller I call:

$conditions = array('InfocenterArticle.id' => $id);
if ($this->notLoggedIn()) $conditions['InfocenterArticle.freigabe'] = 1;
$article = $this->InfocenterArticle->find('first', array(
    'contain' => array(
      'Infocenter',
      'Infocenter.InfocenterArticle' => array(
        'fields' => array('id', 'title', 'freigabe'),
        'order' => array(
          'InfocenterArticle.order_index' => 'desc',
          'InfocenterArticle.created' => 'desc',
          'InfocenterArticle.title' => 'asc'
        ),
        'conditions' => array(
          'InfocenterArticle.infocenter_id' => 'Infocenter.id'
        ),
      ),
    ),
    'conditions' => $conditions,
));

And I can see that my HasImageAttachmentBehavior::setup() method is called but the HasImageAttachmentBehavior::afterFind() (as well as beforeFind()) are not. Infocenter::afterFind() is called though, which enabled me to do some dirty hacking, good enough for now, but I hate it.

What am I doing wrong?

Edit: Additional info in reply to RichardAtHome's comment.

1) My behavior works on models that don't have Containable attached.

2) I made sure that afterFind() doesn't get called by putting a simple die(); in the first line. The script doesn't die().

3) Signature should be okay, I double checked.

4) I'm using CakePHP 1.3.

Thanks for your help.

Rekhyt
  • 76
  • 5
  • Are you sure the afterFind() isn't firing? Have you tried putting some debug code as the first line in afterFind() to make sure it's not being called and something in the afterFind() isn't working? Also, make sure your afterFind() params match those in the Cake cookbook (you don't say which version of CakePHP you are using, 1.3 and 2 have different afterFind signatures) – RichardAtHome Dec 12 '11 at 14:52
  • I added the info you requested to my question. – Rekhyt Dec 12 '11 at 15:00
  • The afterFind works for me in a model, but not in afterFind of a behaviour that is used by the same Model. – Sela Yair Aug 06 '15 at 13:11

3 Answers3

0

I just wrote an extensive entry on how to deal with this type of scenario.

It is for CakePHP 2.x and PHP 5.4+.

Containable behavior alternative function

Community
  • 1
  • 1
Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Aug 20 '13 at 09:42
0

Apparently this was a deliberate point of design(??!?). So you have to upgrade all the way to 3.0 if you want associated models to behave fully like models. Sigh.

Here is an extensive discussion: https://github.com/cakephp/cakephp/issues/1730 and the most straightforward cookbook fix: https://schneimi.wordpress.com/2009/09/06/behavior-afterfind-on-model-associations/

0

Currently I don't believe CakePHP core supports Behavior across Contained models.

It may be due to possible recursion, if you have weird contain array the behaviors may be called incorrectly.

There is a long post on the CakePHP Lighthouse project regarding calling behaviors over associated models, with a few recommendations for workarounds.

http://cakephp.lighthouseapp.com/projects/42648/tickets/95-afterfind-and-beforefind-callbacks-not-working-on-associated-models-was-translate-behavior-should-translate-associated-model-data

fullybaked
  • 4,117
  • 1
  • 24
  • 37