1

我使用 zend_db_select 来连接 3 个表,并且在结果集数组中,当我期望看到带有别名的列名时,它返回一个数组,其中的键没有别名。

$dbSelect = $db->select()->from(array("pp"=>"products_photos"),array())
                         ->joinInner(array("ph"=>"photos"), "pp.photo_id=ph.photo_id","ph.photo_id")
                         ->joinInner(array('pr'=>'products'),"pr.product_id=pp.product_id","pr.product_id")
                         ->where("pr.product_id=$row->product_id");

$photoJoinRowSet = $db->fetchAll($dbSelect);
var_dump($photoJoinRowSet);die();

结果喜欢:

array(2) { [0]=> array(3) { ["product_id"]=> string(1) "1" ["photo_id"]=> string(1) "4" }}

虽然我期待:

array(2) { [0]=> array(3) { ["pr.product_id"]=> string(1) "1" ["ph.photo_id"]=> string(1) "4" }}

......即使用列别名。

有谁知道为什么会这样??谢谢。

4

1 回答 1

1

You didn't specify any aliases here, so your select will translate to something like SELECT ph.photo_id, pr.product_id, without AS, which will return photo_id and product_id as expected.

You need to specify explicitly your aliases if you want the dots in the keys:

$dbSelect = $db->select()->from(array("pp"=>"products_photos"),array())
    ->joinInner(array("ph"=>"photos"), "pp.photo_id=ph.photo_id",
      array("ph.photo_id" => "ph.photo_id"))
    ->joinInner(array('pr'=>'products'), "pr.product_id=pp.product_id",
      array("pr.product_id" => "pr.product_id"))
    ->where("pr.product_id=$row->product_id");

More information on the Zend_Db_Select documentation.

于 2011-06-10T23:41:38.203 回答