Blog Tutorial / General error: 1364 Field 'category_id' doesn't have a default value

Hello,
i am trying to finish the Blog Tutorial of the CakePHP 4.x Strawberry Cookbook.
Unfortunately i am getting an SQLSTATE[HY000]: General error: 1364 Field ‘category_id’ doesn’t have a default value.

I am at Blog Tutorial - Part 3 right now, but unlike the Tutorials suggests me it is impossible to remove the following controls in the Categories add and edit template files because they do not exist:

echo $this->Form->control(‘lft’);
echo $this->Form->control(‘rght’);

Also it is not possible to disable or remove the requirePresence from the validator for both the lft and rght columns in your CategoriesTable mode because both validators dont exist in the file but I added them:

public function validationDefault(Validator $validator): Validator
{
$validator
->add(‘id’, ‘valid’, [‘rule’ => ‘numeric’])
->allowEmptyString(‘id’, ‘create’);

$validator
    ->add('lft', 'valid', ['rule' => 'numeric'])
//    ->requirePresence('lft', 'create')
    ->notEmpty('lft');

$validator
    ->add('rght', 'valid', ['rule' => 'numeric'])
//    ->requirePresence('rght', 'create')
    ->notEmpty('rght');

}

Thank you for helping!

I’m not sure, if this Blog Tutorial is a relict from Cake3 and it was just forgotten to remove it from the Cake4 Tutorial.

The easiest way to test the Tree Behavior is to create the tables articles and categories and then use the bake command to create all the code.

Articles Table:


-- phpMyAdmin SQL Dump
-- version 4.9.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 27. Jul 2021 um 09:55
-- Server-Version: 5.7.32
-- PHP-Version: 7.4.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Datenbank: `blog`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `articles`
--

CREATE TABLE `articles` (
  `id` int(11) NOT NULL,
  `name` varchar(190) NOT NULL,
  `description` text,
  `category_id` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `articles`
--
ALTER TABLE `articles`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `articles`
--
ALTER TABLE `articles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Categories Table:


-- phpMyAdmin SQL Dump
-- version 4.9.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 27. Jul 2021 um 09:56
-- Server-Version: 5.7.32
-- PHP-Version: 7.4.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Datenbank: `blog`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `categories`
--

CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `name` varchar(190) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `lft` int(11) NOT NULL,
  `rght` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Daten für Tabelle `categories`
--

INSERT INTO `categories` (`id`, `name`, `parent_id`, `lft`, `rght`, `created`, `modified`) VALUES
(1, 'Category 1', NULL, 1, 4, '2021-07-27 09:36:34', '2021-07-27 09:38:10'),
(2, 'Subcategory 1 for Category 1', 1, 2, 3, '2021-07-27 09:38:10', '2021-07-27 09:38:10');

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `categories`
--
ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `categories`
--
ALTER TABLE `categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

Well, it is possible to add Categories including parents… but it is impossible to add articles because of the General error: 1364 Field ‘category_id’ doesn’t have a default value.

i will what you suggested now… thank you!

both tables are like the ones that were created by the tutorial…

but its not working because of the error

CREATE TABLE articles (
id int(11) NOT NULL,
title varchar(255) NOT NULL,
body text NOT NULL,
category_id int(11) NOT NULL,
created datetime NOT NULL,
modified datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


– Daten für Tabelle articles

INSERT INTO articles (id, title, body, category_id, created, modified) VALUES
(1, ‘The title’, ‘This is the article body.’, 1, ‘2021-07-26 15:04:21’, ‘2021-07-26 15:04:21’);


– Indizes der exportierten Tabellen


– Indizes für die Tabelle articles

ALTER TABLE articles
ADD PRIMARY KEY (id);


CREATE TABLE categories (
id int(11) NOT NULL,
parent_id int(11) DEFAULT NULL,
lft int(10) NOT NULL,
rght int(10) NOT NULL,
name varchar(100) NOT NULL,
description varchar(255) NOT NULL,
created datetime NOT NULL,
modified datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


– Daten für Tabelle categories

INSERT INTO categories (id, parent_id, lft, rght, name, description, created, modified) VALUES
(1, NULL, 1, 8, 'Test 1, 'test desc1 ', ‘2021-07-26 12:37:17’, ‘2021-07-26 12:37:17’),
(2, 1, 2, 3, ‘Test 2’, 'test desc2 ', ‘2021-07-26 12:39:34’, ‘2021-07-26 12:39:34’),
(3, 1, 4, 5, ‘Test 3’, ‘test desc3’, ‘2021-07-26 12:52:37’, ‘2021-07-26 12:52:37’),
(4, 1, 6, 7, ‘Test 4’, ‘test desc4’, ‘2021-07-27 10:00:23’, ‘2021-07-27 10:00:23’);


– Indizes der exportierten Tabellen


– Indizes für die Tabelle categories

ALTER TABLE categories
ADD PRIMARY KEY (id);

Error SQLSTATE[HY000]: General error: 1364 Field ‘category_id’ doesn’t have a default value

  • Cake\ORM\Table->saveAPP/Controller/ArticlesController php:54](localhost/articles/add#)

APP/Controller/ArticlesController php](phpstorm:open?file=/var/www/html/src/Controller/ArticlesController.php&line=54)

{
$article = $this->Articles->newEmptyEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->getData());
54: if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));

i just found out how to fix that issue by myself… it was necessary to add ‘category_id’=> true into the Article class of the src/Model/Entity/Article.php file…

protected $_accessible = [
    'title' => true,
    'body' => true,
    'created' => true,
    'modified' => true,
    'category_id'=> true
];

}

In the tutorial, it creates the src/Model/Entity/Article.php like this :

<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Article extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'slug' => false,
    ];
}

where '*'=>true makes all fields accessible.

In the later stages of the tutorial, you will use the bake command to create these files

# Generate all the code at once.
bin/cake bake all tags

blog tutorial… its a different one!