/**
  * Delete all objects
  *
  * This creates a single delete query using the complete keys of 
  * each of the dbobjects that has been registered
  */
 function delete()
 {
     if (count($this->dbobjects) && is_array($this->dbobjects)) {
         $clause = new Clause();
         $class_name = $this->class_name;
         $obj = new $class_name();
         DbObject::cascadeDelete($this->dbobjects);
         foreach ($this->dbobjects as $dbobj) {
             /**$sub_clause = new Clause();
             
                                 foreach($dbobj->primaryKeys() as $field)
                                 {
                                     $sub_clause->addTableCondition($obj->tableName(),$field->name(),$field->value());
                                 }*/
             if ($dbobj->db_clause) {
                 $clause->addSubClause($dbobj->db_clause, Clause::OR_REL, Clause::LAST);
             } else {
                 $clause->addSubClause($dbobj->uniqueClause(), Clause::OR_REL, Clause::LAST);
             }
         }
         $query = new DeleteQuery($obj->tableName());
         $query->setClause($clause);
         Session::unRegister($this->class_name . '_rel_deleteregister');
         $query->doQuery();
     }
 }
Exemplo n.º 2
0
 /**
  * Return a paginated list of objects from a query
  *
  * Pagination involves splitting up a list of objects into a series of
  * smaller lists. For example, you might want to show your user a list
  * of 1000 books 50 books at time. This is because a list of a thousand
  * books would take up lots of memory and time and not be very useable.
  *
  * Pagination is quite easy when are doing a query on a single table, but
  * gets a little more tricky when you want to paginate a joined query.
  * This method works for any query, now matter how complex it is.
  *
  * This method returns an object of the type {@link DataPage}. See the
  * documentation for further details.
  *
  * @param num          - the page number
  * @param num_per_page - the number of items per page
  * @see fetch,DataPage
  */
 function page($num, $num_per_page, $query = '')
 {
     if (count($this->exclude)) {
         die('Using exclude() with page() is no currently supported. You will need to do this manually');
     }
     if (!is_object($query)) {
         $query = $this->loadQuery();
     }
     $start = ($num - 1) * $num_per_page;
     $ids = $query->getLimitedIds($this->tableName(), $this->primaryKeys(), $start, $num_per_page);
     $count = $query->foundRows();
     //$this->tableName(),$this->primaryKeys());
     $num_pages = ceil($count / $num_per_page);
     if ($num > $num_pages) {
         throw new Exception('Page: ' . $num . ' does not exist!');
     }
     $master = new Clause();
     foreach ($ids as $arr) {
         $sub = new Clause();
         foreach ($this->primaryKeys() as $key) {
             $sub->addCondition($this->tableName() . '.' . $key->name(), $arr[$key->name()]);
         }
         $master->addSubClause($sub, Clause::OR_REL, Clause::LAST);
     }
     $query->getClause()->addSubClause($master, Clause::AND_REL, Clause::ALL);
     $objects = $this->loadMultiple($query);
     $ret = new DataPage($objects, $num, $num_per_page, $count);
     return $ret;
 }