Exists statement for ZF2

In the next example you´ll see how to use an exists stament and how to use subqueries in ZF2.


$adapter = $this->tableGateway->adapter;
$sql = new Sql($adapter);

$campos = array(
	'fecha_generacion' => 'fecha_generacion',
	'numero_envio' => new Expression('max(numero_envio)')
);

$subSelect = $sql->select();
$subSelect->columns(array('codigo_empresa'));
$subSelect->from(array('b' => 'documento_resumen'))
		->where('b.codigo_empresa = a.codigo_empresa')
		->where('b.tipo_documento = a.tipo_documento')
		->where('b.fecha_generacion = a.fecha_generacion')
		->where('b.codigo_respuesta = \'0\'');

$select = $sql->select()
	->quantifier(\Zend\Db\Sql\Select::QUANTIFIER_DISTINCT)
	->columns($campos)
	->from(array('a' => 'documento_resumen'))
		->where(
			array(
				'a.codigo_empresa' => $codigoEmp,
				'a.tipo_documento' => $tipoDoc,
				'a.codigo_respuesta' => '2'
			)
		);
$select->where->addPredicate(
		new \Zend\Db\Sql\Predicate\Expression(
		  "NOT EXISTS(" . @$subSelect->getSqlString($adapter->getPlatform()) . ")"
		)
	);
$select->group('a.fecha_generacion');

// Run the query
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();

return ArrayUtils::iteratorToArray($result);

Recompile Interbase for Mac

Add interbase.so to MAMP / OSX

get xcode and macports.
install m4 through macports
download php-source corresponding to mamp-version

create symblinks needed
from http://www.ibexpert.net/forum/viewtopic.phpf=13&t=7&start=0&st=0&sk=t&sd=a

# in short:
# (backslash = keep on same row)
cd /usr
sudo  mkdir -p local/firebird/lib

sudo ln -s /Library/Frameworks/Firebird.framework/Firebird \
local/firebird/lib/libfbclient.dylib

sudo ln -s /Library/Frameworks/Firebird.framework/Headers/ \
local/firebird/include

#create .so
sudo su

cd /path-to-php-source/ext/interbase

/Applications/MAMP/bin/php5/bin/phpize

./configure –with-interbase=/usr/local/firebird

make

#install so to mamp-ext.dir
cp modules/interbase.so \
/Applications/MAMP/bin/php5/lib/ \
php/extensions/no-debug-non-zts-20050922

#add to /Applications/MAMP/conf/php5/php.ini
[firebird]
extension = interbase.so
ibase.allow_persistent = “1”
ibase.max_persistent = “-1”
ibase.max_links = “-1”
ibase.default_db = null
ibase.default_user = NULL
ibase.default_password = NULL
ibase.default_charset = NULL
ibase.timestampformat = “%Y-%m-%d %H:%M:%S”
ibase.dateformat = “%Y-%m-%d”
ibase.timeformat = “%H:%M:%S”

#restart mamp – interbase should show in phpinfo
#enjoy!