Good evening,
I got some columns in my table that must not be null:
CREATE TABLE `articles` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`sysartno` int(11) NOT NULL,
`sku` varchar(20) COLLATE utf8_bin NOT NULL,
`localartno` int(11) NOT NULL,
`articlename` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`shortdesc` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`desc` text COLLATE utf8_bin,
`brand` int(11) DEFAULT NULL,
`HAN` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`priceNetto` double DEFAULT NULL,
`tax` double DEFAULT NULL,
`priceBrutto` double DEFAULT NULL,
`searchTerms` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`metaKeywords` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`metaTitle` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`metaDesc` text COLLATE utf8_bin,
`gid` int(11) NOT NULL,
`sid` int(11) NOT NULL,
PRIMARY KEY (`aid`),
UNIQUE KEY `sysartno_UNIQUE` (`sysartno`),
UNIQUE KEY `localartno_UNIQUE` (`localartno`),
KEY `grundFK_idx` (`gid`),
KEY `statusFK_idx` (`sid`),
KEY `brandFK_idx` (`brand`),
CONSTRAINT `grundFK` FOREIGN KEY (`gid`) REFERENCES `irrelevanttable1` (`gid`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `markeFK` FOREIGN KEY (`brand`) REFERENCES `irrelevanttable2` (`mid`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `statusFK` FOREIGN KEY (`sid`) REFERENCES `irrelevanttable3` (`sid`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
The model file created on top of this is from the webtools.php; the initalize()
:
public function initialize()
{
$this->setSource("articles");
$this->hasMany('aid', 'irrelevanttable4', 'aid', ['alias' => 'irrelevanttable4']);
$this->hasMany('aid', 'irrelevanttable5', 'aid', ['alias' => 'irrelevanttable5']);
$this->hasMany('aid', 'irrelevanttable6', 'aid', ['alias' => 'irrelevanttable6']);
$this->belongsTo('gid', '\irrelevanttable1', 'gid', ['alias' => 'irrelevanttable1']);
$this->belongsTo('brand', '\irrelevanttable2', 'mid', ['alias' => 'irrelevanttable2']);
$this->belongsTo('sid', '\irrelevanttable3', 'sid', ['alias' => 'irrelevanttable3']);
}
Now, I populate a new instance of the model with the data:
$article = new articles();
$article->setSysartno($art->sysartno);
$article->setSku($art->sku);
$article->setLocalartno($art->localartno);
$article->setArticlename($art->articlename);
...
(all the fields get populated)
Now, when I $article->save();
the model, I get a error message "Articlename is required".
My first thought was "oh no, not THAT again" (see this, might be related: https://forum.phalcon.io/discussion/20567/query-tells-field-is-required-but-is-set)
Then I checked the values of $article
BEFORE the save()
, and it shows me all fields well populated, just like the data provided in $art
(which is always populated by the way, so no random null values from there).
Then I made NULL values in articlename possible (just for testing). This way, the query sent from the save()
looks like this:
INSERT INTO `articles` (all the field names) VALUES (105144, 'DE-105144', 105144, null, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, 9, 1)
If I now check $article
AFTER the save()
, all values except the first four (aid, sysartno, sku, localartno) and the last two (gid, sid) are set to null
(the last two fields contain hardcoded values).
So the data seems to get lost inside the save()
-call.
Also:
$tmp = $article;
var_dump($tmp); // fine
var_dump($article); // fine
echo $article->getArticlename(); // fine
echo $tmp->getArticlename(); // fine
$tmp->save(); // Articlename required
This will probably work with raw SQL, but I want to make use of the foreign keys from the model, so this isn't really an option. I do know this problem with "xy is required" has been asked some times on this forum, but none of the solutions helped me. Hard coding the "problematic values" doesn't change anything, too.
Oh and another thing; why does the output of var_dump($modelInstance)
contain ALL the application data, like metaDataCache, configiguration values, paths, or in short: EVERYTHING thats inside the DI?