ORM - Save NULL foreign keys
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.
Try:
public function beforeValidation()
{
if ($this->scan_id == '') {
$this->scan_id = null;
}
}
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
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
What is the solution ? This is a big problem in my situation :)
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;
}
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 ?
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?
Thank you.
Hi guys. Do you have an ET for this ?
Hi guys. Still no update on this. It is quite an important issue.
any solution for this problem?
Is someone still following this issue ? @phalcon ? @niden ?
Working on this
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 ?
:+1: for fixing this!
thanks!
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.
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?
At least some update, @phalcon ? This is really important!
Thanks
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 ?
Good luck getting answer, @natronite :)