CakePHP中的关系问题

时间:2013-08-22 06:41:05

标签: cakephp cakephp-2.0

有两个模型是项目和活动。

以下是关系:

var $belongsTo = array(
    'Project' => array(
        'className' => 'Project',
        'foreignKey' => 'project_id'

    ),

var $hasMany = array(
    'Event' => array(
        'className' => 'Event',
        'foreignKey' => false,
        'dependent' => false,

    ),

问题在于,每当我尝试列出所有项目时,只列出附有事件的项目。 我期待着解决这个问题。

以下是我得到的查询:

  

选择EventidEventproject_idEventuser_idEvent。{{1} },dateEventhoursEventminutesEventxhoursEventxminutesEventxdetailsEventassignmentEventstart_timeProject,{{ 1}}。idProjectnameProjectcustomer_idProjectproject_nr,{{1} }。ProjectaddressProjectpost_nrProjectcityProjectcompany_idUseridUseremployee_nrUsernameUsersurname。{{ 1}},UseremailUserpasswordUserroleUser。{{1} },phoneUseraddressUserpost_nrUser FROM cityUser AS token_hash LEFT JOIN Usercompany_id AS scheduling ON(eventsEvent = schedulingprojects) LEFT JOIN ProjectEvent AS project_id ON(Projectid = schedulingusers)WHERE 1 = 1 LIMIT 15

我在上次更新后获得的查询如下:

  

选择UserEventuser_idUseridEventid。{{1} },Eventproject_idEventuser_idEventdateEventhoursEventminutesEventxhoursEventxminutesEventxdetails,{{ 1}}。EventassignmentEventstart_timeEventmaterial_idEvent,{{1} }。km_driveEventtime_driveEventfinish_timeProjectidProjectnameProjectcustomer_idProjectproject_nrProjectaddressProject。{{ 1}},post_nrProjectcityProjectcompany_idProjectcolor。{{1} },UseridUseremployee_nrUsernameUsersurnameUseremailUserpasswordUserroleUserphone,{{ 1}}。User FROM addressUser AS post_nr LEFT JOIN Usercity AS User ON({{ 1}}。token_hash = Usercompany_id)LEFT JOIN Usercar_id AS scheduling ON(events。{ {1}} = Eventscheduling)WHERE 1 = 1 LIMIT 20

这是我在尝试列出项目时获得的数组:

projects

所以基本上我得到了相关项目的事件列表,没有相关的客户。我想得到的是这样的数组:

Project

1 个答案:

答案 0 :(得分:1)

活动模型

class Event extends AppModel {
     public $actsAs = array('Containable');

    var $belongsTo = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'project_id'
        ),
        // ... more relationships
    );

项目模型

class Project extends AppModel {
    public $actsAs = array('Containable');

    var $hasMany = array(
        'Event' => array(
            'className' => 'Event',
            'foreignKey' => 'project_id'
            'dependent' => false,
        ),
        // ... more relationships
    );

项目控制器

class ProjectsController extends AppController {

    public $paginate = array(
        'Project' => array(
            'contain' => array(
                 'Event',
                 'User'
             ) // end Project contain
         ) // end Project pagination
     ); // end pagination

     public function index() {
         $this->set('projects', $this->paginate());
     }