Apache Zeta Components Manual :: File Source for db_external.php
Source for file db_external.php
Documentation is available at db_external.php
* File containing the ezcTreeDbExternalTableDataStore class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version //autogentag//
* @package TreeDatabaseTiein
* ezcTreeDbExternalTableDataStore is an implementation of a tree node
* data store that uses an external table to store data in.
* @package TreeDatabaseTiein
* @version //autogentag//
* Contains the database connection handler.
* Contains the name of the table to fetch data from.
* Contains the name of the field that contains the node ID.
* Contains the name of the field to fetch data from.
* If this field is null, then the whole row is returned.
private $dataField =
null;
* Contains the DOM representing this tree this data store stores data for.
* Constructs a new storage backend that stores data in a table external
* The store will use the database connection specified by $dbHandler, and
* the table $dataTable to store the data in. The lookup field that is matched
* against the node ID is specified with $idField. By default the store will
* return the whole row unless a specific field has been configured through
* the $dataField argument in this constructor.
* @param ezcDbHandler $dbHandler
* @param string $dataTable
* @param string $dataField
public function __construct( ezcDbHandler $dbHandler, $dataTable, $idField, $dataField =
null )
$this->dbHandler =
$dbHandler;
$this->table =
$dataTable;
$this->idField =
$idField;
$this->dataField =
$dataField;
* Deletes the data for the node $node from the data store.
* @param ezcTreeNode $node
public function deleteDataForNode( ezcTreeNode $node )
* Deletes the data for all the nodes in the node list $nodeList.
* @param ezcTreeNodeList $nodeList
$nodeIdsToDelete =
array();
$nodeIdsToDelete[] = (string)
$id;
$q =
$db->createDeleteQuery();
$q->deleteFrom( $db->quoteIdentifier( $this->table ) )
->where( $q->expr->in( $db->quoteIdentifier( $this->idField ), $nodeIdsToDelete ) );
* Deletes the data for all the nodes in the store.
$q =
$db->createDeleteQuery();
$q->deleteFrom( $db->quoteIdentifier( $this->table ) );
* Takes the data from the executed query and uses the $dataField
* property to filter out the wanted data for this node.
private function filterDataFromResult( array $data )
if ( $this->dataField ===
null )
unset
( $data[$this->idField] );
return $data[$this->dataField];
* Retrieves the data for the node $node from the data store and assigns it
* to the node's 'data' property.
* @param ezcTreeNode $node
$q =
$db->createSelectQuery();
->from( $db->quoteIdentifier( $this->table ) )
->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
$result =
$s->fetch( PDO::FETCH_ASSOC );
$node->injectData( $this->filterDataFromResult( $result ) );
$node->dataFetched =
true;
* This method *tries* to fetch the data for all the nodes in the node list
* $nodeList and assigns this data to the nodes' 'data' properties.
* @param ezcTreeNodeList $nodeList
$nodeIdsToFetch =
array();
foreach ( $nodeList->nodes as $node )
if ( $node->dataFetched ===
false )
$nodeIdsToFetch[] =
$node->id;
if ( count( $nodeIdsToFetch ) ===
0 )
$q =
$db->createSelectQuery();
->from( $db->quoteIdentifier( $this->table ) )
->where( $q->expr->in( $db->quoteIdentifier( $this->idField ), $nodeIdsToFetch ) );
foreach ( $s as $result )
$nodeList[$result[$this->idField]]->injectData( $this->filterDataFromResult( $result ) );
$nodeList[$result[$this->idField]]->dataFetched =
true;
* Stores the data in the node to the data store.
* @param ezcTreeNode $node
// first we check if there is data for this node
$q =
$db->createSelectQuery();
$q->select( $db->quoteIdentifier( $this->idField ) )
->from( $db->quoteIdentifier( $this->table ) )
->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
if ( !$update ) // we don't have data yet, create an insert query
$q =
$db->createInsertQuery();
$q->insertInto( $db->quoteIdentifier( $this->table ) )
->set( $db->quoteIdentifier( $this->idField ), $q->bindValue( $node->id ) );
else // we have data, so use update
$q =
$db->createUpdateQuery();
$q->update( $db->quoteIdentifier( $this->table ) );
if ( $this->dataField ===
null )
foreach ( $node->data as $field =>
$value )
$q->set( $db->quoteIdentifier( $field ), $q->bindValue( $value ) );
$q->set( $db->quoteIdentifier( $this->dataField ), $q->bindValue( $node->data ) );
if ( $update ) // add where clause if we're updating
$q->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
$node->dataStored =
true;
* Associates the DOM tree for which this data store stores data for with
* This method is only needed for when a data store is used
* with an XML based tree backend. XML based tree backends call this method
* to associate the DOM tree with the store. This is not needed for this
* data store so the method is a no-op.
* @param DOMDocument $dom