如何组织CakePHP代码? [英] How to organize CakePHP code?

查看:90
本文介绍了如何组织CakePHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从CakePHP开始。我有非常简单的模型:

I'm starting with CakePHP. I have very simple models:


  • 用户

  • 公司
  • li>公司用户(通过用户和公司之间的关系)
  • Users
  • Companies
  • CompaniesUsers (a hasMany through relation between Users and Companies)

我创建了一个用户,在用户和公司之间没有任何关系,我被重定向到公司控制器的添加操作。

I create a user, and when I login as this user, if there isn't any relation between User and Companies, I'm redirected to the add action of the Companies controller. This is all done, and working well.

我的大问题是:当我保存公司时,我必须保存公司的数据和当前用户之间的关系和创建的公司。这一切都必须在一个事务内完成,因为我不想要孤儿公司。但我应该在哪里放这个代码?在Companies模型中创建save()函数并在保存后创建关系?在Companies模型中创建一个afterSave()函数?将所有代码放在CompaniesController的添加操作中?我不认为这最后一个选项是一个好主意,bussiness逻辑必须在模型中,不是吗?

My "big" problem is: When I save a company, I must save the company's data AND the relation between the current user and the created company. This all must be done inside a transaction, because I don't want "orphan" companies. But where should I put this code? Create a save() function in the Companies model and create the relation after saving? Create an afterSave() function in the Companies model? Put all the code in the CompaniesController's add action? I don't think this last option is a good idea, the bussiness logic must be in the model, isn't it?

我知道这必须是一个非常基本问题,但我是CakePHP和MVC的新手。

I know that this must be a very basic question, but I'm new to CakePHP and to MVC.

推荐答案


因为我不想要孤儿公司

This all must be done inside a transaction, because I don't want "orphan" companies



使用saveAssociated



CakePHP已经提供了一个完全符合你要求的函数: saveAssociated (也可通过 saveAll

Model :: saveAssociated(array $ data = null,array $ options = array())

Model::saveAssociated(array $data = null, array $options = array())

options数组有多个键,最相关:

The options array has multiple keys, of most relevance:


原子:如果为true(默认),将尝试保存所有记录单个事务。如果数据库/表不支持事务,则应设置为false。

atomic: If true (default), will attempt to save all records in a single transaction. Should be set to false if database/table does not support transactions.

因此,不需要任何额外的代码, code> saveAssociated 其中正在调用

As such there's no need for any extra code, simply call saveAssociated where right now save is being called.

确保保存的数据是 hasMany通过关系 - 如果有疑问,它应该是与包含相同数据的find相同的格式:

Ensure the saved data is the right format for a hasMany through relationship - if in doubt it should be the same format as a find containing the same data:

class ExampleController extends AppController {

    function example() {
        $data = array(
            'Company' => array(
                'name' => 'My Company'
            ),
            'CompaniesUser' => array(
                'user_id' => 'My User Id'
            )
        );

        $this->Company->saveAssociated($data);
    }

}

与给定用户的关系 - 或无法修改数据库中的任何内容。

This will either save the new company and the relation to the given user - or fail modifying nothing in the database.

这篇关于如何组织CakePHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆