Beispiel #1
0
 /**
  * Search objects from data source
  *
  * It is highly recommended to instantiate the $what object using Search_Object::instantiate() in order to initialize all properties as unset and build a correct search object.
  * If some properties are an not-loaded objects, the search will be done on the object identifier, without joins to the linked object.
  * If some properties are loaded objects : if the object comes from a read, the search will be done on the object identifier, without join. If object is not linked to data-link, the search is done with the linked object as others search criterion.
  *
  * @param $what       object|array source object for filter, or filter array (need class_name) only set properties will be used for search
  * @param $class_name string must be set if $what is a filter array and not an object
  * @param $options    Option[] some options for advanced search
  * @return object[] a collection of read objects
  */
 public function search($what, $class_name = null, $options = [])
 {
     if (!isset($class_name)) {
         $class_name = get_class($what);
     }
     $class_name = Builder::className($class_name);
     $builder = new Select($class_name, null, $what, $this, $options);
     $query = $builder->buildQuery();
     $this->setContext($builder->getJoins()->getClassNames());
     $result_set = $this->connection->query($query);
     if ($options) {
         $this->getRowsCount($result_set, 'SELECT', $options);
     }
     $objects = $this->fetchAll($class_name, $options, $result_set);
     $this->afterReadMultiple($objects, $options);
     return $objects;
 }
 /**
  * Sets current restriction using $builder's joins foreign classes
  *
  * If current restrictions exist before call of restrict(), they are reset by this call.
  *
  * @param $builder Select
  */
 private function restrict(Select $builder)
 {
     $this->current_restrictions = [];
     if ($this->restrictions) {
         foreach ($builder->getJoins()->getJoins() as $join) {
             if (isset($join->foreign_class)) {
                 $restrictions = $this->getRestrictions($join->foreign_class);
                 foreach ($restrictions as $restriction) {
                     $this->applyRestriction($builder, $join->foreign_class, $restriction);
                 }
             }
         }
     }
 }
Beispiel #3
0
 /**
  * @param $filter_object object|array source object for filter, set properties will be used for
  *                       search. Can be an array associating properties names to corresponding
  *                       search value too.
  * @param $options       Option[] some options for advanced search
  * @return string
  */
 public function prepareQuery($filter_object = null, $options = [])
 {
     $filter_object = $this->objectToProperties($filter_object);
     $sql_select_builder = new Sql\Builder\Select($this->class_name, $this->columns, $filter_object, $this->link, $options);
     $query = $sql_select_builder->buildQuery();
     $this->path_classes = $sql_select_builder->getJoins()->getClasses();
     $this->link->setContext(array_merge($sql_select_builder->getJoins()->getClassNames(), $sql_select_builder->getJoins()->getLinkedTables()));
     return $query;
 }
Beispiel #4
0
 public function testWhereReverseJoinQuery()
 {
     $builder = new Select(Order::class, ['date', 'number', 'Order_Line->order.number', 'Order_Line->order.quantity'], ['Order_Line->order.number' => '2']);
     $this->assume(__METHOD__, $builder->buildQuery(), 'SELECT t0.`date`, t0.`number`, t1.`number` AS `Order_Line->order.number`, t1.`quantity` AS `Order_Line->order.quantity`' . LF . 'FROM `orders` t0' . LF . 'LEFT JOIN `orders_lines` t1 ON t1.id_order = t0.id' . LF . 'WHERE t1.`number` = "2"');
 }
Beispiel #5
0
 public function testLeftMatch()
 {
     $builder = new Select(Order::class, null, ['number' => Func::leftMatch('N01181355010')]);
     $this->assume(__METHOD__, $builder->buildQuery(), 'SELECT t0.*' . LF . 'FROM `orders` t0' . LF . 'WHERE t0.`number` = LEFT("N01181355010", LENGTH(t0.`number`))');
 }