English 中文(简体)
记录示范记录,加以编辑,从主计长处提取
原标题:Loading a Model record, editing it and save it from the Controller

I m 试图通过一个用户启动网页()改变注册用户的地位 CakePHP 2。 启用网页上载有一个贴画,位于该用户表中:

id    username    email            password                  active    key
1     bill        [email protected]    4u2iu4hk24(hashed str)    0         2ol3p(hashed str)

启动程序与这种程序相似:

http://url.com/users/activate/2ol3pth3i89txc2zd6tg3

All I do is to get the hashed key, search a registered user with this key, load it, remove the hashed key and change the active status to 1. I do this procedure in this Controller method:

public function activate ($code = false) {
    if (!empty ($code)) {
        $this->User->findByActivationKey($code); // magic find instead of $this->User->find( contidions , array( User.activation_key  => $code));
        if (!empty($user)) {
            $this->User->set(array (
                 activation_key  =>  0 ,
                 active  => 1
            ));
            if ($this->User->save()) {
                $this->render( activation_successful );
            } else {
                // here is always where I get with this code
                $this->render( activation_fail );
            }
        } else {
            $this->render( activation_fail );
        }
    }
}

一切都属于罚款,但$this->User->save(赢得了t work。

使用<代码>debug($this->User->invalid Fields());,将这一错误退回:

app/Controller/UsersController.php (line 64)
Array
(
[password] => Array
    (
        [0] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
        [1] => Password must contain <span5</span> chars min and <span>20</span> chars max message.
    )

)

显然,这一错误来自《示范法》,但为什么提到了<条码>密码/代码>?

<?php
App::uses( AuthComponent ,  Controller/Component );
class User extends AppModel {
    public $name =  User ;
    var $validate = array (
         username  => array (
             MustBeCompiled  => array (
                 rule  =>  notEmpty ,
                 message  =>  Error message 
            )
        ), 
         password  => array (
             not_empty  => array (
                 rule  =>  notEmpty ,
                 message  =>  Password cannot be empty message. 
            ),
             between  => array (
                 rule  => array ( between , 5, 20),
                 message  =>  Password must contain <span5</span> chars min and <span>20</span> chars max message. 
            )
        ),
         email  => array (
             valid_email  => array (
                 rule  =>  email ,
                 message  =>  Mail invalid message. 
            ),
             existing_email  => array (
                 rule  =>  isUnique ,
                 message  =>  Mail in use message. 
            )
        )
    );

    function beforeSave ($options = array()) {
        // the way to hash the password
        if (!empty ($this->data[$this->alias][ password ])) {
            $this->data[$this->alias][ password ] = AuthComponent::password($this->data[$this->alias][ password ]);
            return true;
        }
    }
}
?>

Maybe the problem could be in beforeSave()? Where I m wrong?

问题回答

你们不要把id转过模型,而我猜想你们已经制定了验证规则,以便在节省费用后挽救记录。

所有这一切都应在一种模式中进行,例如用户:如果用户/标本没有发现,则:核查(美元代码)和放弃一个例外。 成员:锡尔控制器、脂肪模型。 模型也更容易测试。





相关问题
Django m2m and saving objects

I have a couple of simple objects that have a many-to-many relationship. Django has joined them using obj1_obj2 table and it looks like this in mysql; id | person_id | nationality_id ---------...

Objective-C creating a text file with a string

I m trying to create a text file with the contents of a string to my desktop. I m not sure if I m doing it right, I don t get errors but it doesn t work either... NSArray *paths = ...

Save/Load Properties to file or database provider

I need to save and load properties of a Class dynamicly, but what is the best practis for this ? I have for now, two classes that I need to save. public abstract class BaseComponent { protected ...

flash write ByteArray to file on the disk

I need to write a ByteArray to a file on local disk with Flash AS3. The flashapplication is run locally (it s a projector exe). I found the FileReference class with the save() function which works ...

Saving output of a for-loop to file

I have opened a file with blast results and printed out the hits in fasta format to the screen. The code looks like this: result_handle = open("/Users/jonbra/Desktop/my_blast.xml") from Bio.Blast ...

热门标签