Пример #1
0
 /**
  * Used to get related records of a result set in a Many-to-Many relationship
  *
  * @access public
  * @param array $results the result set (an array of records)
  * @param string $table the name of the other table
  * @return array the result set with related records
  */
 function joinMany2Many($results, $table)
 {
     // Get relationship
     $other_rel = $this->_other_obj->getRelationship($table);
     if (autocrud_is_error($other_rel)) {
         return $this->halt('no-relationship');
     }
     // Get other object and other fields
     $other_obj =& $other_rel->getOther();
     $owner_field = $other_rel->getOwnerField();
     $other_field = $other_rel->getOtherField();
     $ids_list = array();
     $newresults = array();
     $keys = array();
     // Get ID's from results
     foreach ($results as $key => $row) {
         // Get ID
         $id = $row[$this->_owner_field];
         $ids_list[] = $id;
         // Copy to a new array, and add it under its own ID
         $newresults[$id] = $row;
         // Save key (in case a specific key was being used)
         $keys[] = $key;
     }
     $results = $newresults;
     $ids_list = array_unique($ids_list);
     $ids_list = implode("', '", $ids_list);
     // Get records from in between table
     $this->_other_obj->where = "`" . $this->_other_field . "` IN ('" . $ids_list . "')";
     $between = $this->_other_obj->select();
     // Collect ID's from in between table
     $ids_list = array();
     foreach ($between as $row) {
         // Get ID
         $id = $row[$table];
         $ids_list[] = $id;
     }
     $ids_list = array_unique($ids_list);
     $ids_list = implode("', '", $ids_list);
     // Get records from other table
     $crud =& $this->_other_obj->getParent();
     $other_obj->where = "`" . $other_field . "` IN ('" . $ids_list . "')";
     $other = $other_obj->select($other_field);
     // Match up results with other records
     $name = $other_obj->getName();
     foreach ($between as $row) {
         $id_owner = $row[$this->_other_field];
         $id_other = $row[$owner_field];
         // Owner exists?
         if (!isset($results[$id_owner])) {
             continue;
         }
         // Other exists?
         if (!isset($other[$id_other])) {
             continue;
         }
         $owner_row =& $results[$id_owner];
         $other_row =& $other[$id_other];
         // Make sure owner table array property exists
         if (!isset($owner_row[$name])) {
             $owner_row[$name] = array();
         }
         // Add other row to owner
         $owner_row[$name][] = $other_row;
     }
     // Return keys
     $newresults = array();
     $i = 0;
     foreach ($results as $row) {
         $key = $keys[$i];
         $newresults[$key] = $row;
         $i++;
     }
     $results = $newresults;
     return $results;
 }
Пример #2
0
 /**
  * Used to delete an existing record, like this:
  * <code>$crud->table->delete (23);</code>
  *
  * @access public
  * @param mixed $id the ID of the record you want to delete
  * @return mixed true on success, error object on failure
  */
 function delete($id = '')
 {
     // Begin SQL query
     $sql = "DELETE FROM `" . $this->table . "`";
     // Id been passed?
     if (!empty($id)) {
         $sql .= " WHERE `" . $this->key . "` = " . $this->quote($id) . " ";
     }
     // WHERE property set?
     if (!empty($this->where)) {
         if (empty($id)) {
             $sql .= " WHERE ";
         }
         $sql .= $this->where;
         $this->where = '';
     }
     // Execute query
     $result = $this->_parent->query($sql);
     if (autocrud_is_error($result)) {
         return $result;
     }
     return true;
 }