php - CakePHP 1.3 - where 子句中的未知列

标签 php mysql sql cakephp cakephp-1.3

我正在处理一个已经存在的 cakephp 1.3 项目,我需要向数据库中添加一个新表。我的 Controller 中有这个:

    $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  

显然我的 Controller 代码是错误的,但我不确定如何将电子邮件表与 ShootingPlacement 表相关联。我认为我的模型是正确的。到目前为止,如果我有这个:

    $conditions = array('ShootingPlacement.person_id' => $id);
    $shootingPlacements = $this->ShootingPlacement->find('all', compact('conditions'));

它将从 Shooting、ShootingPlacement 和 Person 中检索行,我希望 Email 也在那里。电子邮件有 2 个外键:一个来自 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'
        )
    );
}

我在 View 中需要的是遍历 ShootingPlacement 变量,我需要它包含 ShootingPlacement 和 Person 的特定 ID 的电子邮件表数据(正如您在查询中看到的,Person 和 ShootingPlacement 已经处于关系中, 我也只要有Email即可)

最佳答案

你应该非常小心你所追求的关系。快速浏览其中一些答案,他们似乎建议您只需将电子邮件模型的连接添加到您的人员模型中,并依赖于您的查找条件来确保您的查询不会占用服务器的内存。

我假设首先,您希望此电子邮件关系隐含在您对 Person 的所有查询中,否则您可以简单地指定每个查询的连接.在这种情况下,您肯定希望使用 model relationships 链接它。 .

您的代码显示 Shooting 和 ShootingPlacement(假设这是一个模型到模型的映射关系)都属于两个模型。顺便说一句,Shooting belongsTo Emission - 我们在这里还没有看到。我认为这不适用于当前情况。

现在,让我们假设因为您的 Email 表有外键,所以它将是一个hasOne 关系,而不是一个hasMany - 这就是您需要链接的内容。我要把它链接到 ShootingPlacement 模型,因为这是您要查询的模型,所以它应该是围绕它连接模型的中心点。结构明智,因为一切似乎都源自您的 Person 模型,我不得不建议您改为查询 that 模型。但到目前为止,它的设置方式将允许您从几乎任何地方进行查询,并且仍然检索大部分相同的结果,除了一些模型名称和表别名。

纯粹是因为你的Email和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'
        )
    );
}

我还在其中添加了可包含行为。这允许您从每个查询中控制您希望将哪些关联模型与您的主要模型结果一起返回。它将默认为全部,但是当您只需要特定的东西和/或出于内存原因时会很方便(如果您不限制它们或仅指定您想要的字段名称,这些类型的查询会很快破坏您的服务器内存返回)。

现在,当您创建电子邮件模型时,我不建议通过再次将其链接回 ShootingPlacement 来进一步复杂化这一团困惑的模型。正如您所说,它还有一个指向 Person 模型的外键。所以你可能想为你的 Person 模型做与上面完全相同的事情(当然改变条件以反射(reflect) Person 外键)。这样你的模型就更灵活了;它仍然会连接到 ShootingPlacement Person,如果需要,您还可以在没有其他关联模型的情况下单独查询它。

Documentation

See also

关于php - CakePHP 1.3 - where 子句中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20262620/

相关文章:

sql - 防止 postgres 内联更新子查询

sql - 如何在不使用 to_date/to_char 函数的情况下比较 oracle sql 中的日期?

javascript - 如何在特定的 WordPress 页面上运行 JavaScript 函数?

PHP分页类

java - 使用play-framework 2.5连接MySQL数据库

php - 从MySQL中检索两级分层数据的最佳方法

php - 如何在 wordpress 中获取下一篇/上一篇文章的 hrefs 和标题

php - SQL 查询可在测试服务器上运行,但不能实时运行……有什么区别?

mysql - 使用 sql 审核 .net 应用程序

SQL 和 VB.NET : SELECT query using data in textbox