public function init()
 {
     $member = $this->member;
     $instance = $this->instance;
     $class = get_class($instance);
     $annot = Reflexion::getAnnotationMember($class, $member, "ManyToMany");
     if ($annot != false) {
         $this->targetEntity = $annot->targetEntity;
         $this->inversedBy = strtolower($this->targetEntity) . "s";
         if (!is_null($annot->inversedBy)) {
             $this->inversedBy = $annot->inversedBy;
         }
         $this->targetEntityClass = get_class(new $this->targetEntity());
         $pk = OrmUtils::getFirstKey($this->targetEntityClass);
         $annotJoinTable = Reflexion::getAnnotationMember($class, $member, "JoinTable");
         $this->joinTable = $annotJoinTable->name;
         $joinColumnsAnnot = $annotJoinTable->joinColumns;
         $this->myFkField = "id" . OrmUtils::getTableName($class);
         $this->myPk = OrmUtils::getFirstKey($class);
         if (!is_null($joinColumnsAnnot)) {
             $this->myFkField = $joinColumnsAnnot->name;
             $this->myPk = $joinColumnsAnnot->referencedColumnName;
         }
         $this->targetEntityTable = OrmUtils::getTableName($this->targetEntity);
         $this->fkField = "id" . ucfirst($this->targetEntityTable);
         $this->pk = OrmUtils::getFirstKey($this->targetEntityClass);
         $inverseJoinColumnsAnnot = $annotJoinTable->inverseJoinColumns;
         if (!is_null($inverseJoinColumnsAnnot)) {
             $this->fkField = $inverseJoinColumnsAnnot->name;
             $this->pk = $inverseJoinColumnsAnnot->referencedColumnName;
         }
         return true;
     }
     return false;
 }
예제 #2
0
 /**
  * Met à jour $instance dans la base de données.
  * Attention de ne pas modifier la clé primaire
  * @param Classe $instance instance à modifier
  * @param $updateMany Ajoute ou met à jour les membres ManyToMany
  */
 public static function update($instance, $updateMany = false)
 {
     $tableName = OrmUtils::getTableName(get_class($instance));
     $ColumnskeyAndValues = Reflexion::getPropertiesAndValues($instance);
     $ColumnskeyAndValues = array_merge($ColumnskeyAndValues, OrmUtils::getManyToOneMembersAndValues($instance));
     $keyFieldsAndValues = OrmUtils::getKeyFieldsAndValues($instance);
     $sql = "UPDATE " . $tableName . " SET " . SqlUtils::getUpdateFieldsKeyAndValues($ColumnskeyAndValues) . " WHERE " . SqlUtils::getWhere($keyFieldsAndValues);
     \Logger::log("update", $sql);
     \Logger::log("Key and values", json_encode($ColumnskeyAndValues));
     $statement = DAO::$db->prepareStatement($sql);
     foreach ($ColumnskeyAndValues as $key => $value) {
         DAO::$db->bindValueFromStatement($statement, $key, $value);
     }
     $result = $statement->execute();
     if ($result && $updateMany) {
         DAO::insertOrUpdateAllManyToMany($instance);
     }
     return $result;
 }