/** * Especifica o valor de uma propriedade acessando o seu setter associado * * @param string $property * @param mixed $value */ public function __set($property, $value) { $setter = MethodSintaxe::buildSetterName($property); if (method_exists($this, $setter)) { $ref = new ReflectionMethod($this, $setter); $ref->invoke($this, $value); } else { throw new BadMethodCallException("O metodo {$setter} não existe"); } }
/** * Insere um Objeto, instancia de classe className, populado na tabela especifcada por tableName * @param mixed $object * @param string $whereCondition * @param string $additionalParameters optional parametros adicionais para o insert */ public function insertObject($object, $whereCondition = "", $topLevelClass = "") { if (self::isEmpty($topLevelClass)) { $topLevelClass = self::getTopLevelClass(); } $object_is_array = false; if (is_array($object)) { $object_array = $object; $object = $object_array[0]; } //$this->ref = new ReflectionClass($object); $ref1 = new ReflectionClass($object); $this->ref = $ref1; $parentClassesArray = array($this->ref); $r = $this->ref; while ($r->getParentClass()->getName() != $topLevelClass) { $r = $this->ref->getParentClass(); $parentClassesArray[] = $r; } $parentClassesArray = array_reverse($parentClassesArray); $fkId = ""; foreach ($parentClassesArray as $ref) { /*@var $ref ReflectionClass*/ $colunas = ""; $valores = ""; $ai = new ArrayIterator($ref->getDefaultProperties()); while ($ai->valid()) { /*@var $propertie ReflectionProperty*/ //verificar se as superclasse num tão pegando tudo $propertie = $ai->key(); if (substr($propertie, 0, 1) == self::DEFAULT_NO_INDEXED_PROPERTIE_PREFIX) { $ai->next(); continue; } $valor = null; $strCod = '$valor .= addslashes( $object->' . MethodSintaxe::buildGetterName($propertie) . '());'; eval($strCod); if ($propertie != self::DEFAULT_KEY_COLUNM && (!self::isEmpty($valor) || $valor === false)) { if ($this->validateAttributeColunm($propertie)) { $colunas .= $propertie . ' , '; $valores .= "'"; $strCod = '$valores .= addslashes( $object->' . MethodSintaxe::buildGetterName($propertie) . '());'; eval($strCod); $valores .= "' , "; } } $ai->next(); } if (!self::isEmpty($fkId)) { $colunas .= self::DEFAULT_KEY_COLUNM . " , "; $valores .= $fkId . ' , '; } $colunas = substr($colunas, 0, -2); $valores = substr($valores, 0, -2); if (!self::isEmpty($whereCondition)) { $whereCondition = "WHERE " . $whereCondition; } if (func_num_args() > 2) { $additionalParameter = func_get_arg(3); } else { $additionalParameters = ""; } /*@var $refMet ReflectionMethod */ $tableName = self::$tablePrefix . $this->upperToUnderline($ref->getName()); $q1 = "INSERT INTO " . $tableName . " ({$colunas}) VALUES ({$valores}) {$whereCondition} {$additionalParameters}"; $r1 = self::executeQuery($q1); $fkId = CRUDQuery::lastInsertId(); //$fkId = $r1->lastInsertId(); // if (strlen(mysqli_error(self::getConn())) > 0) { // throw new CRUDException("SQL ERROR: $q1 - " . mysqli_error(self::getConn())); // } } $refMet = new ReflectionMethod($this->getClassName(), MethodSintaxe::buildSetterName($this->getKeyColunm())); $refMet->invoke($object, (int) CRUDQuery::lastInsertId()); return $object; }