0

Cakephp 不保存日期字段的选定值我的表单字段如下:

<? echo $this->Form->control('deadline', ['class' => 'form-control text-form-control', 'type' => 'datetime']); ?>

我可以使用数据库中的现有任务编辑和更改日期,但是当我添加新任务时,我遇到了按摩错误:

Cannot convert value of type `string` to integer
InvalidArgumentException

在表格模型中,我有:

<?php
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) {
    foreach (['deadline'] as $key) {
        if (isset($data[$key]) && is_array($data[$key])) {
            $data[$key] = strtotime($data[$key]['year'] . '-' . $data[$key]['month'] . '-' . $data[$key]['day'] . ' ' . $data[$key]['hour'] . ':' . $data[$key]['minute']);
        }
    }
}
?>

堆栈跟踪:

Error: [InvalidArgumentException] Cannot convert value of type `string` to integer (\vendor\cakephp\cakephp\src\Database\Type\IntegerType.php:72)
#0 \vendor\cakephp\cakephp\src\Database\Type\IntegerType.php(91): Cake\Database\Type\IntegerType->checkNumeric('2020-01-23 15:5...')
#1 \vendor\cakephp\cakephp\src\Database\TypeConverterTrait.php(36): Cake\Database\Type\IntegerType->toDatabase('2020-01-23 15:5...', Object(Cake\Database\Driver\Mysql))
#2 \vendor\cakephp\cakephp\src\Database\Statement\PDOStatement.php(68): Cake\Database\Statement\StatementDecorator->cast('2020-01-23 15:5...', 'integer')
#3 \vendor\cakephp\cakephp\src\Database\ValueBinder.php(146): Cake\Database\Statement\PDOStatement->bindValue('c7', '2020-01-23 15:5...', 'integer')
#4 \vendor\cakephp\cakephp\src\Database\Connection.php(332): Cake\Database\ValueBinder->attachTo(Object(Cake\Database\Statement\MysqlStatement))
#5 \vendor\cakephp\cakephp\src\Core\Retry\CommandRetry.php(67): Cake\Database\Connection->Cake\Database\{closure}()
#6 \vendor\cakephp\cakephp\src\Database\Connection.php(336): Cake\Core\Retry\CommandRetry->run(Object(Closure))
#7 \vendor\cakephp\cakephp\src\Database\Query.php(218): Cake\Database\Connection->run(Object(Cake\ORM\Query))
#8 \vendor\cakephp\cakephp\src\ORM\Table.php(2129): Cake\Database\Query->execute()
#9 \vendor\cakephp\cakephp\src\ORM\Table.php(2021): Cake\ORM\Table->_insert(Object(App\Model\Entity\Task), Array)
#10 \vendor\cakephp\cakephp\src\ORM\Table.php(1934): Cake\ORM\Table->_processSave(Object(App\Model\Entity\Task), Object(ArrayObject))
#11 \vendor\cakephp\cakephp\src\ORM\Table.php(1639): Cake\ORM\Table->Cake\ORM\{closure}()
#12 \vendor\cakephp\cakephp\src\Database\Connection.php(737): Cake\ORM\Table->Cake\ORM\{closure}(Object(Cake\Database\Connection))
#13 \vendor\cakephp\cakephp\src\ORM\Table.php(1640): Cake\Database\Connection->transactional(Object(Closure))
#14 \vendor\cakephp\cakephp\src\ORM\Table.php(1935): Cake\ORM\Table->_executeTransaction(Object(Closure), true)
#15 \src\Controller\TasksController.php(65): Cake\ORM\Table->save(Object(App\Model\Entity\Task))
#16 \vendor\cakephp\cakephp\src\Controller\Controller.php(609): App\Controller\TasksController->add()
4

1 回答 1

0

好的,不太确定这是否会对您有所帮助,我能想到一些您可以尝试的方法。
我认为您正在尝试做很多事情而不是依赖 cakePHP 3(相信我,我觉得您在那里,因为我自己也有这个习惯)。
就您通过在表初始化中添加时间戳在表中添加行为而言:

public function initialize(array $config): void
{
    parent::initialize($config);

    $this->addBehavior('Timestamp');//<--- this one here
    ......
}

如果已使用正确的控件来捕获视图中的数据(正如您所拥有的那样),则您无需处理日期时间转换。
请注意,如果字段“deadline”在数据库中定义为 DateTime 类型,则无需指定字段的类型(因为您尝试转换为YYY-MM-DD HH :ii:SS看起来很像 SQL DateTime)。
CakePHP 3 将从数据库模式中推断出它并采取行动。

总结一下,我会尝试的是:

  • 添加我上面写的代码
  • 删除beforeMarshal代码
  • 最后(但可选)将“截止日期”控件的表单行更改为:
<? echo $this->Form->control('deadline', ['class' => 'form-control text-form-control']); ?>

在这些更改之后,使用检查控制器中接收到的数据

$data = $this->request->getData();

然后实体在修补数据后的样子

$entity = $this->YourModel->patchEntity($youEntityModel, $data);

如果您检查$entity类似的东西,debug($entity)您应该能够看到属性“错误”,并且如果有一些奇怪的东西,例如错误初始化或格式错误的数据,以及当 cakePHP 尝试将数据从表单格式化为合适的实体对象之前保存。

于 2021-06-17T04:37:24.233 回答