Cake Bake - 在数据源中找不到模型GroupsUser的表groups_users

时间:2014-03-06 15:20:36

标签: php mysql cakephp

当我尝试烘焙名为DeploymentRequest的模型时,我收到以下错误:

Table groups_users for model GroupsUser was not found in datasource

如果我查看我的数据库结构,那么就不会与该连接表建立关系(也不需要存在)。我确实有模型组和用户作为ACL的请求者。

错误:

Baking test fixture for DeploymentRequest...

Creating file C:\wamp\apscmdb\app\Test\Fixture\DeploymentRequestFixture.php
Wrote `C:\wamp\apscmdb\app\Test\Fixture\DeploymentRequestFixture.php`
Bake is detecting possible fixtures...
Error: Table groups_users for model GroupsUser was not found in datasource defau
lt.
#0 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Model\Model.php(3498): Model->se
tSource('groups_users')
#1 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Model\Model.php(1355): Model->ge
tDataSource()
#2 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Model\Model.php(864): Model->sch
ema()
#3 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Model\Model.php(892): Model->__i
sset('GroupsUser')
#4 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\TestTask.ph
p(405): Model->__get('GroupsUser')
#5 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\TestTask.ph
p(396): TestTask->_processModel(Object(Group))
#6 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\TestTask.ph
p(396): TestTask->_processModel(Object(User))
#7 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\TestTask.ph
p(376): TestTask->_processModel(Object(DeploymentRequest))
#8 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\TestTask.ph
p(146): TestTask->generateFixtureList(Object(DeploymentRequest))
#9 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\ModelTask.p
hp(854): TestTask->bake('Model', 'DeploymentReque...')
#10 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Command\Task\ModelTask.
php(109): ModelTask->bakeTest('DeploymentReque...')
#11 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Shell.php(431): ModelTa
sk->execute()
#12 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\Shell.php(428): Shell->
runCommand('execute', Array)
#13 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\ShellDispatcher.php(207
): Shell->runCommand('model', Array)
#14 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\lib\Cake\Console\ShellDispatcher.php(66)
: ShellDispatcher->dispatch()
#15 C:\wamp\cakephp-2.4.5\cakephp-2.4.5\app\Console\cake.php(36): ShellDispatche
r::run(Array)
#16 {main}

数据库结构:

CREATE TABLE deployment_requests(
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT(10) UNSIGNED NOT NULL,
  environment_id INT(10) UNSIGNED NOT NULL,
  revision INT(10) UNSIGNED NOT NULL,
  is_scheduled TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  requested_deploy_date DATE DEFAULT NULL,
  requested_deploy_time TIME DEFAULT NULL,
  additional_instructions TEXT DEFAULT NULL,
  is_completed TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  created DATETIME DEFAULT NULL,
  modified DATETIME DEFAULT NULL,
  created_by INT(10) UNSIGNED DEFAULT NULL,
  modified_by INT(10) UNSIGNED DEFAULT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_unicode_ci;

CREATE TABLE groups(
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  name_eng VARCHAR(255) NOT NULL,
  name_fra VARCHAR(255) NOT NULL,
  description_eng TEXT NOT NULL,
  description_fra TEXT NOT NULL,
  created DATETIME DEFAULT NULL,
  modified DATETIME DEFAULT NULL,
  created_by INT(10) UNSIGNED DEFAULT NULL,
  modified_by INT(10) UNSIGNED DEFAULT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 4
AVG_ROW_LENGTH = 5461
CHARACTER SET utf8
COLLATE utf8_unicode_ci;

CREATE TABLE users(
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  group_id INT(10) UNSIGNED DEFAULT NULL,
  legal_given_names VARCHAR(255) NOT NULL,
  legal_family_name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  username VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 601943
AVG_ROW_LENGTH = 3276
CHARACTER SET utf8
COLLATE utf8_unicode_ci;

编辑#1

用户模型:

<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Group $Group
 * @property AssetDeploymentUser $AssetDeploymentUser
 * @property AssetGroupDeploymentUser $AssetGroupDeploymentUser
 * @property DeploymentRequest $DeploymentRequest
 * @property DeploymentSupportRequest $DeploymentSupportRequest
 */
class User extends AppModel {
    public $displayField = 'name';
    public $virtualFields = array(
            'name' => 'CONCAT(User.legal_given_names, " ", User.legal_family_name)'
    );

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['group_id'])) {
            $groupId = $this->data['User']['group_id'];
        } else {
            $groupId = $this->field('group_id');
        }
        if (!$groupId) {
            return null;
        } else {
            return array('Group' => array('id' => $groupId));
        }
    }

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'legal_given_names' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'legal_family_name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'AssetDeploymentUser' => array(
            'className' => 'AssetDeploymentUser',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'AssetGroupDeploymentUser' => array(
            'className' => 'AssetGroupDeploymentUser',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'DeploymentRequest' => array(
            'className' => 'DeploymentRequest',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'DeploymentSupportRequest' => array(
            'className' => 'DeploymentSupportRequest',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

小组模特:

<?php
App::uses('AppModel', 'Model');
/**
 * Group Model
 *
 * @property User $User
 * @property Asset $Asset
 */
class Group extends AppModel {
    public $displayField = 'name_eng';

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        return null;
    }


    //The Associations below have been created with all possible keys, those that are not needed can be removed


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
    public $hasAndBelongsToMany = array(
    'User' => array(
            'className' => 'User',
            'joinTable' => 'groups_users',
            'foreignKey' => 'group_id',
            'associationForeignKey' => 'user_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        ),
        'Asset' => array(
            'className' => 'Asset',
            'joinTable' => 'assets_groups',
            'foreignKey' => 'group_id',
            'associationForeignKey' => 'asset_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );

}

1 个答案:

答案 0 :(得分:1)

根据您的数据库,我相信Group拥有很多用户和用户所属的组。因此,从$ hasAndBelongsToMany属性中删除User数组,并在Group.php中添加以下内容:

public $hasMany = = array(
    'User' => array(
            'className' => 'User',
            'foreignKey' => 'group_id'
     )
);