Forms cakephp 3.0注册与现有用户问题关联的帐户

Forms cakephp 3.0注册与现有用户问题关联的帐户,forms,associations,cakephp-3.0,model-validation,Forms,Associations,Cakephp 3.0,Model Validation,Accounts表具有引用用户表id的外键用户id 我一直在尝试完成AccountsController的添加功能。当前,如果记录是不存在的用户,它可以将其添加到“我的帐户”表和关联的“用户”表中。当我试图将帐户添加到现有的用户错误时,问题就出现了。我在主键为[NULL]的表accounts中找不到记录;更多信息如下。 我之所以有一个用户表,是因为它将包含通过提供电子邮件订阅的公共用户;没有帐户和注册用户注册用户将拥有帐户。 我认为出现此问题的原因是电子邮件在UsersTable规则中不是唯一的

Accounts表具有引用用户表id的外键用户id

我一直在尝试完成AccountsController的添加功能。当前,如果记录是不存在的用户,它可以将其添加到“我的帐户”表和关联的“用户”表中。当我试图将帐户添加到现有的用户错误时,问题就出现了。我在主键为[NULL]的表accounts中找不到记录;更多信息如下。 我之所以有一个用户表,是因为它将包含通过提供电子邮件订阅的公共用户;没有帐户和注册用户注册用户将拥有帐户。 我认为出现此问题的原因是电子邮件在UsersTable规则中不是唯一的

会计控制员 add.ctp帐户 上面的工作并报告键入的地址

AccountsController在出现错误时报告电子邮件主键约束 这不起作用,给了我一个错误: 注意8:未定义的索引:user.email[APP/Controller\accountscocontroller.php,第68行]

我想获取user.email,这样我就可以在我的Users表中搜索该行,并检索它的主键,以插入我试图创建的帐户的用户id

表的sql 编辑3:我找到了从表单中检索关联数据的方法,或者更准确地说,修补的实体$account是通过$account->user->email。 我有进展了,但为现有的公共用户注册帐户是一种相当肮脏的方式

AccountsController已更新 当前的问题是通过查询生成器将用户id输入我的帐户行。目前,它成功地更改了我的用户的用户类型,但后来当我尝试在Accounts表中插入一行时,我得到了这个结果。错误:SQLSTATE[21000]:基数冲突:1241个操作数应包含1列

此外,sql错误是->将用户id、密码、电话号码、地址值插入帐户选择Users.usertype作为用户类型,Users.id作为用户id从用户中选择,其中email=:c0和email=:c1和email=:c2、:c3、:c4、:c5

我不知道为什么它要把两列放到一个字段中。非常感谢你的帮助。在写这篇文章时,我意识到有3个查询执行到这一点,其中有一个电子邮件条件。似乎查询已连接

AccountsController已更新2 公共功能添加 {


新功能将使用提供的电子邮件删除用户,其中用户类型是公共的;由于电子邮件是唯一的,它将只删除1个。然后,它将像默认的添加功能一样正常工作,并编辑新用户的或删除后再添加到客户端的用户类型。

收到错误时,请始终显示完整的错误消息,包括我无法访问stacktrace!这不是一个正确的问题描述,它可能意味着什么,如果你尝试了什么,那么请将代码添加到你的问题中,并解释到底出了什么问题。另一方面,谁在添加这些帐户,用户自己?如果是的话,他们当时是否已经通过身份验证/登录?没有o为用户登录。用户唯一可用的功能是添加他们的详细信息以订阅时事通讯。将为帐户提供登录功能。为了澄清我的问题,当帐户添加功能完成时,还没有要实现的登录功能。我在尝试从我的帐户表单请求user.email数据时遇到的错误is通知8:未定义索引:user.email[APP/Controller\accountscocontroller.php,第68行]
    public function add()
    {
        $account = $this->Accounts->newEntity();
        if ($this->request->is('post')) {
            $account = $this->Accounts->patchEntity($account, $this->request->data);
            if ($this->Accounts->save($account)) {
                $this->Flash->success('The account has been saved.');
                return $this->redirect(['action' => 'index']);  
            }else {
                $this->Flash->error('The account could not be saved. Please, try again.');
            }
       }
        $users = $this->Accounts->Users->find('list', ['limit' => 200]);
        $this->set(compact('account', 'users'));
        $this->set('_serialize', ['account']);
    }
        <?= $this->Form->create($account); ?>
        <fieldset>
            <legend><?= __('Add Account') ?></legend>
            <?php
                echo $this->Form->input('user.name');
                echo $this->Form->input('user.email');
                echo $this->Form->input('password');
                echo $this->Form->input('phonenumber');
                echo $this->Form->input('address');
                echo $this->Form->input('user.postcode');
            ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>
    </div>
 public function add()
    {
        $account = $this->Accounts->newEntity();
        if ($this->request->is('post')) {
            $account = $this->Accounts->patchEntity($account, $this->request->data);
            if ($this->Accounts->save($account)) {
                $this->Flash->success('The account has been saved.');
                return $this->redirect(['action' => 'index']);          
            }else {
                $this->Flash->error($this->request->data['address']);
            } 

        }
        $users = $this->Accounts->Users->find('list', ['limit' => 200]);
        $this->set(compact('account', 'users'));
        $this->set('_serialize', ['account']);
    }
 public function add()
    {
        $account = $this->Accounts->newEntity();
        if ($this->request->is('post')) {
            $account = $this->Accounts->patchEntity($account, $this->request->data);
            if ($this->Accounts->save($account)) {
                $this->Flash->success('The account has been saved.');
                return $this->redirect(['action' => 'index']);          
            }else {
                $this->Flash->error($this->request->data['user.email']);
            } 

        }
        $users = $this->Accounts->Users->find('list', ['limit' => 200]);
        $this->set(compact('account', 'users'));
        $this->set('_serialize', ['account']);
    }
 CREATE TABLE users (
          id INT AUTO_INCREMENT PRIMARY KEY,
          name VARCHAR(255),
          email VARCHAR(255),
          postcode VARCHAR(255),
          usertype VARCHAR(20) DEFAULT 'Public',
          created DATETIME,
          updated DATETIME
    );

CREATE TABLE accounts (
      id INT AUTO_INCREMENT PRIMARY KEY,
      user_id INT NOT NULL,
      password VARCHAR(255),
      phonenumber VARCHAR(10),
      address VARCHAR(255),
      created DATETIME,
      updated DATETIME,         
      FOREIGN KEY user_key (user_id) REFERENCES users(id)
);
public function add()
    {

        $account = $this->Accounts->newEntity();
        if ($this->request->is(['post', 'patch', 'put'])) {
            $account = $this->Accounts->patchEntity($account, $this->request->data);

            if ($this->Accounts->save($account)) {
                $users = $this->Accounts->Users;
                $query = $users->query();
                $query->update()
                    ->set([
                        'usertype'=>'Client'
                    ])
                    ->where(['email' => $account->user->email])
                    ->execute();
                $this->Flash->success('The account has been saved.');
                return $this->redirect(['action' => 'index']);          

            }else {
                $users = $this->Accounts->Users;
                $query = $users->query();
                $usertype = $query->select('usertype')
                    ->where(['email' => $account->user->email]);

                    if(true){
                        $query->update()
                            ->set(['name'=>$account->user->name,
                                'postcode'=>$account->user->postcode,
                                'usertype'=>'Client'
                                ])
                            ->where(['email' => $account->user->email])
                            ->execute();
//everything up to this point works

                        $userid = $query->select('id')
                            ->where(['email' => $account->user->email]);

                        $accounts = $this->Accounts;
                        $query = $accounts->query();
                        $query->insert(['user_id', 'password', 'phonenumber', 'address'])
                            ->values([
                                'user_id' => $userid,
                                'password' => $this->request->data['password'],
                                'phonenumber' => $this->request->data['phonenumber'],
                                'address' => $this->request->data['address']
                            ])
                            ->execute();


                        $this->Flash->success('The account has been saved.');
                        return $this->redirect(['action' => 'index']);          
                    }
                    else{
                        $this->Flash->error('The account could not be saved.');
                    }
            } 

        }
        $users = $this->Accounts->Users->find('list', ['limit' => 200]);
        $this->set(compact('account', 'users'));
        $this->set('_serialize', ['account']);
    }
    $account = $this->Accounts->newEntity();
    if ($this->request->is(['post'])) {
        $account = $this->Accounts->patchEntity($account, $this->request->data);
        $users = $this->Accounts->Users;
        $query = $users->find();    
        $query->delete()
            ->where(['email' => $account->user->email])->where(['usertype' => 'Public'])
            ->execute();
        //the above query will delete a user if the email provided matches and their usertype is public
        //subsequently, this code will delete and add a user; not update user. ie. their id will change.    
        if ($this->Accounts->save($account)) {
            $users = $this->Accounts->Users;
            $query = $users->query();
            $query->update()
                ->set([
                    'usertype'=>'Client'
                ])
                ->where(['email' => $account->user->email])
                ->execute();
            $this->Flash->success('The account has been saved.');
            return $this->redirect(['action' => 'index']);          

        }else {
            $this->Flash->error('The account could not be saved.');
        }
    }
    $users = $this->Accounts->Users->find('list', ['limit' => 200]);
    $this->set(compact('account', 'users'));
    $this->set('_serialize', ['account']);
}