3
votes

I would like to add a where clause with this method.

public function fetchAll()
{
    $resultSet = $this->select();
    return $resultSet;
}

I have added like this.

public function fetchAll()
{
    $resultSet = $this->select();
    $resultSet->where("status = ?", 1);
    return $resultSet;
}

But, it shown the below error.

Fatal error: Call to undefined method Zend\Db\ResultSet\ResultSet::where()

Could you please help me to add WHERE, OR WHERE, GROUP and ORDER with above mentioned method.

2
Is status a field name here? - Kenzo
thanks. yes. Status column has 0 and 1 values. i like to select 1. - 2plus
I assume you extend from TableGateway? - Sam
In this case add a $where = new \Zend\Db\Sql\Where(); //do more! to $this->select($where). See framework.zend.com/manual/2.0/en/modules/… Also: start to read the manual, please! This is explained, see framework.zend.com/manual/2.0/en/modules/… - Sam

2 Answers

1
votes

Hopefully this will help you out:

    public function Profile($accountid)
    {
        $result = $this->select(function (Select $select) use ($accountid) {
            $select
                ->columns(array(
                    'datefrom',
                    'available',
                    'used',
                    'details'
                ))
                ->where($this->adapter->getPlatform()->quoteIdentifier('accountid') . ' = ' . $this->adapter->getPlatform()->quoteValue($accountid));
        });

        $row = $result->current();

        if (!$row) {
            throw new \Exception('could_not_find_row');
        }

        return $row;
    }
1
votes

You can also do $sql->select()->where(array('status' => 1)) or $table->select(array('status' => 1)) (annoying, wish they used the same syntax).

You pass where an associative array with the mapping column => value (and since it's an array you can give it multiple clauses). Diemuzi's way is good for complex identifiers but this is a more readable way for simple comparison.