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

Save()/Update()/Create() fails on models with relations using composite primary keys

Hello everyone, 'Not Implemented' Exception is being thrown at me when I try to update/create a model which has a relation using composite primary keys. Naturally, I thought that this is yet to be implemented, however while I was searching the web, I came across some thread answers by @phalcon which imply that this is actually possible to pull out, but it just keeps throwing those exceptions :)

So my question is: can you create()/update()/save() related models using composite keys?



98.9k

I think only many-to-many relations with composite keys could throw that exception, create()/update()/save() must work on composite keys without problem

I can confirm this is still an issue. I found the culprit in the source code.

My two related models:

class TestingJobEndpoint extends \Phalcon\Mvc\Model

    public function initialize()
    {
        $this->belongsTo("job_id", "TestingJob", "id");
        $this->belongsTo("endpoint_id", "TestingEndpoint", "id");
        $this->hasMany(array('job_id', 'endpoint_id'), 'TestingJobEndpointSla', array('jobId', 'endpointId'), array('alias' => 'Slas'));
    }
}
class TestingJobEndpointSla extends \Phalcon\Mvc\Model
...
    public function initialize()
    {
        $this->belongsTo(array('jobId', 'endpointId'), 'TestingJobEndpoint', array('job_id', 'endpoint_id'), array('alias' => 'JobEndpoint'));
        $this->belongsTo('endpointId', 'TestingEndpoint', 'id', array('alias' => 'LocalEndpoint'));
    }

Source Code

static PHP_METHOD(Phalcon_Mvc_Model, _postSaveRelatedRecords){
...
            if (Z_TYPE_P(columns) == IS_ARRAY) { 
                PHALCON_CALL_METHOD(NULL, connection, "rollback", nesting);
                PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Not implemented");
                return;
            }
...
}

At least there should be something in the docs about it, because the way the things are now, this is expected to be implemented and work.

Has anyone found a solution to this problem, I get the exception below when i try to save a model.

"Not implemented" File = phalcon/mvc/model.zep Line = 2640

My model relations are defined like this

$this->hasMany(array('id','company_id'), 'Multiple\Backend\Models\Subscription', array('store_id','company_id'), array('alias' => 'Subscription'));
$this->belongsTo(array('store_id','company_id'), 'Multiple\Backend\Models\Store', array('id','company_id'), array('alias' => 'Store'));