示例#1
0
 /**
  * @param       $table
  * @param array $data
  *
  * @return bool|null
  * @throws Exception
  */
 public function updateOrInsert($table, array $data = [])
 {
     $primaryKeys = $this->repository->getCache()->getTablePrimaryKeys($table);
     /**
      * @T00D00 - bug for translations, permissions ...!
      */
     if (!$primaryKeys) {
         return;
     }
     foreach ($primaryKeys as $primaryKey) {
         if (!isset($data[$primaryKey])) {
             return;
         }
     }
     $this->entity->setTable($table);
     foreach ($primaryKeys as $primaryKey) {
         $this->entity->where($primaryKey, $data[$primaryKey]);
     }
     $this->entity->getQuery()->select(['`' . $table . '`.*']);
     $record = $this->entity->one();
     if ($record) {
         return $this->update($table, $data);
     }
     return (new InsertRecord($this->record, $this->entity, $this->repository))->setTables($table)->execute();
 }
示例#2
0
文件: Entity.php 项目: pckg/database
 /**
  * @return Collection
  */
 public function all()
 {
     $this->applyExtensions();
     $all = $this->repository->all($this);
     $this->resetQuery();
     $this->resetRelations();
     return $all;
 }
示例#3
0
 /**
  *
  */
 public function execute()
 {
     $data = $this->entity->tabelizeRecord($this->record);
     /**
      * We need to get entity table extensions.
      * Lets hardcode them for now.
      */
     $extensions = ['i18n', 'p17n', ''];
     $table = $this->entity->getTable();
     $primaryKeys = $this->entity->getRepository()->getCache()->getTablePrimaryKeys($table);
     if (!$primaryKeys) {
         throw new Exception('Will NOT delete from table without primary keys ...');
     }
     foreach ($extensions as $ext) {
         if ($ext) {
             $ext = '_' . $ext;
         }
         if ($this->entity->getRepository()->getCache()->hasTable($table . $ext)) {
             /**
              * We will delete record from $table ...
              */
             $query = (new Delete())->setTable($table . $ext);
             /**
              * ... add primary key condition ...
              */
             foreach ($primaryKeys as $key) {
                 $query->where($key, $data[$table][$key]);
             }
             /**
              * ... prepare query ...
              */
             $prepare = $this->repository->prepareQuery($query);
             /**
              *  ... and execute it.
              */
             $this->repository->executePrepared($prepare);
         }
     }
     $this->record->setSaved(false);
     $this->record->setDeleted(true);
     return true;
 }
示例#4
0
文件: Cache.php 项目: pckg/database
   /**
    *
    */
   protected function buildRelations()
   {
       $sql = 'SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `COLUMN_NAME`, `REFERENCED_TABLE_SCHEMA`, `REFERENCED_TABLE_NAME`, `REFERENCED_COLUMN_NAME`
 FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` WHERE `TABLE_SCHEMA` = SCHEMA() AND `REFERENCED_TABLE_NAME` IS NOT NULL;';
       $prepare = $this->repository->getConnection()->prepare($sql);
       $prepare->execute();
       foreach ($prepare->fetchAll(PDO::FETCH_ASSOC) as $result) {
           $table = $result['TABLE_NAME'];
           $primary = $result['COLUMN_NAME'];
           $references = $result['REFERENCED_TABLE_NAME'];
           $on = $result['REFERENCED_COLUMN_NAME'];
           $key = 'FOREIGN__' . substr($table . '__' . $primary, -55);
           $this->cache['constraints'][$table][$key] = ['type' => 'FOREIGN', 'primary' => $primary, 'references' => $references, 'on' => $on];
       }
   }
示例#5
0
 /**
  * @param       $table
  * @param array $data
  *
  * @return mixed
  * @throws Exception
  */
 public function insert($table, array $data = [])
 {
     /**
      * We will insert $data into $table ...
      */
     $query = (new Insert())->table($table)->setInsert($data);
     /**
      * ... prepare query ...
      */
     $prepare = $this->repository->prepareQuery($query);
     /**
      * ... execute it ...
      */
     $this->repository->executePrepared($prepare);
     /**
      * ... and return inserted ID.
      */
     return $this->repository->getConnection()->lastInsertId();
 }