PhalconPHP - set the default foreign key action

时间:2015-06-26 10:15:40

标签: foreign-keys phalcon

Is there a way to define a default foreign key action to be used in every model so I don't have to keep defining it inside every model like below?

$this->hasOne('id', '\Namespace', 'id', [
    'foreignKey' => [
        'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
    ]
]);

1 个答案:

答案 0 :(得分:1)

覆盖hasOne()。

class YourBaseModel extends \Phalcon\Mvc\Model {

    protected function hasOne($local, $remote_model, $remote_field, $options = null) {
        $options['foreignKey'] = [
            'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
        ];

        parent::hasOne($local, $remote_model, $remote_field, $options);
    }

}

class YourModel extends YourBaseModel {

    public function initialize() {
        $this->hasOne('id', '\Namespace', 'id');
    }

}