将数据从CakePHP 2.5.1中的一个表单保存到多个表中

时间:2016-01-18 17:06:10

标签: mysql cakephp cakephp-2.x

我是CakePHP的新手,使用CakePHP 2.5.1和MySQL开发云应用程序。我有以下表格:

users

  user_id ,
  people_id(FK- people.people_id),
  username,
  password

people

  people_id, 
  manager_id(FK- people.people_id), 
  firsr_name,
  last_name,

roles

  role_id ,
  people_id (FK- people.people_id),
  role

addresses

  address_id,
  people_id (FK- people.people_id),
  street,
  house no,
  post code,
  city

外键people.manager_id指的是primary_key people.people_id。字段roles.role可以包含'manager''customer'

现在,在我的注册表单中,将有以下输入字段:

-username
-password
-first name
-last name
-street
-house no
-post code
-city
-role     

我使用了用户模型(User.php),注册表单(add.ctp)和控制器(UsersController.php)来输入用户名和密码;并实施登录,注销。

查看 add.ctp

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Registrierung'); ?></legend>

        <?php echo $this->Form->input('username');

              echo $this->Form->input('password');
              echo $this->Form->input('password_confirm', array('label' => 'Confirm Password', 'maxLength' => 255, 'title' => 'Confirm password', 'type'=>'password'));


        );  
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Register')); ?>
</div>

工作正常。现在我需要在注册表单中包含上面的输入字段。这意味着为每个表创建模型并在我的belogsTo模型中定义hasManyUser等关联,我已经定义了以下关联:

class User extends AppModel {

    public $belongsTo = array(
            'Person' => array(
                'className' => 'Person',
                'foreignKey' => 'people_id'

            )
    );
}

但是,它会引发以下错误:

Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Person.id' in 'on clause' 

有人能告诉我如何实现这些关联吗?我找不到符合我要求的任何例子。

2 个答案:

答案 0 :(得分:1)

MySQL错误是由您命名主键的方式引起的。在CakePHP中,主键应命名为$primaryKey

如果您想使用其他名称(不推荐),您必须告诉CakePHP您将使用的名称。这样做声明了class Person extends AppModel { public $primaryKey = 'people_id'; } 属性。例如:

people_id

但是,最好重命名主键并尽可能遵守CakePHP约定。

此外,通过CakePHP处理复数的方式,您的person_id字段应重命名为Person

编辑:添加关系

您的关系如下。

您的class Person extends AppModel{ public $belongsTo = array( 'Manager' => array( 'className' => 'Person', 'foreignKey' => 'manager_id', ), ); public $hasMany = array( 'User', 'Role', 'Address', 'Subordinate' => array( 'className' => 'Person', 'foreignKey' => 'manager_id', ), ); } 班级:

class User extends AppModel{

    public $belongsTo = array(
        'Person'
    );
}

class Role extends AppModel{

    public $belongsTo = array(
        'Person'
    );
}

class Address extends AppModel{

    public $belongsTo = array(
        'Person'
    );
}

您的其他课程:

people_id

请务必将所有外键从person_id更改为Role

模型roles.person_id

中的修改

我认为您可能希望将字段people.role_id移至class Person extends AppModel{ public $belongsTo = array( 'Manager' => array( 'className' => 'Person', 'foreignKey' => 'manager_id', ), 'Role' ); public $hasMany = array( 'User', 'Address', 'Subordinate' => array( 'className' => 'Person', 'foreignKey' => 'manager_id', ), ); } 。恕我直言,这更有意义。

您的关系将变为:

class Role extends AppModel{

    public $hasMany = array(
        'Person'
    );
}

{{1}}

答案 1 :(得分:0)

您可以在每个模型中明确定义主键,但这会使它变得非常复杂。

我建议你坚持惯例。请将所有主键字段更改为&#34; id&#34;。这可能解决了你的大部分问题。

和平!的xD

相关问题