【问题标题】:Phalcon save not workingPhalcon保存不起作用
【发布时间】:2014-09-07 14:42:26
【问题描述】:

我对 Phalcon 很陌生,我真的很喜欢它,目前正在从事我的第一个项目,但是我目前遇到了 ORM 的问题。

我似乎无法保存。

我的用户模型目前有一个简单的表设置

id (int, PK) 名称(字符串) 用户名(字符串) 电子邮件(字符串) 活跃(整数) createdDate (int)

我的模型使用注释策略将这些定义为属性:

<?php

/**
 * Users class
*
* Represents Holla users
*
 */
class Users extends Phalcon\Mvc\Model
{

/**
 * @Primary
 * @Identity
 * @Column(type="integer", nullable=false)
 */
public $id;

/**
 * @Column(type="string", length=70, nullable=false)
 */
public $name;

/**
 * @Column(type="string", length=70, nullable=false)
 */
public $username;

/**
 * @Column(type="string", length=256, nullable=false)
 */
public $password;

/**
 * @Column(type="integer", nullable=false)
 */
public $active;

/**
 * @Column(type="integer", nullable=false)
 */
public $createdDate;

我的初始化有以下内容:

public function initialize()
{

    $this->setSource('users');
    $this->hasManyToMany('id', 'UsersAttributes', 'userId', 'attributeId', 'Attributes', 'id', array('alias' => 'attributes'));
    $this->hasMany('id', 'Status', 'userId');
    $this->hasMany('id', 'UsersNeighbors', 'userId');
    $this->hasMany('id', 'UsersFriends', 'userId');
    $this->hasMany('id', 'UsersNeighborhoods', 'userId');

}

然后我有一个简单的注册表单,我指向我的一个控制器中的一个方法,我在其中进行了一些验证,然后尝试进行保存:

        $user = new Users();

        $user->name = $name;
        $user->username = strtolower(str_replace(' ', '', $name));
        $user->password = $this->security->hash($password);

        if($user->save())
        {

            $this->flash->success('Registered successfully!');

            $this->session->set('auth', array(
                'id' => $user->id,
                'name' => $user->name
            ));

            return $this->response->redirect('user/home');

        }
        else
        {

            $this->flash->error('An error occured, unable to register');

            return $this->response->redirect('');

        }

我已将分析器设置为保存到日志中,当我进行注册时似乎发生的一切是:

[Wed, 16 Jul 14 21:46:44 +0000][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='users'
[Wed, 16 Jul 14 21:46:44 +0000][INFO] DESCRIBE `users`

然后它只是将我引导到主视图,并显示我在上面定义的 flash 错误“发生错误”,所以我有点不知道为什么它没有创建新用户而只是返回 false在用户保存时。

提前致谢

【问题讨论】:

  • 检查$user-&gt;getMessages() 是否有类似此处的错误:docs.phalconphp.com/en/latest/reference/…
  • @jodator 谢谢,是的,它有效,我得到了保存工作,想在答案中弹出它,我会接受吗?
  • 我很高兴有帮助。在此先感谢:)

标签: php orm phalcon


【解决方案1】:

检查$user-&gt;getMessages() 的错误总是有助于调试$model-&gt;save() 的问题。

更多信息在这里:http://docs.phalconphp.com/en/latest/reference/models.html#creating-updating-records

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多