Exemplo n.º 1
0
 /**
  * Dynamically binds all of the attributes for the current BO to the supplied prepared statement
  * parameters.  If arrays of attribute names and values are provided, only those will be bound to
  * the supplied statement.
  *
  * @param mysqli_stmt $stmt The SQL statement to bind to.
  * @param array Optional array of BO attributes.
  * @param array Optional array of BO values.
  *
  * @return mysqli_stmt
  *
  * @since 1.1
  */
 private function bindParams($stmt, $attributes = array(), $values = array())
 {
     self::$logger->debug('>>bindParams(stmt=[' . var_export($stmt, true) . '])');
     $bindingsTypes = '';
     $params = array();
     // here we are only binding the supplied attributes
     if (count($attributes) > 0 && count($attributes) == count($values)) {
         $count = count($values);
         for ($i = 0; $i < $count; ++$i) {
             if (Validator::isInteger($values[$i])) {
                 $bindingsTypes .= 'i';
             } else {
                 $bindingsTypes .= 's';
             }
             array_push($params, $values[$i]);
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
     } else {
         // bind all attributes on the business object
         // get the class attributes
         $reflection = new ReflectionClass(get_class($this->BO));
         $properties = $reflection->getProperties();
         foreach ($properties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->BO->getTransientAttributes())) {
                 // Skip the OID, database auto number takes care of this.
                 if ($propName != 'OID' && $propName != 'version_num') {
                     if ($this->BO->getPropObject($propName) instanceof Integer) {
                         $bindingsTypes .= 'i';
                     } else {
                         $bindingsTypes .= 's';
                     }
                     array_push($params, $this->BO->get($propName));
                 }
                 if ($propName == 'version_num') {
                     $temp = $this->BO->getVersionNumber()->getValue();
                     $this->BO->set('version_num', $temp + 1);
                     $bindingsTypes .= 'i';
                     array_push($params, $this->BO->getVersionNumber()->getValue());
                 }
             }
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
         // the OID may be on the WHERE clause for UPDATEs and DELETEs
         if (!$this->BO->isTransient()) {
             $bindingsTypes .= 'i';
             array_push($params, $this->BO->getOID());
         }
     }
     self::$logger->debug('bindingsTypes=[' . $bindingsTypes . '], count: [' . mb_strlen($bindingsTypes) . ']');
     self::$logger->debug('params [' . var_export($params, true) . ']');
     if ($params != null) {
         $bind_names[] = $bindingsTypes;
         $count = count($params);
         for ($i = 0; $i < $count; ++$i) {
             $bind_name = 'bind' . $i;
             ${$bind_name} = $params[$i];
             $bind_names[] =& ${$bind_name};
         }
         call_user_func_array(array($stmt, 'bind_param'), $bind_names);
     }
     self::$logger->debug('<<bindParams [' . var_export($stmt, true) . ']');
     return $stmt;
 }
 /**
  * (non-PHPdoc).
  *
  * @see Alpha\Model\ActiveRecordProviderInterface::findMissingFields()
  */
 public function findMissingFields()
 {
     self::$logger->debug('>>findMissingFields()');
     $missingFields = array();
     $matchCount = 0;
     $sqlQuery = 'PRAGMA table_info(' . $this->BO->getTableName() . ')';
     $result = self::getConnection()->query($sqlQuery);
     $this->BO->setLastQuery($sqlQuery);
     // get the class attributes
     $reflection = new ReflectionClass(get_class($this->BO));
     $properties = $reflection->getProperties();
     foreach ($properties as $propObj) {
         $propName = $propObj->name;
         if (!in_array($propName, $this->BO->getTransientAttributes())) {
             while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
                 if ($propName == $row['name']) {
                     ++$matchCount;
                     break;
                 }
             }
             $result->reset();
         } else {
             ++$matchCount;
         }
         if ($matchCount == 0) {
             array_push($missingFields, $propName);
         } else {
             $matchCount = 0;
         }
     }
     // check for the "classname" field in overloaded tables
     if ($this->BO->isTableOverloaded()) {
         $foundMatch = false;
         while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
             if ('classname' == $row['name']) {
                 $foundMatch = true;
                 break;
             }
         }
         if (!$foundMatch) {
             array_push($missingFields, 'classname');
         }
     }
     if (!$result) {
         throw new AlphaException('Failed to access the system database correctly, error is [' . self::getLastDatabaseError() . ']');
     }
     self::$logger->debug('<<findMissingFields [' . var_export($missingFields, true) . ']');
     return $missingFields;
 }