【问题标题】:Phalcon model is not recognizing AUTO_INCREMENT table columnPhalcon 模型无法识别 AUTO_INCREMENT 表列
【发布时间】:2016-04-14 21:27:36
【问题描述】:

当前表结构为:

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title         | varchar(25)      | NO   | UNI | NULL    |                |
| content       | varchar(500)     | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

型号:

<?php

namespace Com\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Articles extends Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $title;

    /**
     *
     * @var string
     */
    public $content;

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'articles';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Articles[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Articles
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

从控制器保存数据后,我收到此错误:id is required

编辑

这里是保存方法的sn-p:

$article = new Articles();

$article->title = $this->request->getPost('title', 'striptags');
$article->content = $this->request->getPost('content');

if (!$article->save()) {
    $this->flash->error($article->getMessages());
} else {
    $this->flash->success("Article created.");
    Tag::resetInput();
}

【问题讨论】:

  • 用保存方法更新了问题。
  • 您是否正确配置了modelsMetadata 服务?
  • 是的,配置正确。问题仅限于 Articles 模型。
  • 你解决了这个问题吗?
  • @FazalRasel 不,这很奇怪,我改用 NoSQL 解决方案。

标签: php mysql model phalcon


【解决方案1】:

我从来没有遇到过自动增量字段和 Phalcon 的问题。 我唯一能建议的是将默认设置为 AUTO_INCREMENT

【讨论】:

  • 您好,感谢您的回答。是否可以为 primary key 列提供默认值?
  • 例如,您可以添加主键列 'id' 并将其设为默认 auto_increment,例如: ALTER TABLE table_name ADD COLUMN id INT(11) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY ( id);这可能会有所帮助:w3schools.com/sql/sql_autoincrement.asp
  • 不幸的是,目前就是这样,并且由于某些奇怪的原因无法正常工作。 :(
【解决方案2】:

删除缓存文件

例如。 "~\cache\metaData\meta-bds_models_yourmodel-your_model.php"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2019-11-01
    相关资源
    最近更新 更多