zendframework / zend-db

Db component from Zend Framework
BSD 3-Clause "New" or "Revised" License
101 stars 122 forks source link

SQL column alias bug #317

Open plasid opened 6 years ago

plasid commented 6 years ago

....$select...->join('vendors', 'vendors.id = campaigns_vendors.vendor_id', ['vendor_id'=>'id', 'vendor_daily_limit'=>'daily_limit'], $select::JOIN_LEFT)

When I query this select object I get the following error: "Not a valid magic property for this object" When I rename the column alias to anything else but 'vendor_daily_limit' the error goes away. When I print the produced SQL and run it via phpMyadmin or Bench then the query executes perfectly.

Very bizarre error and no indication of what is causing it.

tptrixtop commented 6 years ago

I have no luck to reproduce that error on my local env)

Could you please add more information, may be table structure or scope code ?)

samsonasik commented 6 years ago

you should can do something like this:

use Zend\Db\Sql\Predicate\Expression;

$select->join(
     'vendors', 
     'vendors.id = campaigns_vendors.vendor_id', 
     [
         'vendor_id'          => new Expression('vendors.id'),
         'vendor_daily_limit' => new Expression('vendors.daily_limit'),
      ],
      $select::JOIN_LEFT
);
plasid commented 6 years ago

It turns out I was looking at the wrong code. The error "Not a valid magic property for this object" is produced when you call a non-existing method on the Sql object i.e. $sql->join(....)->equalTo(....) - equalTo is a method of Where and not Sql, since ZF uses overloading for this, the error message is not obvious. A better message would be something like: "method equalTo not defined for Sql"

Sorry for wasting your time.

michalbundyra commented 4 years ago

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at https://github.com/laminas/laminas-db/issues/51.