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

After setting up the foreign key constraints on database, I cannot use the function of CASCADE.

Model 1: class Teacher extends \Phalcon\Mvc\Model { public $id; public $name; public function initialize() { $this->hasMany( "id", "students", "teacher_id" 'foreignKey' => array( 'action' => Relation::ACTION_CASCADE ) ); } } Model 2: class Students extends \Phalcon\Mvc\Model { public $id; public $teacher_id; public $name; public function initialize() { $this->belongsTo( "teacher_id", "teacher", "id", array( "foreignKey" => true ) ); } } $teacher = Teacher::findFirst("id=1"); $teacher->delete(); SQLSTATE[HY000]: General error: 2292 OCIStmtExecute: ORA-02292: integrity constraint (TEST.FK_STUDENT_TEACHER_ID) violated - child record found (/root/rpmbuild/BUILD/php-5.5.1/ext/pdo_oci/oci_statement.c:148)

I changed the order of the steps of deletion and then I can use CASCADE. Is bug? /**

  • Check if there is virtual foreign keys with cascade action */ if (PHALCON_GLOBAL(orm).virtual_foreign_keys) {

            PHALCON_INIT_NVAR(check_foreign_keys);
            phalcon_call_method(check_foreign_keys, this_ptr, "_checkforeignkeysreversecascade");
            if (PHALCON_IS_FALSE(check_foreign_keys)) {
                    RETURN_MM_FALSE;
            }
    }
    
    /**
     * Do the deletion
     */
    PHALCON_INIT_VAR(success);
    phalcon_call_method_p4(success, write_connection, "delete", table, delete_conditions, values, bind_types);
    
    if (PHALCON_GLOBAL(orm).events) {
            if (zend_is_true(success)) {
                    PHALCON_INIT_NVAR(event_name);
                    ZVAL_STRING(event_name, "afterDelete", 1);
                    phalcon_call_method_p1_noret(this_ptr, "fireevent", event_name);
            }
    }'''

need to delete child record before parent record i think it is a bug



98.9k

First, It deletes the students, then it deletes the teacher, maybe it is not finding the students. Could you please set an events manager up in the connection to see what SQL comments are being sent to the database server?



20.4k

Hi,

i'm getting the same problem too: the CASCADE in model initialize is ignored so i've to currently delete children records manually. This seems a bug ( regression since no problems were found on 1.2.3 ).

Regards

same problem here