I'm stuck with my CakePHP project because in my controller, save() is returning false (if I leave a field blank) even though there are no validation problems.
I created from scratch a new simplified project to reproduce the issue (and the problem actually it's still there:
This is the database:
CREATE TABLE cars (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
make VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE images (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
car_id INT UNSIGNED,
PRIMARY KEY (id),
FOREIGN KEY car_key (car_id) REFERENCES cars (id)
);
The 'images' table only contains the id and a reference to the cars table since the image will be then stored with the id as the filename.
Then, I did nothing else that let CakePHP generate the useful stuff:
bin/cake bake all --everything
I started the server and created a couple of 'cars', then if I try to add an 'image' that works only if I select a car in the dropdown, if I leave that field blank save() will return false (even though there are no validation constraints on that field).
(In the complete version, the form has the possibility to add an image file, that will be saved with the element id as the filename, that part works well).
If I create an image (selecting a car), then I go to Edit, I'm able to remove the reference to Cars, it is saved correctly.
Why that happens?
EDIT:
Could that be a bug in CakePHP, when you have just an id and a foreign key field? If I add a third column to the 'images' table (name VARCHAR(50)), and regenerate the project, then I'm allowed to create an 'image' without referencing a 'car', even though I leave all the fields blank.
EDIT 2:
Tried logging all the queries in mysql:
If, in the form, I select a 'car_id' then the query is ran:
INSERT INTO images (car_id) VALUES (5)
If i don't select a 'car_id' no query is attempted. If I try to run the correct query manually, it works:
INSERT INTO images () VALUES ()