编程语言
首页 > 编程语言> > CakePHP 1.3-where子句中的未知列

CakePHP 1.3-where子句中的未知列

作者:互联网

我正在一个已经存在的cakephp 1.3项目中,我需要向数据库中添加一个新表.我的控制器中有这个:

    $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  

显然我的控制器代码是错误的,但是我不确定如何将Email表与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'
        )
    );
}

我在视图上需要遍历ShootingPlacement变量,并且我需要它包含该ShootingPlacement和Person的特定ID的电子邮件表数据(如您在查询中所看到的那样,Person和ShootingPlacement已经存在关系,我只也需要有电子邮件)

解决方法:

您对所追求的关系应该非常小心.快速浏览其中的一些答案,它们似乎建议您仅将Email模型的联接添加到Person模型中,并依靠查找条件来确保查询不会破坏服务器的内存.

我将假设首先,您希望在Person的所有查询中都隐含此Email关系,否则您可以简单地在每个查询中指定要使用的联接.在这种情况下,您肯定要使用model relationships进行链接.

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

现在,让我们假设一个坏处,因为您的电子邮件表具有外键,它将是hasOne关系,而不是hasMany-因此您需要通过它进行链接.我将其链接到ShootingPlacement模型,因为这是您要查询的模型,因此它应该是围绕其连接模型的中心点.在结构上明智,因为一切似乎都源于您的Person模型,所以我建议您改为查询该模型.但是到目前为止,它的设置方式将使您可以从几乎任何地方查询,并且仍然可以检索几乎相同的结果栏以及一些模型名称和表别名.

纯粹是因为您的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模型执行与上述完全相同的操作(当然,更改条件以反映Person外键).这样,您的模型将更加灵活.它仍将与ShootingPlacement和Person结合在一起,并且如果需要,还可以让您单独查询它,而无需其他关联模型.

Documentation

> CakePHP 1.3 Model Associations
> CakePHP 1.3 Containable Behaviour

See also

> This article on Stack

标签:cakephp-1-3,php,sql,mysql,cakephp
来源: https://codeday.me/bug/20191010/1887789.html