0

私は CakePHP アプリケーションを持っていて、ログイン フォームとサインアップ フォームを同じページに配置したいと考えています。 -per-page-for-the-same-modelサインアップ部分は機能していましたが、ログイン部分が機能していません。次のエラーが表示されます。データソースのデフォルトで。」これは私が使用しているコントローラーです:

    class TblusersController extends AppController {

        public function signup() {
            $this->loadModel('Tbluser');
            $this->loadModel('Tbluserlogin');

            if (!empty($this->data)) {
                if (isset($this->data['Tbluser'])) { // Check if the signup Form was submitted

                   $this->Session->setFlash("SignUp Form was submitted.","notif");

                } else if (isset($this->data['Tbluserlogin'])) { // Check if the login Form was submitted
                    $this->Session->setFlash("Login Form was submitted.","notif");
                }

            }
        }
    }
?> 

私が使用しているモデルは次のとおりです。
Tbluser.php

 <?php

    class Tbluser extends AppModel{

        public $validate = array(
            'username'=>array(
                array(
                    'rule'=>'alphaNumeric',
                    'allowEmpty'=>false,
                    'message'=>'Invalide Username!'
                ),
                array(
                    'rule' => array('minLength', '4'),
                    'message' => 'Username has to be more than 3 chars'
                ),
                array(
                    'rule'=>'isUnique',
                    'message'=>'Username already taken!'
                )
            ),
            'password' => array(
                    array(
                        'rule' => 'alphaNumeric',
                        'allowEmpty'=>false,
                        'message' => 'Password must be AlphaNumeric!'
                    ),
                    array(
                        'rule' => array('minLength', '4'),
                        'message' => 'Username has to be more that 3 chars'
                    ),
                    array(
                        'rule' => array('confirmPassword', 'cakehashedpassword'),
                        'message' => 'Passwords do not match'
                    )), 
            'email'=>array(
                array(
                    'rule'=>array('email',true),
                    'required'=>true,
                    'allowEmpty'=>false,
                    'message'=>'Invalide email adress!'
                ),
                array(
                    'rule'=>'isUnique',
                    'message'=>'Mail adress already taken!'
                )
            )
        );
    }
    ?>   

Tbluserlogin.php モデル:

<?php 
class Tblforumuserlogin extends Tblforumuser{

}
?>      

私のビューファイルは「signup.ctp」です

<h4>Sign up</h4>    
<div><?php echo $this->Session->flash();?></div>
<?php                   

echo $this->Form->create("Tblforumuser", array('url' => '/Tblusers/signup')); 
echo $this->Form->input('username' ,array('label'=>'Username<b style="color:red;">')); 
echo $this->Form->input('password' ,array('label'=>'Password<b style="color:red;">','type' => 'password')); 
echo $this->Form->input('email' ,array('label'=>'Email<b style="color:red;">'));
echo $this->Form->end('Register');

?> 

<h4>Log in to Ohyeahhh</h4> 
<div><?php echo $this->Session->flash(); ?></div>
<?php echo $this->Form->create("Tbluserlogin", array('url' => '/Tblusers/signup'));  ?>
<?php echo $this->Form->input('username' ,array('label'=>"Username :")); ?>
<?php echo $this->Form->end('Login'); ?>               

ありがとうございました。

4

1 に答える 1

2

CakePHP の規則に従っていないため、CakePHP は使用する適切なデータベース テーブルを自動的に見つけることができません。

デフォルトでは、CakePHP モデルはデータベーステーブルにちなんで命名されるべきです。たとえば、テーブルfoos(複数形) の場合、モデルにはFoo(単数形) という名前を付ける必要があります。

規則に従っていない場合は、useTableプロパティを介してモデルに使用するテーブルを手動で指定する必要があります。

class Tblforumuserlogin extends Tblforumuser
{
    public $useTable = 'tblforumusers';
}

同じプロパティを使用すると、より「わかりやすい」モデル名を作成することもできます (つまり、データベース テーブルの名前を変更できない場合は、おそらくこれがより良い方法です)。

class User extends AppModel
{
    public $useTable = 'tblforumusers';
]

代替アプローチ

どちらのモデルも「どの」フォームが送信されたかを区別するためにのみ使用されるため、ここでは多くのオーバーヘッドが発生するようです。別のアプローチは、「通常の」モデルを使用することですが、アクションを別の「ログイン」アクションに向けます。

class TblusersController extends AppController
{
    public $uses = array(
        'Tbluser';
    );

    public function signup()
    {
        if ($this->request->is('post')) {
             // handle sign-up
        }
    }

    public function login()
    {
        if ($this->request->is('post')) {
             if ($this->Auth->login()) {
                 $this->redirect($this->Auth->redirectUrl);
             }
        }

        // Login failed or no form submitted
        return $this->redirect(array('action' => 'signup'));
    }
}

そしてあなたの見解の中で;

echo $this->Form->create("Tblforumuser"); 
echo $this->Form->input('username' ,array('label'=>'Username<b style="color:red;">')); 
echo $this->Form->input('password' ,array('label'=>'Password<b style="color:red;">')); 
echo $this->Form->input('email' ,array('label'=>'Email<b style="color:red;">'));
echo $this->Form->end('Register');


echo $this->Form->create("Tblforumuser", array('action' => 'login')); 

// etc...

echo $this->Form->end('Login');

さらに別のアプローチ

フォームに隠しフィールドを追加して、どのフォームが送信されたかを示すだけです。

echo $this->Form->create("Tblforumuser"); 
echo $this->Form->hidden('formsent', array('value' => 'register'));
// etc...
echo $this->Form->end('Register');


echo $this->Form->create("Tblforumuser"); 
echo $this->Form->hidden('formsent', array('value' => 'login'));
// etc...
echo $this->Form->end('Login');

そしてあなたのコントローラーの中に;

if ($this->request->is('post')) {
    if ('register' === $this->request->data['Tblforumuser']['formsent']) {
        // register
    } else {
        // login
    }
}
于 2013-05-11T15:55:45.933 に答える