0
votes

I a making a plugin in CakePhp and I am using multiple models inside the plugin. I created those models by using the console and baked them. I ran into a problem when using 1 of them and it only appears with this specific model.

The model file name:
GamificationBadgeUnlockActionProgression.php

Content of model:

<?php
    App::uses('GamificationAppModel', 'Gamification.Model');

    class GamificationBadgeUnlockActionProgression extends GamificationAppModel {

    function __construct() {
        parent::__construct();
        echo 'oleej';
        exit;
    }

    public $belongsTo = array(
        'GamificationBadgeProgression' => array(
            'className' => 'GamificationBadgeProgression',
            'foreignKey' => 'gamification_badge_progression_id'
        ),
        'GamificationBadgeUnlockAction' => array(
            'className' => 'GamificationBadgeUnlockAction',
            'foreignKey' => 'gamification_badge_unlock_action_id'
        )
    );

    public function addMissingProgression($missingProgression)
    {
        /*$this->saveMany($missingProgression, array(
            'fieldlist' => array('gamification_badge_progression_id', 'gamification_badge_unlock_action_id')
        ));*/
    }
}

In the controller of the plugin I have:

public $uses = array('Gamification.GamificationBadge',
    'Gamification.GamificationBadgeProgression', 
    'Gamification.GamificationBadgeUnlockAction',
    'Gamification.GamificationBadgeUnlockActionProgression', 
    'Gamification.GamificationBadgeUnlockType');

All the other models are being loaded, except 'Gamification.GamificationBadgeUnlockActionProgression'. Which is very weird. It has all required stuff and it follows the naming conventions and everything. It just isn't being loaded as the constructor is never called and the echo is never shown.

When I call the function from the model like this with an array as parameter:

    $this->GamificationBadgeUnlockActionProgression->addMissingProgression($missingProgression);

Then I get an error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'addMissingProgression' at line 1

Because the model is not loaded.
However CakePhp does create a magic model with the same name, which results in the error above, because the model magically excists but the function ofcourse doesnt and neither do the relations.

What am I missing here?

1
you can check weather the model is load or not by ClassRegistry::isKeySet('Name of your model');Fazal Rasel
It is, because cakephp creates a magical version of itDijkeMark

1 Answers

2
votes

That kind of error is thrown when the Model isn't properly loaded, it tries to execute the method as an SQL function, which obvisouly doesn't exist. At the top of your Controller, check if it has:

App::uses('GamificationBadgeUnlockActionProgression', 'Gamification.Model');

That, combined with the Gamification.GamificationBadgeUnlockActionProgression in the $uses array that you already have should be enough for it to load. If it still fails, as a last resort you could manually load it in your method using a fallback like:

if (!is_object($this->GamificationBadgeUnlockActionProgression)) {
    ClassRegistry::init('Gamification.GamificationBadgeUnlockActionProgression');
    $this->GamificationBadgeUnlockActionProgression = new GamificationBadgeUnlockActionProgression();
}

That would manually load the Model and create an instance for it under $this->GamificationBadgeUnlockActionProgression.

There also appears to be an error in the associations in the model, it should include the Plugin prefixes, like:

public $belongsTo = array(
    'GamificationBadgeProgression' => array(
        'className' => 'Gamification.GamificationBadgeProgression',
        'foreignKey' => 'gamification_badge_progression_id'
    ),
    'GamificationBadgeUnlockAction' => array(
        'className' => 'Gamification.GamificationBadgeUnlockAction',
        'foreignKey' => 'gamification_badge_unlock_action_id'
    )
);