table - fully qualified namespace; * 'field' -> table field * 'exclude' -> where clause or field/value pair to exclude from the query */ public function __construct(array $data = null) { if (!array_key_exists('table', $data)) { $data['table'] = new CategoriesTable(); } if (!array_key_exists('field', $data)) { $data['field'] = 'id'; } parent::__construct($data); } /** * * get select object, construct if it wasn't set yet * * @return \Cube\Db\Select */ public function getSelect() { if ($this->_select === null) { $adapter = $this->_table->getAdapter(); $tableName = $this->_table->getPrefix() . $this->_table->getName(); $select = $this->_table->select() ->columns("(SELECT count(*) FROM " . $adapter->quoteTableAs($tableName) . " WHERE " . $adapter->quoteIdentifier('parent_id') . " = '" . strval($this->_value) . "') AS nb_rows") ->where($adapter->quoteIdentifier($this->_field) . ' = ?', strval($this->_value)) ->having('nb_rows = 0'); if ($this->_exclude !== null) { if (is_array($this->_exclude)) { $select->where( $adapter->quoteIdentifier($this->_exclude['field'], true) . ' != ?', strval($this->_exclude['value']) ); } else { $select->where($this->_exclude); } } $select->limit(1); $this->_select = $select; } return $this->_select; } }