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

时间:2014-12-15 04:04:55

标签: mysql orm phalcon

我正在使用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"
    ));
  }
}

我收到以下错误:

The column 'id' is ambiguous, when preparing: SELECT [Address].* FROM [Address] INNER JOIN [ClientAddress] ON [ClientAddress].[address_id] = [Address].[id] WHERE [ClientAddress].[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子句前面加上表名吗?

1 个答案:

答案 0 :(得分:1)

我遇到了和你相同的问题。我通过添加

来修复它
$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表的相同列名

,则会出现以下问题