【发布时间】:2013-08-10 18:24:36
【问题描述】:
CakePHP 等新手,我对以下问题感到非常困惑,因此非常感谢任何指导。从本质上讲,我在 hasAndBelongsToMany 关系方面遇到了问题,我不知道我是否正确处理它,因为我在一个控制器和一个模型中完成大部分工作。
我有一个客户的页面,客户有很多工作(可行),客户属于一种客户类型(可行),客户也有许多案例研究(也可行),客户有工作(很好)。
作业有并且属于许多学科——这不起作用,但是,查询似乎正在运行(调试模式下的 SQL 输出显示了这一点,所以我将 SQL 直接运行到 MySQL 中——它查询正常) 但 Cake 并没有将数据提供给 clients 数组。
这是我的客户端模型和控制器的代码。
Client.php(模型)
public $belongsTo = array(
'ClientType' => array(
'className' => 'ClientType',
'foreignKey' => 'type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'CaseStudy' => array(
'className' => 'CaseStudy',
'foreignKey' => 'main_contractor',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Job' => array(
'className' => 'Job',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => 'Job.id desc'
)
);
ClientsController.php(控制器)
$options['joins'] = array(
array('table' => 'case_studies',
'alias' => 'CaseStudy',
'type' => 'LEFT',
'conditions' => array(
'CaseStudy.client_id = Client.id',
)
),
array('table' => 'jobs',
'alias' => 'Job',
'type' => 'LEFT',
'conditions' => array(
'Job.client_id = Client.id',
)
),
array('table' => 'sectors',
'alias' => 'Sector',
'type' => 'LEFT',
'conditions' => array(
'Job.sector_id = Sector.id',
)
),
array('table' => 'disciplines_jobs',
'alias' => 'DisciplinesJobs',
'type' => 'LEFT',
'conditions' => array(
'Job.id = DisciplinesJobs.job_id',
)
),
array(
'table' => 'disciplines',
'alias' => 'Discipline',
'type' => 'LEFT',
'conditions' => array(
'DisciplinesJobs.discipline_id = Discipline.id'
)
)
);
$options['conditions'] = array('Client.id' => $client_id);
$clients = $this->Client->find('all', $options);
上面$clients数组的输出:
array(
(int) 0 => array(
'Client' => array(
'id' => '47',
'type_id' => '2',
'name' => 'Balfour Beatty',
'logo' => '1361786198_thumbnail_balfour beatty.jpg',
'website_url' => 'http://www.google.com',
'date_added' => '2013-02-25 10:56:38',
'date_modified' => '2013-02-25 10:56:38'
),
'ClientType' => array(
'id' => '2',
'name' => 'Constructors'
),
'CaseStudy' => array(
(int) 0 => array(
'id' => '23',
'client_id' => '47',
'sector_id' => '1',
'name' => 'Shoreham Academy',
'header_image' => '1365088787_thumbnail_1365088787_header copy.jpg',
'main_contractor' => '47',
'architect' => 'Architecture PLB',
'project_value' => '565000',
'scope_of_works' => '<table></table>',
'text' => '<p><</p>',
'type' => 'flooring',
'date_added' => '2013-04-04 11:19:47',
'date_modified' => '2013-04-04 11:19:47'
)
),
'Job' => array(
(int) 0 => array(
'id' => '1',
'client_id' => '47',
'sector_id' => '2',
'project' => 'Shoreham Academy (Project not case study)',
'date' => '2012-10-19',
'cost' => '£416k',
'quantity_of_flooring' => '7000m',
'date_added' => '2013-08-06 21:46:59',
'date_modified' => '2013-08-06 21:47:01'
)
)
)
请注意,上面的 $clients 数组没有任何来自学科工作表的学科数据,但 SQL 输出是成功运行的:
SELECT `Client`.`id`, `Client`.`type_id`, `Client`.`name`, `Client`.`logo`, `Client`.`website_url`, `Client`.`date_added`, `Client`.`date_modified`, `ClientType`.`id`, `ClientType`.`name`
FROM `ar_flooring`.`clients` AS `Client`
LEFT JOIN `ar_flooring`.`case_studies` AS `CaseStudy` ON (`CaseStudy`.`client_id` = `Client`.`id`)
LEFT JOIN `ar_flooring`.`jobs` AS `Job` ON (`Job`.`client_id` = `Client`.`id`)
LEFT JOIN `ar_flooring`.`sectors` AS `Sector` ON (`Job`.`sector_id` = `Sector`.`id`)
LEFT JOIN `ar_flooring`.`disciplines_jobs` AS `DisciplinesJobs` ON (`Job`.`id` = `DisciplinesJobs`.`job_id`) LEFT JOIN `ar_flooring`.`disciplines` AS `Discipline` ON (`DisciplinesJobs`.`discipline_id` = `Discipline`.`id`)
LEFT JOIN `ar_flooring`.`client_types` AS `ClientType` ON (`Client`.`type_id` = `ClientType`.`id`)
WHERE `Client`.`id` = 47
我没有纪律模型或控制器。我没有作业模型或控制器,因为我正在尝试使用客户端模型和控制器来做所有事情 - 这是正确的吗?
有人知道为什么会这样吗?
希望这是有道理的。
干杯!
【问题讨论】:
-
您的问题中的 SQL 语句是 cakePHP SQL 输出还是您手动生成?
-
这是 cakePHP SQL 输出,Arash。
-
在查找函数
$this->client->recursive = -1之前试试这个 -
当你直接在mysql中尝试这个查询时,它的结果是ok的吗?
-
$this->client->recursive = -1 只返回客户端数据,不返回任何其他关系数据。当我运行输出查询时,所有连接都是正确的,但它没有作业字段,例如,生成的 SQL 输出丢失:Job.id、Job.name 等 - 但是,如果我手动添加这些,运行查询,结果现在显示正确,这意味着 LEFT JOIN 代码生成正确...
标签: php mysql cakephp relationships