7

I feel like I must me missing something very simple. It's a very simple task, all I want to do is get something like:

SELECT * FROM lookup_items
JOIN lookup ON lookup_items.lookup_id = lookup.id

This returns all the columns for all the joined tables, in regular SQL. Here's my attempt in zf2:

$select = new Select();

$select->from('lookup_items');
$select->join('lookup', 'lookup_items.lookup_id = lookup.id');

The result set only includes the columns in 'lookup_items'. I've tried various ways to get the 'lookup' columns, including:

$select->columns(array('lookup_items.*', 'lookup.*'));

But they all just blow up. Surely there's a way to do this, and it's just so simple I'm missing it completely.

I thought a simple example would be avoid confusion, but here's more code:

class LookupItemsTable extends AbstractTableGateway
{
public function getList($resource)
{
    $system_name = str_replace('*', '%', strtoupper($resource));

    $joinTable = 'lookup';

    $select = new Select();

    $select->from($this->table);
    $select->join($joinTable, "{$this->table}.lookup_id = {$joinTable}.id");

    $where = array();
    $where[] = "{$this->table}.enabled is true";
    $where[] = "{$joinTable}.enabled is true";
    $where[] = "UPPER({$joinTable}.system_name) ilike '{$system_name}'";

    $select->where($where);

    $sort[] = 'sort_order ASC';
    $sort[] = 'value ASC';
    $select->order($sort);

    $rowset = $this->selectWith($select);

    return $rowset;
}
}

Where:

$resource = $this->params()->fromRoute('resource', 'BUSINESS');

And $this->table is 'lookup_items'. Really all I want to do is get columns from both joined tables. I guess there's a zf2 way to just make a straight SQL statement without all the OO falderal, so I could just force it that way. But I'd rather work within the framework as much as possible.

1
  • What is the context of the code? Is it within a TableGateway class? If so are you setting an entity as a prototype for the result set? Can you post more code please. Commented May 22, 2013 at 18:05

4 Answers 4

7

Just change this line

$select->join('lookup', 'lookup_items.lookup_id = lookup.id');

to

$select->join('lookup', 'lookup_items.lookup_id = lookup.id', array('lookupcol1', 'lookupcol2');
1
  • 1
    Additionally please read @"Al Fonce" ans below to get a clear picture of this problem Commented Sep 5, 2015 at 11:35
3

Raj answer is the best one but it only works if you don't forget to add these fiels in your LookupItems model.

class LookupItems
{
    // Your lookup_items fields here...
    ...
    // And the added lookup fields here, the ones you add in the array
    public $lookupcol1;
    public $lookupcol2;

And in the exchangeArray method :

public function exchangeArray($data)
{
   // .... your fields, and the new ones
    $this->lookupcol1 = (! empty($data['lookupcol1'])) ? $data['lookupcol1'] : null;
    $this->lookupcol2 = (! empty($data['lookupcol2'])) ? $data['lookupcol2'] : null;
}
1

I figured it out.

Added this:

$select->columns(array('*'));

And then this near the end:

$sql = new Sql($this->adapter);
$statement = $sql->prepareStatementForSqlObject($select);
$rowset = $statement->execute();

This returns the expected result, with the caveat that now my rows are returned as associative arrays instead of objects.

0

This is how you can create queries with join in zf2.

    $resultSet = $this->select(function (Select $select) {
            // omit the table name 
           //$select->from('foo');
            $select->join('users', "users.id foo.createdby", 'firstname', '');
            $select->order('id ASC');
           // echo $select->getSqlString();// to print your query
        });
    $entities = array();
    foreach ($resultSet as $row) {
        $entity = new Entity\Foo();
        $entity->setId($row->id)
            ->setFullname($row->fullname)
                    ->setCaseid($row->caseid)
                    ->setTestimonial($row->testimonial)
                    ->setSortorder($row->sortorder)
                    ->setActive($row->active)
                    ->setCreated($row->created)
                    ->setModified($row->modified)
                    ->setFirstname($row->firstname)
                    ->setCreatedby($row->createdby);
        $entities[] = $entity;
    }

    return $entities;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.