首页 文章

如何定义与cakePHP的关系?

提问于
浏览
0

我已经阅读了关于对流的内容,并且我的应用程序/数据库是直接的 .

我也读过$ hasOne,$ hasMany,$ belongsTo等等

但是,我正面临一个我无法为自己解决的问题/问题 .

我的车型:汽车,活动,加油,维修
我的控制器:CarsController

我的关系:

class Car extends AppModel {
    public $BelongsTo = array('User');
    public $hasMany = array('Event');
}

.

class Event extends AppModel {
    public $BelongsTo = array('Car');
    public $hasOne = array('Refuel');
}

.

class Refuel extends AppModel {
    public $BelongsTo = array('Event');
}

Output of find() 来自CarsController

array(
    'Car' => array(
        'id' => '1',
        'user_id' => '1',
        'make' => 'Make',
        'model' => 'Model',
    ),
    'Event' => array(
        (int) 0 => array(
            'id' => '1',
            'car_id' => '1',
            'dateEvent' => '20-10-2014',
            'description' => '1'
        ),
        (int) 1 => array(
            'id' => '2',
            'car_id' => '1',
            'dateEvent' => '20-10-2014',
            'description' => '2'
        ),
        (int) 2 => array(
            'id' => '3',
            'car_id' => '1',
            'dateEvent' => '20-10-2014',
            'description' => '3'
        )
    )
)

You need to know this: 汽车属于用户 . 用户可能有很多车
汽车有很多活动 . 一个活动只有一辆车 .
每个事件应该与一个加油或一个维修相关联 . 加油或维修不能关联多个事件 .

Tables:

用户:id
cars:id,user_id
事件:id,car_id
加油:id,event_id(唯一)
修理:id,event_id(唯一)

  • 我是否定义了很好的关系?

  • 如何在cakePHP中表达它,特殊事件 - 加油和事件修复?

2 回答

  • -1

    你错过了

    class User extends AppModel {
        public $hasMany = array('Car');
    }
    
  • 0

    我发现数组('recursive'=> 2)解决了这个问题!

    但是这是什么以及为什么?

相关问题