2
votes

I'm very new to Zend Framework and php.

I went through the Zend Framework 2 tutorial and tried to use AbstractTableGateway for querying multiple tables.

But got the following message on the web page:

The table name of the provided select object must match that of the table

Here is part of my code:

class PublicationTable extends AbstractTableGateway {

protected $table = 'publication';

public function fetchAll()
{
    $sql = new Sql($this->adapter);
    $select = $sql->select();
    $select->from(array('p' => 'publication'))
           ->join('author','publication_fk=p.publication_pk');

    $resultSet = $this->selectWith($select);
    return $resultSet;
}


...

}

I'm aware that the variable "protected $table" is a String. So how can one resolve this? Thanks for the help!

EC

1

1 Answers

5
votes

The from() method takes a table name, not a list of columns. Use columns() to specify the columns that you want. I've never tried from a TableGateway though as if you are doing joins, then TableGateway isn't the best pattern to follow.

If you use the DbAdapater directly, then something like this should work:

use Zend\Db\Sql\Select,
    Zend\Db\ResultSet\ResultSet;

$select = new Select;
$select->from('publication')
    ->join('author', 'publication.publication_pk = author.publication_fk',
          array('columnnamefromauthortable1', 'columnnamefromauthortable2'));

$statement = $adapter->createStatement();
$select->prepareStatement($adapter, $statement);

$resultSet = new ResultSet();
$resultSet->initialize($statement->execute());