/** * Create POST route for retrieving rows from table and the number of rows from table */ protected function findRowsRoute() { // Run Qbuild query for specific table $this->app->post('/{table}', function ($request, $response, $args) { // Make sure response is JSON $newResponse = $response->withHeader('Content-type', 'application/json'); $query = $request->getParam('query'); $query_method = $request->getParam('query_method'); // MySQL table to query $table = ORM::for_table($args['table']); if (!empty($query)) { foreach ($query as $command) { $params = $command['params']; // Build query switch ($command['method']) { case 'where': $table->where($params[0], $params[1]); break; case 'where_in': $table->where_in($params[0], $params[1]); break; case 'where_not_in': $table->where_not_in($params[0], $params[1]); break; case 'where_like': $table->where_like($params[0], $params[1]); break; case 'where_not_like': $table->where_not_like($params[0], $params[1]); break; case 'where_any_is': $table->where_any_is($params[0], $params[1]); break; case 'where_not_equal': $table->where_not_equal($params[0], $params[1]); break; case 'where_lt': $table->where_lt($params[0], $params[1]); break; case 'where_gt': $table->where_gt($params[0], $params[1]); break; case 'where_lte': $table->where_lte($params[0], $params[1]); break; case 'where_gte': $table->where_gte($params[0], $params[1]); break; case 'group_by': $table->group_by($params); break; case 'join': $table->join($params[0], array($params[1], $params[2], $params[3])); break; case 'inner_join': $table->inner_join($params[0], array($params[1], $params[2], $params[3])); break; case 'left_outer_join': $table->left_outer_join($params[0], array($params[1], $params[2], $params[3])); break; case 'right_outer_join': $table->right_outer_join($params[0], array($params[1], $params[2], $params[3])); break; case 'limit': $table->limit($params); break; case 'offset': $table->offset($params); break; case 'order_by_asc': $table->order_by_asc($params); break; case 'order_by_desc': $table->order_by_desc($params); break; case 'select': foreach ($params as $column) { $table->select($column); } break; default: break; } } } // Attempt to run query try { switch ($query_method) { case 'find': $results = array(); // Returns rows found for "find" $rows = $table->find_many(); foreach ($rows as $row) { $result = $row->as_array(); $results[] = $result; } $response_body = array("results" => $results); break; case 'count': // Get number of rows for "count" and return response $count = intval($table->count()); $response_body = array("count" => $count); break; default: break; } } catch (Exception $e) { error_log(ORM::get_last_statement()->queryString); error_log($e->getMessage()); $response_body = array("error" => $e->getMessage()); } // Add data to response in JSON format $newResponse->write(json_encode($response_body)); return $newResponse; }); }
/** * Execute the SELECT query that has been built up by chaining methods * on this class. Return an array of rows as associative arrays. */ protected function _run() { // allow parent method to run if (!$this->cnt_query) { // need a way to make sure this is set if ($this->_is_raw_query) { if (is_array($this->_raw_parameters)) { foreach ($this->_raw_parameters as $k => $v) { if (!is_numeric($v)) { $v = '"' . $v . '"'; } $this->_raw_query = str_replace(':' . $k, $v, $this->_raw_query); } } } return parent::_run(); } // we are not caching the COUNT - @todo - implement caching $query = $this->_build_select(); parent::_execute($query, $this->_values, $this->_connection_name); $statement = parent::get_last_statement(); $rows = array(); while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; } // reset Idiorm bound values $this->_values = array(); return $rows; }
public function create($create_absent = false) { if (!$create_absent && !$this->quantity) { return false; } $new = $this->create_record(); $new->set($this->from(array('model', 'sku', 'quantity', 'manufacturer_id', 'image', 'shipping', 'price', 'minimum', 'status'))); if (property_exists($this, $this->id_field) && (int) $this->{$this->id_field} > 0) { $new->set($this->id_field, $this->id()); } $new->set_expr('date_added', 'NOW()'); $new->set_expr('date_modified', 'NOW()'); $new->set_expr('date_available', 'NOW()'); //p($new); $created = $new->save(); $result = array('id' => $this->id(), 'created' => $created); if ($created) { $this->id($new->id()); $this->to_store(); $this->create_description(); //$this->prepare_categories(); //$this->to_category(); $this->to_url_alias($new->id()); if ($this->copy_image) { $result['image'] = $this->download_image(); $this->create_extra_images(); } } else { $result += array('error' => \ORM::get_last_statement()); } return $result; }
public static function get_last_statement() { return \ORM::get_last_statement(); }
public function post(\Classes\Opencart\Product $product, $create_absent = false) { if (!$create_absent && !$product->quantity) { return false; } $new = \ORM::for_table($this->table('primary'))->create(); $new->set($this->from(array('model', 'quantity', 'image', 'shipping', 'price', 'minimum', 'status'), $product)); if (property_exists($product, $this->id_field)) { $new->set($this->id_field, $product->product_id); } $new->set_expr('date_added', 'NOW()'); $new->set_expr('date_modified', 'NOW()'); $created = $new->save(); $result = array('id' => $product->product_id, 'created' => $created); if ($created) { $product->{$this->id_field} = $new->id(); $this->to_store($product); $this->create_description($product); $this->prepare_categories($product->category); $this->to_category($product); $this->to_url_alias($product); if ($product->copy_image) { $result['image'] = $product->download_image(); } } else { $result += array('error' => \ORM::get_last_statement()); } return $result; }
public function testCustomConnectionName() { $person3 = Model::factory('ModelWithCustomConnection')->find_one(1); $statement = ORM::get_last_statement(); $this->assertInstanceOf('MockDifferentPDOStatement', $statement); }