【问题标题】:Phalcon save unable to performPhalcon保存无法执行
【发布时间】:2017-10-08 06:01:11
【问题描述】:

Phalcon save 函数正在询问必填字段,即使它可从 post 获得。以前使用tag,一切正常,能够完成完整的CRUD 功能。现在我想实现验证并将代码从tag升级到form;更改后我无法执行saveupdate。对于视图,我使用.volt 语法来呈现form

总是收到错误消息为“名称是必需的”,即使它 硬编码。

可能出了什么问题?

型号:

<?php
    class Invoicestatus extends \Phalcon\Mvc\Model
    {
        protected $id;
        protected $name;
        protected $description;
        public function setId($id)
        {
            $this->id = $id;

            return $this;
        }
        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }
        public function setDescription($description)
        {
            $this->description = $description;

            return $this;
        }
        public function getId()
        {
            return $this->id;
        }
        public function getName()
        {
            return $this->name;
        }
        public function getDescription()
        {
            return $this->description;
        }
        public function initialize()
        {
            $this->setSchema("invoice");
            $this->setSource("invoicestatus");
            $this->hasMany(
                'Id',
                'Invoice',
                'InvoiceStatusId',
                [
                    'alias' => 'Invoice',
                    'foreignKey' => [
                        'message' => 'The invoice status cannot be deleted because other invoices are using it',
                    ]
                ]
            );
        }
        public function getSource()
        {
            return 'invoicestatus';
        }
        public static function find($parameters = null)
        {
            return parent::find($parameters);
        }
        public static function findFirst($parameters = null)
        {
            return parent::findFirst($parameters);
        }
    }
    ?>

控制器:

<?php
    $form = new InvoicestatusForm();
    $invoicestatus = new Invoicestatus();
    $data = $this->request->getPost();

    if (!$form->isValid($data, $invoicestatus)) {
        $messages = $form->getMessages();

        foreach ($messages as $message) {
            $this->flash->error($message);
        }

        return $this->dispatcher->forward(
            [
                "action"     => "new",
            ]
        );
    }
    //$invoicestatus->name = $this->request->getPost('name', 'string');
    //$invoicestatus->description = $this->request->getPost('description', 'string');
    //$success = $invoicestatus->save();
    $success = $invoicestatus->save($data, array('name', 'description'));
    if($success)
    {
        $form->clear();
        $this->flash->success("Invoice Status successfully saved!");
        $this->dispatcher->forward(['action' => 'index']);
    }
    else
    {
        $this->flash->error("Following Errors occured:");
        foreach($invoicestatus->getMessages() as $message)
        {
            $this->flash->error($message);
        }
        $this->dispatcher->forward(['action' => 'new']);
    }
    ?>

表格:

<?php
    class InvoicestatusForm extends Form
    {
        public function initialize($entity = null, $options = null)
        {
            if (isset($options['edit']) && $options['edit']) {
                $id = new Hidden('id');
            } else {
                $id = new Text('id');
            }
            $this->add($id);
            $name = new Text('name', ['placeholder' => 'Name']);
            $name->setFilters(["striptags","string",]);
            $name->addValidators([new PresenceOf(["message" => "Name is required...",])]);
            $this->add($name);
            $description = new Text('description', ['placeholder' => 'Description']);
            $this->add($description);
        }
    }
    ?>

【问题讨论】:

  • 解决这个问题的一个方法是进入您的数据库表并将列设置为 NULL 而不是 NOT NULL。
  • 您还可以检查$this-&gt;request-&gt;getPost() 是否包含数据。
  • @Ultimater ‘post’ 正在返回值。我也不想在 DB 中允许空值。

标签: php phalcon phalcon-orm


【解决方案1】:

这不是在验证之前和保存时发生的 notNullValidations。您可以在数据库中设置列类型 NULL 或通过以下方式禁用 notNullValidations:

Model::setup(
    [
        'notNullValidations' => false,
    ]
);

【讨论】:

  • 有问题吗?我不想让 NULL 值进入数据库。这是唯一的方法吗?
  • 另外,当我遇到错误时,我不会尝试存储 NULL 值
  • 如果你没有为属性设置任何值 - 那么它默认为空。
  • 首先,这个错误是来自表单验证还是你保存模型的时候?
  • 然后 idk,使用 xdebug。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2014-09-07
  • 2014-08-08
  • 2020-11-17
  • 2019-09-05
  • 1970-01-01
相关资源
最近更新 更多