gpt4 book ai didi

mysql - Phalcon ORM 显示在遍历模型关系时该列不明确

转载 作者:行者123 更新时间:2023-11-29 12:31:10 25 4
gpt4 key购买 nike

我正在使用 Phalcon 来查询模型之间的一些关系。我有以下型号:

<?php

class Client extends ModelBase
{
private $id;
private $code;
private $active;

public function initialize()
{
parent::initialize();

$this->hasManyToMany(
"id",
"ClientAddress",
"client_id",
"address_id",
"Address",
"id",
array("alias" => "addresses")
);

public function getAddresses($params = null) {
return $this->getRelated("addresses", $params);
}
}


class Address extends ModelBase
{
private $id;
.......

<?php

class ClientAddress extends ModelBase
{
private $id;
private $active;

public function initialize()
{
parent::initialize();
$this->belongsTo("client_id", "Client", "id", array(
"alias" => "client"
));

$this->belongsTo("address_id", "Address", "id", array(
"alias" => "address"
));
}
}

我收到以下错误:

列“id”不明确,准备时: SELECT [Address].* FROM [Address] INNER JOIN [ClientAddress] ON [ClientAddress].[address_id] = [Address].[id] WHERE [客户端地址].[client_id] = ?0 AND id = ?1

产生此错误的代码是:

$client = Client::findFirst(array(
"conditions" => "id = ?1",
"bind" => array( 1 => $current_client_id )
));

$addresses = $client->getAddresses(array(
"conditions" => "id = ?1",
"bind" => array( 1 => $id )
));

为什么会发生这种情况?我的关系设置不正确吗?我会假设它会在我的 where 子句中添加表名作为前缀?

最佳答案

我也遇到了和你一样的问题。我通过添加修复了它

$client = Client::findFirst(array(
"conditions" => "[ClientAddress].[id] = ?1",
"bind" => array( 1 => $current_client_id )
));

$addresses = $client->getAddresses(array(
"conditions" => "[Address].[id] = ?1",
"bind" => array( 1 => $id )
));

当两个不同的 MySQL 表具有相同的列名时,您会遇到以下问题

关于mysql - Phalcon ORM 显示在遍历模型关系时该列不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com