I have two tables : product with id, name, price and brand fields and brand with id, name, url fields. I would like to select both of fields from this two tables. I am unable to define columns and defines aliases.
ProductTable
...
public function fetchAll()
{
$select = new Select;
$select->from($this->table);
$select->join('brand', 'product.brand = brand.id', array('name', 'url'));
$select->columns(array('product.id', 'name', 'price'));
$statement = $this->adapter->createStatement();
$select->prepareStatement($this->adapter, $statement);
$resultSet = new ResultSet();
$resultSet->initialize($statement->execute());
return $resultSet;
}
Message : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product.product.id' in 'field list'
What is the good practice for the request : select p.id as id, p.name as name, p.price, b.id as brandid, b.name as brandname...
After a few tries I found this solution :
public function fetchAll()
{
$select = new Select;
$select->from($this->table);
$select->join(array('b' => 'brand'), 'product.brand = b.id', array('brandid' => 'id', 'brandname' => 'name', 'url'));
$select->columns(array('id', 'name', 'price'));
$statement = $this->adapter->createStatement();
$select->prepareStatement($this->adapter, $statement);
$resultSet = new ResultSet();
$resultSet->initialize($statement->execute());
return $resultSet;
}
I found how to put a alias on join tables but what about the base table product ?