We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Not implemented when save()

Hello,

Im using Phalcon3, Im getting the error "Not implemented" when saving a model. I already have data in my model, and then added a toMany relationship. I tried to update the model and then I got this error. It seems that the relationship it's well done since I can retrieve data from it, but since I added it I cannot update any reference to the model. Here are the classes: This class is the one giving the error on save:

class Team extends Model
{
    public $id;
    public $name;
    public $url;
    public $id_sport;
    public $id_ext;
    private $types = ["id" => "int", "name" =>"string", "url" => "string", "id_sport" => "int", "id_ext" => "int"];

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('team');
        $this->belongsTo('id_sport', 'Sport', 'id', NULL);

        $this->hasMany(
            ['id'],
            TeamToGroups::class,
            ['id_team'],
            [
                'reusable' => true, // cache related data
                'alias'    => 'teamToGroups',
            ]
        );
        $this->hasOne(
            ['id_sport'],
            Sport::class,
            ['id'],
            [
                'reusable' => true, // cache related data
                'alias'    => 'sport',
            ]
        );
    }
    }
This is the class is related to:

class TeamToGroups extends Model
{
    public $id;
    public $id_team;
    public $id_groups;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('teamToGroups');
        $this->belongsTo('id_team', 'Team', 'id', NULL);
        $this->belongsTo('id_groups', 'Groups', 'id', NULL);

        $this->hasOne(
            ['id_groups'],
            Groups::class,
            ['id'],
            [
                'reusable' => true, // cache related data
                'alias'    => 'groups',
            ]
        );

    }

And finally the third class:


use Phalcon\Mvc\Model;

class Groups extends Model
{
    public $id;
    public $id_phase;
    public $district;
    public $id_category;
    public $id_sport;
    public $sex;
    public $id_ext;
    public $name_ext;
    private $types = ["id" => "int", "id_phase" =>"int", "district" => "string", "id_category" => "int", "id_sport" => "int", "sex" => "string", "id_ext" => "int"];

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('groups');
        $this->belongsTo('id_phase', 'Phase', 'id', NULL);
        $this->belongsTo('id_category', 'Category', 'id', NULL);
        $this->belongsTo('id_sport', 'Sport', 'id', NULL);

        $this->hasMany(
            ['id'],
            TeamToGroups::class,
            ['id_groups'],
            [
                'reusable' => true, // cache related data
                'alias'    => 'teamToGroups',
            ]
        );
        }
I've seen other post on it but they dont seem to fix it, also it seems its a bug related to phalcon2

Thanks.
edited Feb '19

hi Carla your models looks fine, can you share with us the code where you tring to update models?

tip: try to be consistent with the way you're initializing your models. eg:

public function initialize()
    {
        $this->setSource('team');
        $this->belongsTo('id_sport', Sport::class, 'id', NULL); // here you used 'Sport'

        $this->hasMany(
            'id', // here you used ['id'], if is a simple column usea a string
            TeamToGroups::class,
            'id_team', // idem
            [
                'reusable' => true, // cache related data
                'alias'    => 'teamToGroups',
            ]
        );
        $this->hasOne(
            'id_sport',
            Sport::class,
            'id',
            [
                'reusable' => true, // cache related data
                'alias'    => 'sport',
            ]
        );
    }

good luck

edited Feb '19

Here's where the error appears

foreach ($teamsFetchArray as $key => $team) {
                $team->url = $team->createUrl();
                if ($team->save() == false) {
                    echo "No se pudo guardar \n";

                    foreach ($team->getMessages() as $message) {
                        echo $message;
                    }
                    error_log(date("Y-m-d H:i:s")." Umh, We can't store:\n", 3, LOG_PATH.date("Y-m-d").".log");
                    return false;
                } else {
                    echo "Great, a new Team was saved successfully!";
                }
            }

and here's createUrl method


public function createUrl(){
        try{
            $competicion = $this->urlCompatible($this->teamToGroups[0]->groups->phase->competition->name);
            $sport = $this->urlCompatible($this->sport->name);
            $league = $this->urlCompatible($this->teamToGroups[0]->groups->district);
            $teamName = $this->urlCompatible($this->name);

            return strtolower("/".$competicion."/".$sport."/".$league."/".$teamName."/".$this->id);
        }
        catch(Throwable $e){
            echo '{"status": 500, "error":{"text":"'. $e->getMessage() .'"}}';
        }
    }
    private function urlCompatible($string, $wordLimit = 0){
        $separator = "-";
        $string = strtolower($string);
        $string = iconv('UTF-8','ASCII//TRANSLIT',$string);

        $string = str_replace(" ", "-", $string);
        $string = str_replace(",", "", $string);
        $string = str_replace("/", "", $string);
        $string = str_replace(str_split('\\:*?"<>|+:&.'), "", $string);
        $string = str_replace("--", "-", $string);

        return trim(trim($string, $separator));
    }


8.4k

as @Emilio Degiovanni said

change the following:

Groups:

$this->hasMany('id',TeamToGroups::class,'id_groups',[
    'reusable' => true, // cache related data
    'alias'    => 'teamToGroups',
]);

TeamToGroups:

$this->hasOne('id_groups',Groups::class,'id',[
    'reusable' => true, // cache related data
    'alias'    => 'groups',
]);

Team:

$this->hasMany('id',TeamToGroups::class,'id_team',[
    'reusable' => true, // cache related data
    'alias'    => 'teamToGroups',
]);

$this->hasOne('id_sport',Sport::class,'id',[
    'reusable' => true, // cache related data
    'alias'    => 'sport',
]);