CAKEPHP1.3-where子句中的未知列
我正在处理一个已经存在的CakePHP1.3项目,我需要向数据库中添加一个新表。我的控制器中有:CAKEPHP1.3-where子句中的未知列,php,mysql,sql,cakephp,cakephp-1.3,Php,Mysql,Sql,Cakephp,Cakephp 1.3,我正在处理一个已经存在的CakePHP1.3项目,我需要向数据库中添加一个新表。我的控制器中有: $conditions = array('ShootingPlacement.person_id' => $id, 'Email.person_id' => $id, 'Email.shooting_placement_id' => 'ShootingPlacement.id'); $shootingPlacements = $this->ShootingPl
$conditions = array('ShootingPlacement.person_id' => $id, 'Email.person_id' => $id, 'Email.shooting_placement_id' => 'ShootingPlacement.id');
$shootingPlacements = $this->ShootingPlacement->find('all', compact('conditions'));
这给了我一个错误:
Warning (512): SQL Error: 1054: Unknown column 'Email.person_id' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
这就是它试图创建的查询:
SELECT `ShootingPlacement`.`id`, ... FROM `shooting_placements` AS `ShootingPlacement`
LEFT JOIN `people` AS `Person` ON (`ShootingPlacement`.`person_id` = `Person`.`id`)
LEFT JOIN `shootings` AS `Shooting` ON (`ShootingPlacement`.`shooting_id` = `Shooting`.`id`)
WHERE `ShootingPlacement`.`person_id` = 123688 AND `Email`.`person_id` = 123688 AND `Email`.`shooting_placement_id` = 'ShootingPlacement.id'
ORDER BY `lastname` ASC
显然,我的控制器代码是错误的,但我不确定如何将电子邮件表与ShootingPlacement表关联起来。我认为我的模型是正确的。到目前为止,如果我有这个:
$conditions = array('ShootingPlacement.person_id' => $id);
$shootingPlacements = $this->ShootingPlacement->find('all', compact('conditions'));
它将检索来自Shooting、ShootingPlacement和Person的行,我希望电子邮件也在那里。电子邮件有两个外键:一个来自ShootinPlacement,另一个来自Person
这些是模型,我创建的唯一一个是电子邮件,其余的工作正常
class Email extends AppModel
{
var $name = 'Email';
var $belongsTo = array
(
'Person' => array
(
'className' => 'Person',
'foreignKey' => 'person_id'
),
'ShootingPlacement' => array
(
'className' => 'ShootingPlacement',
'foreignKey' => 'shooting_placement_id'
)
);
}
class ShootingPlacement extends AppModel
{
var $name = 'ShootingPlacement';
var $belongsTo = array
(
'Person' => array
(
'className' => 'Person',
'foreignKey' => 'person_id',
'order' => 'lastname ASC'
),
'Shooting' => array
(
'className' => 'Shooting',
'foreignKey' => 'shooting_id'
)
);
}
class Person extends AppModel
{
var $name = 'Person';
var $belongsTo = array
(
'PersonOrigin' => array
(
'className' => 'PersonOrigin',
'foreignKey' => 'person_origin_id'
)
);
var $hasMany = array
(
'ShootingPlacement' => array
(
'className' => 'ShootingPlacement',
'foreignKey' => 'person_id',
'dependent' => false
)
);
}
class Shooting extends AppModel
{
var $name = 'Shooting';
var $belongsTo = array
(
'ShootingLocation' => array
(
'className' => 'ShootingLocation',
'foreignKey' => 'shooting_location_id'
),
'Emission' => array
(
'className' => 'Emission',
'foreignKey' => 'emission_id'
)
);
}
我需要在视图上循环ShootingPlacement变量,并且我需要它包含ShootingPlacement和Person的特定id的电子邮件表数据(正如您在查询中看到的,Person和ShootingPlacement已经处于关系中,我只需要有电子邮件)这将提供所需的连接:
$conditions = array('ShootingPlacement.person_id' => $id, 'Email.person_id' => $id, 'Email.shooting_placement_id' => 'ShootingPlacement.id');
$joins = array(
array(
'table' => 'emails',
'alias' => 'Email',
'type' => 'LEFT',
'conditions' => array('Email.shooting_placement_id = ShootingPlacement.id')
)
);
$shootingPlacements = $this->ShootingPlacement->find('all',
array(
'conditions' => $conditions,
'joins' => $joins
)
);
你需要将你的模型拍摄位置与你称之为“电子邮件”的链接
class ShootingPlacement extends AppModel
var $name = 'Shooting';
var $hasMany= array
(
'Email' => array
(
'className' => 'Email',
'foreignKey' => 'yourfk'
),
);
}
并且使用它是非常强大的可容纳行为
例如:
$contain=array('Email'=>array('fields'=>array('id','...')));
$conditions=array('ShootingPlacement.id'=>$yourId);
$this->ShootingPlacement->attachBehaviros('Containable');
$this->ShootingPlacement->find('all',$conditions);// your will retrieve yoru SHootingItem + Emails linked
在模型中添加可包含的行为
class Email extends AppModel {
var $name = 'Email';
var $actsAs = array('Containable');
var $belongsTo = array
(
'Person' => array
(
'className' => 'Person',
'foreignKey' => 'person_id'
),
'ShootingPlacement' => array
(
'className' => 'ShootingPlacement',
'foreignKey' => 'shooting_placement_id'
)
);
}
只需在控制器中编写以下代码
$this->ShootingPlacement->recursive = 2;
$this->ShootingPlacement->contain = array(
'Shooting',
'Person' => array(
'Email'
)
);
$conditions = array(
'ShootingPlacement.person_id' => $id,
'Email.shooting_placement_id' => 'ShootingPlacement.id'
);
$shootingPlacements = $this->ShootingPlacement->find('all', compact('conditions'));
希望这对您有所帮助。使用下面的电子邮件添加$hasOne人际关系模型
var $hasOne = array(
'Email' => array(
'className' => 'Email',
'foreignKey' => 'person_id' // Column defined for person ids in Email table
)
);
然后加上
$this->ShootingPlacement->recursive = 2;
或
您可以简单地使用cakephp中的连接来连接电子邮件模型。参考你应该非常小心你所追求的关系。快速浏览一下其中的一些答案,他们似乎建议您只需将电子邮件模型的连接添加到您的个人模型中,并根据查找条件确保您的查询不会占用服务器的内存 我将假设,首先,您希望在对Person的所有查询中都隐含此电子邮件关系,否则您可以简单地在每个查询中指定所需的联接。在这种情况下,您肯定希望使用链接来链接它 您的代码显示Shooting和ShootingPlacement(假定这是一个模型到模型的映射关系)都属于两个模型。顺便说一句,拍摄
属于发射-我们在这里还没有看到。我假设这不适用于当前场景
现在,让我们假设一下坏的情况,因为您的电子邮件表有外键,所以它将是一个hasOne
关系,而不是hasMany
——所以您需要通过它来链接它。我将把它链接到ShootingPlacement模型,因为这是您要查询的模型,所以它应该是模型围绕它连接的中心点。从结构上看,因为一切似乎都源于你的人物模型,所以我建议你改为查询那个模型。但是,到目前为止,它的设置方式将允许您从几乎任何地方进行查询,并且仍然可以检索几乎相同的结果,除了一些模型名和表别名
纯粹是因为电子邮件和ShootingPlacement之间的外键具有不同的名称,而CakePHP 1.3不能很好地处理这个问题,我还建议您不要使用外键,而是将其作为条件放入关系中
class ShootingPlacement extends AppModel
{
var $name = 'ShootingPlacement';
var $actsAs = array('Containable');
var $hasOne = array(
'Email' => array(
'className' => 'Email',
'foreignKey' => false,
'conditions' => array(
'Email.shooting_placement_id = ShootingPlacement.id',
'Email.person_id = ShootingPlacement.person_id'
)
)
);
var $belongsTo = array (
'Person' => array (
'className' => 'Person',
'foreignKey' => 'person_id',
'order' => 'lastname ASC'
),
'Shooting' => array (
'className' => 'Shooting',
'foreignKey' => 'shooting_id'
)
);
}
我还添加了可控制的行为。这允许您从每个查询中控制要与主模型结果一起返回的关联模型。它默认为all,但当您只需要特定的内容和/或出于内存原因时(如果您不限制这些查询或只指定要返回的字段名,这些类型的查询可能会很快破坏您的服务器内存),它会很方便
现在,当你创建你的电子邮件模型时,我不会建议通过再次将其链接回ShootingPlacement来进一步复杂化这些混乱的模型。正如您所说,它还具有Person模型的外键。因此,您可能希望对Person模型执行与上述完全相同的操作(当然,更改条件以反映Person外键)。这样你的模型更灵活一点;它仍将加入ShootingPlacement和Person,并允许您在需要时单独查询它,而无需其他关联模型
文件
另见
-1,这是此特定实例的修复程序,但无法解决根本问题。参考模型关系。感谢您的解释和链接,这对理解其工作原理有很大帮助