Exemplo n.º 1
0
 protected function parseFinder(Sql $sql, Finder $finder, $alias, &$aliases, &$conditions)
 {
     $entity = $finder->getName();
     $parent = $finder->getParentName();
     $next = $finder->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $parsedConditions = $this->parseConditions($conditions, $finder, $alias);
     if (!empty($parsedConditions)) {
         $conditions[] = $parsedConditions;
     }
     if (is_null($parentAlias)) {
         return $sql->from($entity);
     } elseif ($finder->isRequired()) {
         $sql->innerJoin($entity);
     } else {
         $sql->leftJoin($entity);
     }
     if ($alias !== $entity) {
         $sql->as($alias);
     }
     $aliasedPk = "{$alias}." . $this->findPrimaryKey($entity);
     $aliasedParentPk = "{$parentAlias}." . $this->findPrimaryKey($parent);
     if ($entity === "{$parent}_{$next}") {
         return $sql->on(array("{$alias}.{$parent}_id" => $aliasedParentPk));
     } else {
         return $sql->on(array("{$parentAlias}.{$entity}_id" => $aliasedPk));
     }
 }
Exemplo n.º 2
0
 public function setUp()
 {
     $conn = new PDO('sqlite::memory:');
     $db = new Db($conn);
     $conn->exec((string) Sql::createTable('posts', array('id INTEGER PRIMARY KEY', 'title VARCHAR(255)', 'text TEXT', 'author_id INTEGER')));
     $conn->exec((string) Sql::createTable('authors', array('id INTEGER PRIMARY KEY', 'name VARCHAR(255)')));
     $conn->exec((string) Sql::createTable('comments', array('id INTEGER PRIMARY KEY', 'post_id INTEGER', 'text TEXT')));
     $conn->exec((string) Sql::createTable('categories', array('id INTEGER PRIMARY KEY', 'name VARCHAR(255)', 'category_id INTEGER')));
     $conn->exec((string) Sql::createTable('post_categories', array('id INTEGER PRIMARY KEY', 'post_id INTEGER', 'category_id INTEGER')));
     $this->posts = array((object) array('id' => 5, 'title' => 'Post Title', 'text' => 'Post Text', 'author_id' => 1));
     $this->authors = array((object) array('id' => 1, 'name' => 'Author 1'));
     $this->comments = array((object) array('id' => 7, 'post_id' => 5, 'text' => 'Comment Text'), (object) array('id' => 8, 'post_id' => 4, 'text' => 'Comment Text 2'));
     $this->categories = array((object) array('id' => 2, 'name' => 'Sample Category', 'category_id' => null), (object) array('id' => 3, 'name' => 'NONON', 'category_id' => null));
     $this->postsCategories = array((object) array('id' => 66, 'post_id' => 5, 'category_id' => 2));
     foreach ($this->authors as $author) {
         $db->insertInto('authors', (array) $author)->values((array) $author)->exec();
     }
     foreach ($this->posts as $post) {
         $db->insertInto('posts', (array) $post)->values((array) $post)->exec();
     }
     foreach ($this->comments as $comment) {
         $db->insertInto('comments', (array) $comment)->values((array) $comment)->exec();
     }
     foreach ($this->categories as $category) {
         $db->insertInto('categories', (array) $category)->values((array) $category)->exec();
     }
     foreach ($this->postsCategories as $postCategory) {
         $db->insertInto('post_categories', (array) $postCategory)->values((array) $postCategory)->exec();
     }
     $this->conn = $conn;
     $this->style = new CakePHP();
     $this->mapper = new Mapper($conn);
     $this->mapper->setStyle($this->style);
     $this->mapper->entityNamespace = __NAMESPACE__ . '\\';
 }
 public function findAllFieldsAnOptions()
 {
     $fields = $this->fieldRepository->findAll();
     foreach ($fields as $k => $field) {
         $fields[$k]['options'] = $this->optionRepository->findBy(['field_id' => $field->getId()], Sql::orderBy('id'));
     }
     return $fields;
 }
Exemplo n.º 4
0
 /**
  * Method used to translate from php method calls to SQL instructions.
  * It is closely related to __call for the Respect\Relational\Sql class.
  */
 protected function build($operation, $parts)
 {
     switch ($operation) {
         // Allows Sql::createTableIfNotExists($columns)
         case 'createTableIfNotExists':
             $this->buildFirstPart($parts);
             return $this->buildParts($parts, '(%s) ');
         default:
             return parent::build($operation, $parts);
     }
 }
Exemplo n.º 5
0
 public function get()
 {
     $limit = 10;
     $start = filter_input(INPUT_GET, 'start') ?: 0;
     $this->collection->setCondition(['id >' => $start]);
     $products = $this->collection->fetchAll(Sql::orderBy('id')->limit($limit));
     foreach ($products as $product) {
         $product->url = $this->editProductRoute->createUri($product->id);
     }
     $response = ['product/productList' => true, 'productList' => $products, 'self' => $_SERVER['REQUEST_URI']];
     if (count($products) == $limit) {
         $response['next'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . "?start=" . end($products)->id;
     }
     if ($start >= 0) {
         $response['prev'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . "?start=" . max($start - $limit, 0);
     }
     return $response;
 }
Exemplo n.º 6
0
 protected function parseCollection(Sql $sql, Collection $collection, $alias, &$aliases, &$conditions)
 {
     $entity = $collection->getName();
     $parent = $collection->getParentName();
     $next = $collection->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $parsedConditions = $this->parseConditions($conditions, $collection, $alias);
     if (!empty($parsedConditions)) {
         $conditions[] = $parsedConditions;
     }
     if (is_null($parentAlias)) {
         return $sql->from($entity);
     } elseif ($collection->isRequired()) {
         $sql->innerJoin($entity);
     } else {
         $sql->leftJoin($entity);
     }
     if ($alias !== $entity) {
         $sql->as($alias);
     }
     $aliasedPk = $alias . '.' . $this->getStyle()->primaryFromTable($entity);
     $aliasedParentPk = $parentAlias . '.' . $this->getStyle()->primaryFromTable($parent);
     if ($entity === $this->getStyle()->manyFromLeftRight($parent, $next) || $entity === $this->getStyle()->manyFromLeftRight($next, $parent)) {
         return $sql->on(array($alias . '.' . $this->getStyle()->foreignFromTable($parent) => $aliasedParentPk));
     } else {
         return $sql->on(array($parentAlias . '.' . $this->getStyle()->foreignFromTable($entity) => $aliasedPk));
     }
 }
Exemplo n.º 7
0
 public function test_extra_sql_on_fetchAll_should_be_applied_on_mapper_sql()
 {
     $expectedComments = array_reverse($this->comments);
     $fetchedComments = $this->mapper->comment->fetchAll(Sql::orderBy('id DESC'));
     $this->assertEquals($expectedComments, $fetchedComments);
 }
Exemplo n.º 8
0
 protected function parseCollection(Sql $sql, Collection $collection, $alias, &$aliases, &$conditions)
 {
     $s = $this->getStyle();
     $entity = $collection->getName();
     $parent = $collection->getParentName();
     $next = $collection->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $conditions = $this->parseConditions($conditions, $collection, $alias) ?: $conditions;
     //No parent collection means it's the first table in the query
     if (is_null($parentAlias)) {
         $sql->from($entity);
         $this->parseMixins($sql, $collection, $entity);
         return;
     } else {
         if ($collection->isRequired()) {
             $sql->innerJoin($entity);
         } else {
             $sql->leftJoin($entity);
         }
         $this->parseMixins($sql, $collection, $entity);
         if ($alias !== $entity) {
             $sql->as($alias);
         }
         $aliasedPk = $alias . '.' . $s->identifier($entity);
         $aliasedParentPk = $parentAlias . '.' . $s->identifier($parent);
         if ($this->hasComposition($entity, $next, $parent)) {
             $onName = $alias . '.' . $s->remoteIdentifier($parent);
             $onAlias = $aliasedParentPk;
         } else {
             $onName = $parentAlias . '.' . $s->remoteIdentifier($entity);
             $onAlias = $aliasedPk;
         }
         return $sql->on(array($onName => $onAlias));
     }
 }
 /**
  * @param Object $field
  * @param String $order
  * @return ArrayObject Options of field
  */
 public function getOptionsOfField($field, $order)
 {
     return $this->repository->findBy(['field_id' => $field->id], Sql::orderBy($order));
 }
Exemplo n.º 10
0
 public function testExtraQuery()
 {
     $mapper = $this->object;
     $comment = $mapper->comment->fetchAll(Sql::limit(1));
     $this->assertEquals(1, count($comment));
 }