0

I am confused with zend 2 join query with multiple column display, and unable to display those column

StudTable.php

public function custom_query() {
        $select = new Select();
        $select->from(array('s' => 'stud'));
        $select->columns(array('s.id','a.artist', 's.name', 's.sem','s.age'));
        //$select->where(array('id' => 14));

        $select->join(array('a' => 'album'),
                    'a.id = s.album_id');
        $select->order('s.id DESC'); 
        $select->limit(5);

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

index.phtml

<ul>
    <?php foreach ($this->customeObject as $obj_cus) : ?>
        <li>ID: <?php echo $obj_cus->id?>| Artist: <?php echo $obj_cus->artist?> | Name:<?php echo $obj_cus->name?> | Sem:<?php echo $obj_cus->sem?> | Age:<?php echo $obj_cus->age?></li>
    <?php endforeach; ?>
    </ul>

but it shows below error enter image description here

2 Answers 2

1
$select = new Select();
$select->from(array('s' => 'stud'));
/* Select columns from primary table without prefix table */
$select->columns(array('id', 'name', 'sem', 'age'));

/* If need where */
$select->where(array('s.id' => 14));

/* It's a INNER JOIN */
$select->join(
    array('a' => 'album'),
    'a.id = s.album_id',
    /* Select joined columns */
    array('artist')
);
$select->order('s.id DESC'); 
$select->limit(5);
return $resultSet = $this->tableGateway->selectWith($select);
Sign up to request clarification or add additional context in comments.

Comments

1

The columns method signature looks like this:

public function columns(array $columns, $prefixColumnsWithTable = true)

So by default prefixColumnsWithTable is enabled.

That's why you're getting the error message:

Unknown column s.s.id

So the easiest way to fix this is to pass in false as the second parameter to columns:

$select->columns(array('s.id','a.artist', 's.name', 's.sem','s.age'), false);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.