James
James

Reputation: 688

Undefined Index "counterCache" CakePHP

I'm doing a simple task for a project. It works as expected but after (second if) when I use find method on my Contact Model it throws a notice: undefined index "counterCache". I want to fix this notice to prevent future crashes. I understand my code is messy and not perfectly implemented (I had to update the model and I can't change the way everything is connected).

I read CakePHP's API manual but I don't seem to understand what's going on. How can I resolve this notice?

Notice:

Notice (8): Undefined index: counterCache [CORE/Cake/Model/Model.php, line 2109]
Notice (8): Undefined index: counterCache [CORE/Cake/Model/Model.php, line 2109]

Code:

public function confirm($request_id = null, $contact_that_wants_to_add_you_id = null, $confirm = 0){
    $this->layout = 'dashboard';
    $this->User->hasMany=$this->User->belongsTo=$this->User->hasManyAndBelongsTo=array();
    $this->Contact->hasMany=$this->Contact->belongsTo=$this->Contact->hasManyAndBelongsTo=array();
    $this->Contact->belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
    ));
    $requests = $this->Contact->find('all', array(
        'conditions' => array(
            'Contact.id' => $request_id
        )
    ));
    $exists = $this->Contact->find('count', array(
        'conditions' => array(
            'Contact.user_id' => $this->Auth->user('id'),
            'Contact.contact_id' => $contact_that_wants_to_add_you_id
        )
    ));
    $confirm_contact = $this->Contact->find('first', array(
        'conditions' => array(
            'Contact.id' => $request_id,
            'Contact.user_id' => $contact_that_wants_to_add_you_id,
            'Contact.contact_id' => $this->Auth->user('id')
        )
    ));

    if($exists == 0 && $confirm == 1){

        $exists = $confirm_contact;
        $exists['Contact']['id'] = null;
        $exists['Contact']['user_id'] = $this->Auth->user('id');
        $exists['Contact']['contact_id'] = $contact_that_wants_to_add_you_id;
        $exists['Contact']['friend'] = 1;
        $exists['Contact']['confirmed'] = 1;
        $confirm_contact['Contact']['confirmed'] = 1;

        if($this->Contact->save($exists) && $this->Contact->save($confirm_contact)){
            $requests = $this->Contact->find('first', array(
                'conditions' => array(
                    'Contact.id' => $this->Contact->getLastInsertID()
                )
            ));
        }
    }

    $this->set('requests',$requests);
}

Update (Fixed): The solution came really easy when I re-read the documentation. BUT, I still don't know why that happens and I'd like to know why. If anyone could answer that please, I'll be eternally grateful.

Original:

$this->Contact->belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
));

Fix:

$this->Contact->belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'counterCache' => false
));

Upvotes: 0

Views: 440

Answers (1)

ndm
ndm

Reputation: 60453

You are setting up associations wrongly, the model properties should only be used before the model is initialized at construction time.

After that point you should use Model::bindModel(), otherwise the associations will lack the default options, such as counterCache, which may lead to errors such as the one you are experiencing.

$this->Contact->bindModel(
    array(
        'belongsTo' => array(
            'User' => array(
                'className' => '...',
                'foreignKey' => '...',
                // ...
            )
        )
    ),
    false
);

See also Cookbook > Associations> Creating and Destroying Associations on the Fly

Upvotes: 1

Related Questions