Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorsyllk
     
    Hello,

    I've run into a problem where foreign key fields which can be nullable are being inserted with 0. Is there a way to make it insert with NULL instead? For instance,

    Model 1
    ================
    Machines
    id
    name

    Model 2
    ==============
    Tests
    id
    machine_id <--- this field is nullable

    The problem
    ==============
    When inserting a new test machine_id is always set to 0 when input is null.
    • CommentAuthorKaste
     

    can't reproduce. unit-test? ticket?

    Are we talking about this? :

    $Test = $this->Test->create(array('machine_id'=>null));
    $Test>reload();
    
    $this->assertNull($Test->post_id);
    
    • CommentAuthorsyllk
     

    What I meant was an empty foreign key is set always set to 0 even though it should be set to null. This is true for both inserts and update. For instance,

    function test_empty_foreign_key_set_to_null_on_create()  {
        $params = array('name'=>'Empty foreign key field should be set to NULL', 'machine_id'=>'');
        $Testrun =& new Testrun();
        $Testrun->setAttributes($params);
        $this->assertTrue($Testrun->save());
        $Testrun->reload();
        $this->assertNull($Testrun->get('machine_id'));
    }
    
    function test_empty_foreign_key_set_to_null_on_update() {
        $Testrun =& new Testrun();
        $Testrun->newRecord('id->','2','name->','All fields filled.','machine_id->','1');
        $this->assertTrue($Testrun->save());
    
        $Testrun->update(2, array('name'=>'machine_id should be NULL','machine_id'=>''));
        $tRecord = $Testrun->find(2);
        $this->assertNull($tRecord->machine_id);
    }
    

    Ticket # 129.