有人可以为我提供解释吗?
我有一个数据库,我试图从一个页面,组织,组织详细信息,帐户,AccountRoles更新4个表。添加视图本身将包含帐户和组织的字段。当我调用OrganizationsController添加函数时,我想在AccountController,AccountRolesController(将Account设置为管理员角色)和OrganizationDetailsController(将该帐户设置为该特定组织的管理员)上调用add。
我应该使用组件吗?当我在组织上调用add()时,我总是打算执行这些步骤 - 从不单独创建下面的组织条目。我应该直接在该函数或其他方法中执行此操作吗?
如果我需要解释更多,请告诉我,谢谢!
编辑:添加以下四种型号: 组织
class Organization extends AppModel {
public $hasMany = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'organization_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasOne = array(
'OrganizationDetail' => array(
'className' => 'OrganizationDetail',
'foreignKey' => 'organization_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
OrganizationDetail
class OrganizationDetail extends AppModel {
public $belongsTo = array(
'Organization' => array(
'className' => 'Organization',
'foreignKey' => 'organization_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
AccountRole
class AccountRole extends AppModel {
public $belongsTo = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
帐户
class Account extends AppModel {
public $belongsTo = array(
'Organization' => array(
'className' => 'Organization',
'foreignKey' => 'organization_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasOne = array(
'OrganizationDetail' => array(
'className' => 'OrganizationDetail',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasMany = array(
'AccountRole' => array(
'className' => 'AccountRole',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
答案 0 :(得分:0)
你错了整个MVC的想法。控制器不应该调用其他控制器中的方法(或者至少不在这种情况下)。
正确的方法是设置OrganizationsController-> add(),以便通过对每个Model-> save($ data)的独立调用或使用
来保存所有数据Organization->saveAssociated($data,array('deep' => true))
您的表单字段需要正确命名才能生效。例如:
echo $this->Form->create('Organization', array('action' => 'add'));
echo $this->Form->input('Organization.name'));
echo $this->Form->input('Account.0.name', array('label' => 'Account name'));
echo $this->Form->input('OrganizationDetail.details', array('label' => 'Org. Details'));
echo $this->Form->input('Account.0.AccountRole.id', array('label' => 'Account Role'));
echo $this->Form->end('Add');
但是,如果你试图深入太多,你可能会遇到麻烦。如果您发现自己处于这种情况,可能是因为您的数据模型存在缺陷。
这就是您的数据模型目前的样子:
我认为应该是这样的:
在开始编码之前准备好数据模型至关重要。它可以为您节省许多麻烦!