cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

ORM - Save NULL foreign keys

Open calinrada opened this issue 12 years ago • 41 comments

I have a table with a froiegn key (scan_id) that can be null. In the model i have:

    public function beforeValidation()
    {
        if ($this->scan_id == '') {
            $this->scan_id = new \Phalcon\Db\RawValue('NULL');
        }
    }

But when i try to save the object i get "Value of field "scan_id" does not exist on referenced table".

Any suggestions ? Thanks.

Phalcon 1.2.6 PHP 5.5.8

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

calinrada avatar Feb 12 '14 19:02 calinrada

Try:

public function beforeValidation()
    {
        if ($this->scan_id == '') {
            $this->scan_id = null;
        }
    }

ghost avatar Feb 13 '14 16:02 ghost

The same problem. Not working with $this->scan_id = null; Db field is correctly set:

Field Type Null Key Default Extra id int(11) unsigned NO PRI NULL auto_increment scan_id int(11) unsigned YES MUL NULL
created_at datetime NO NULL
updated_at datetime YES NULL

calinrada avatar Feb 13 '14 16:02 calinrada

It seems the current implementation tries to find a record with null on the referenced table: https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model.zep#L1483

ghost avatar Feb 13 '14 16:02 ghost

What is the solution ? This is a big problem in my situation :)

calinrada avatar Feb 13 '14 17:02 calinrada

Something like this instead of the virtual foreign key must provide a similar functionality:

public function beforeSave()
{
    if ($this->scan_id !== null) {
        if (!Scans::count(['scan_id', 'bind' => $this->scan_id])) {
            $this->appendMessage(new Message("Scan does not exist"));
            return false;
        }
    }
        return true;
}

ghost avatar Feb 13 '14 17:02 ghost

Sorry. Your proposal is not working. Also, i have hundreds of models with tens of foreign keys ... adding this "patch" would be suicidal. Can it be fixed somehow in 1.2.7 ?

calinrada avatar Feb 13 '14 17:02 calinrada

There will be no version 1.2.7, next version is 1.3.0. @sjinks would you able to add this to 1.3.0?

ghost avatar Feb 13 '14 17:02 ghost

Thank you.

calinrada avatar Feb 13 '14 17:02 calinrada

Hi guys. Do you have an ET for this ?

calinrada avatar Feb 17 '14 15:02 calinrada

Hi guys. Still no update on this. It is quite an important issue.

calinrada avatar Mar 17 '14 09:03 calinrada

any solution for this problem?

eclipse88 avatar Mar 24 '14 13:03 eclipse88

Is someone still following this issue ? @phalcon ? @niden ?

calinrada avatar Mar 31 '14 15:03 calinrada

Working on this

ghost avatar Mar 31 '14 16:03 ghost

Guys, I might be a pain in the a** but this is getting vital. And it's not only about me:

http://forum.phalconphp.com/discussion/1804/how-to-save-foreign-key-with-null-value- http://forum.phalconphp.com/discussion/2033/null-foreign-key-on-mysql-orm http://forum.phalconphp.com/discussion/1951/saving-null-foreign-keys

Can we please do something quicker ?

calinrada avatar Apr 15 '14 15:04 calinrada

:+1: for fixing this!

thanks!

kbsali avatar Apr 15 '14 15:04 kbsali

Gentlemen,

i met the same problem and it looks like i have found a smart work-around for saving null value as foreign key. (phalcon 1.2.6, php 5.4.27). Imagine we have two models: Album and Song with obvious has-many relation (Song has 'album_id' internally which references to Album, thus Album has many songs). So, i do the following in Songs model

class Songs extends \Phalcon\Mvc\Model {
  public function beforeValidation()
    {
        if (is_null($this->Album)) {
            $this->album_id = null;
        }
    }
}

and the following in my code

$song = Album->Songs->getFirst();

// want to break the relationship (so called save NULL foreign key in db)
$song->Album = null;
$song->save(); // that's all! beforeValidation callback does everything behind the scenes

This trick does work for me in fact.

I was surprised why this works for me so easy, and i found that unfortunetely it works only if there is no array( "foreignKey" => true ) parameter in the relation definition.

Vastly avatar Apr 24 '14 10:04 Vastly

Can somebody confirm whether this has been fixed or not? Seems that Phalcon's ORM is weird when it comes to relations and foreign keys. By the way, I can't figure out whether I can define different constraints for ON FK UPDATE and ON FK DELETE?

scrnjakovic avatar Jun 06 '14 19:06 scrnjakovic

At least some update, @phalcon ? This is really important!

Thanks

scrnjakovic avatar Jun 12 '14 20:06 scrnjakovic

I'm running php 5.5.13 with phalcon 1.3.2 and MySQL 5.6.19 and this is still a big issue in our application! ETA on a fix @phalcon ?

natronite avatar Jun 18 '14 07:06 natronite

Good luck getting answer, @natronite :)

scrnjakovic avatar Jun 18 '14 14:06 scrnjakovic