Exemple #1
0
 public function getKeys(IEntity $entity, $relationName)
 {
     $relOpt = $entity->relations[$relationName];
     $relType = $relOpt['type'];
     switch ($relType) {
         case IEntity::BELONGS_TO:
             $key = $this->toDb($entity->getEntityType()) . 'Id';
             if (isset($relOpt['key'])) {
                 $key = $relOpt['key'];
             }
             $foreignKey = 'id';
             if (isset($relOpt['foreignKey'])) {
                 $foreignKey = $relOpt['foreignKey'];
             }
             return array('key' => $key, 'foreignKey' => $foreignKey);
         case IEntity::HAS_MANY:
             $key = 'id';
             if (isset($relOpt['key'])) {
                 $key = $relOpt['key'];
             }
             $foreignKey = $this->toDb($entity->getEntityType()) . 'Id';
             if (isset($relOpt['foreignKey'])) {
                 $foreignKey = $relOpt['foreignKey'];
             }
             return array('key' => $key, 'foreignKey' => $foreignKey);
         case IEntity::HAS_CHILDREN:
             $key = 'id';
             if (isset($relOpt['key'])) {
                 $key = $relOpt['key'];
             }
             $foreignKey = 'parentId';
             if (isset($relOpt['foreignKey'])) {
                 $foreignKey = $relOpt['foreignKey'];
             }
             $foreignType = 'parentType';
             if (isset($relOpt['foreignType'])) {
                 $foreignType = $relOpt['foreignType'];
             }
             return array('key' => $key, 'foreignKey' => $foreignKey, 'foreignType' => $foreignType);
         case IEntity::MANY_MANY:
             $key = 'id';
             if (isset($relOpt['key'])) {
                 $key = $relOpt['key'];
             }
             $foreignKey = 'id';
             if (isset($relOpt['foreignKey'])) {
                 $foreignKey = $relOpt['foreignKey'];
             }
             $nearKey = $this->toDb($entity->getEntityType()) . 'Id';
             $distantKey = $this->toDb($relOpt['entity']) . 'Id';
             if (isset($relOpt['midKeys']) && is_array($relOpt['midKeys'])) {
                 $nearKey = $relOpt['midKeys'][0];
                 $distantKey = $relOpt['midKeys'][1];
             }
             return array('key' => $key, 'foreignKey' => $foreignKey, 'nearKey' => $nearKey, 'distantKey' => $distantKey);
     }
 }
Exemple #2
0
 public function removeRelation(IEntity $entity, $relationName, $id = null, $all = false, IEntity $relEntity = null)
 {
     if (!is_null($relEntity)) {
         $id = $relEntity->id;
     }
     if (empty($id) && empty($all) || empty($relationName)) {
         return false;
     }
     $relOpt = $entity->relations[$relationName];
     if (!isset($relOpt['entity']) || !isset($relOpt['type'])) {
         throw new \LogicException("Not appropriate defenition for relationship {$relationName} in " . $entity->getEntityType() . " entity");
     }
     $relType = $relOpt['type'];
     $className = !empty($relOpt['class']) ? $relOpt['class'] : $relOpt['entity'];
     if (is_null($relEntity)) {
         $relEntity = $this->entityFactory->create($className);
         if (!$relEntity) {
             return null;
         }
         $relEntity->id = $id;
     }
     $keySet = $this->query->getKeys($entity, $relationName);
     switch ($relType) {
         case IEntity::BELONGS_TO:
             /*$foreignKey = $keySet['foreignKey'];
               $relEntity->$foreignKey = null;
               $this->
               break;*/
         /*$foreignKey = $keySet['foreignKey'];
           $relEntity->$foreignKey = null;
           $this->
           break;*/
         case IEntity::HAS_ONE:
             return false;
         case IEntity::HAS_MANY:
         case IEntity::HAS_CHILDREN:
             $key = $keySet['key'];
             $foreignKey = $keySet['foreignKey'];
             $setPart = $this->toDb($foreignKey) . " = " . "NULL";
             $whereClause = array('deleted' => 0);
             if (empty($all)) {
                 $whereClause['id'] = $id;
             } else {
                 $whereClause[$foreignKey] = $entity->id;
             }
             if ($relType == IEntity::HAS_CHILDREN) {
                 $foreignType = $keySet['foreignType'];
                 $whereClause[$foreignType] = $entity->getEntityType();
             }
             $wherePart = $this->query->getWhere($relEntity, $whereClause);
             $sql = $this->composeUpdateQuery($this->toDb($relEntity->getEntityType()), $setPart, $wherePart);
             if ($this->pdo->query($sql)) {
                 return true;
             }
             break;
         case IEntity::MANY_MANY:
             $key = $keySet['key'];
             $foreignKey = $keySet['foreignKey'];
             $nearKey = $keySet['nearKey'];
             $distantKey = $keySet['distantKey'];
             $relTable = $this->toDb($relOpt['relationName']);
             $setPart = 'deleted = 1';
             $wherePart = $this->toDb($nearKey) . " = " . $this->pdo->quote($entity->id);
             if (empty($all)) {
                 $wherePart .= " AND " . $this->toDb($distantKey) . " = " . $this->pdo->quote($id) . "";
             }
             if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) {
                 foreach ($relOpt['conditions'] as $f => $v) {
                     $wherePart .= " AND " . $this->toDb($f) . " = " . $this->pdo->quote($v);
                 }
             }
             $sql = $this->composeUpdateQuery($relTable, $setPart, $wherePart);
             if ($this->pdo->query($sql)) {
                 return true;
             }
             break;
     }
 }