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

Generate model, error with addForeignKey method

In mysql I created a test schema with 2 tables : Table1 and Test1. Test1 has many Table1

I did use developper tools to generate models with the following command :

./phalcon all-models --relations --fk --namespace=Test\\Model

When I instantiate the Objec Table1

 new Rag\Model\Table1();

I got the following error :

The method "addForeignKey" doesn't exist on model "Rag\Model\Table1"

And indeed I didn't found any reference to the addForeignKey Method in the doc. Probably a mistake, or an old feature from the developper tools ?

Here is the generated Model :


<?php

namespace Test\Model;

class Table1 extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $idtable1;

    /**
     *
     * @var integer
     */
    public $test1_idtable1;

    /**
     *
     * @var string
     */
    public $url;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->belongsTo("test1_idtable1", "Test1", "idtable1");        $this->addForeignKey("test1_idtable1", "Test1", "idtable1");
    }

    public function getSource()
    {
        return 'table1';
    }

}


98.9k

addForeignKey is an old deprecated method (0.4.x), the new syntax is https://docs.phalcon.io/en/latest/reference/models.html#virtual-foreign-keys