You probably meant if ($rows->count()). Like this:
protected function _emailAlreadySubscribed($email)
{
$select = $this->_model->select()->where('email = ?', $email);
$rows = $table->fetchAll($select);
if ($rows->count()) {
return true;
}
return false;
}
That's a valid approach. An even more elegant and generic approach is to write a custom validator doing the same thing. In most cases you will aquire email addresses over a form anyways:
class Project_Validate_DbUnique extends Zend_Validate_Abstract
{
const NOT_UNIQUE = 'dbUniqueNotUnique';
protected $_messageTemplates = array(
self::NOT_UNIQUE => "'%column%' '%value' already exists"
);
/**
* @var array
*/
protected $_messageVariables = array(
'column' => '_column',
);
/**
* The table where to check for unique value in column
*
* @var Zend_Db_Table
*/
protected $_dbTable = NULL;
/**
* The column name where to check for unique value
*
* @var string
*/
protected $_column = '';
/**
* The values of the primary key for this row if updating - to exclude the current row from the test
*
* @var array
*/
protected $_rowPrimaryKey = NULL;
public function __construct(Zend_Db_Table_Abstract $table, $column, $rowPrimaryKey = NULL)
{
$this->_dbTable = $table;
$this->_column = $column;
$this->_rowPrimaryKey = $rowPrimaryKey;
}
public function isValid($value)
{
$this->_setValue($value);
$select = $this->_dbTable->select();
$select->where($this->_dbTable->getAdapter()->quoteInto($this->_column . ' = ?', $value));
if (isset($this->_rowPrimaryKey))
{
$rowPrimaryKey = (array) $this->_rowPrimaryKey;
$info = $this->_dbTable->info();
foreach ($info['primary'] as $key => $column)
{
$select->where($this->_dbTable->getAdapter()->quoteInto($column . ' != ?', $rowPrimaryKey[$key - 1]));
}
}
$row = $this->_dbTable->fetchAll($select);
if ($row->count())
{
$this->_error();
return false;
}
return true;
}
}