Opencart objects defined, Opencart development tutorials




Predefined objects’ methods are functionalities which are already defined and you can use it directly.


It prevents from DRY, which means Don't Repeat Yourself. Don’t write code unless you have to. Write code only what you need, if you missed these predefined objects’ methods then you may repeat codes.


You can find predefined objects’ methods at system/library


Opencart has many predefined objects’ methods that can be called in Controller and Model.  Like in admin section we can see predefined objects like: config, log, event, load, request, response, db, session, cache, url, langauge, openbay, document, customer, currency, tax, weight, length, cart, encryption, model_setting_event, user.



Database Object of Opencart is
$this->db

Methods are:
  • $this->db->escape($value) - mysql_real_escape_string() on the value passed
  • $this->db->countAffected() - rows affected by an UPDATE query
  • $this->db->getLastId() - last auto increment id same as mysql_insert_id()
  • $this->db->connected() – Pings a server connection, or tries to reconnect if the connection has gone down
$this->db->query($sql)
All SQL queries are run from $this->db->query method which looks like: $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE $this->db->query("REPLACE INTO `" . DB_PREFIX . "category_path` SET ...); $this->db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET ...); $this->db->query("DELETE FROM " . DB_PREFIX . "category WHERE ...); $this->db->query("UPDATE " . DB_PREFIX . "category SET image = ...);

$this->db->query method returns three properties:  
$result->num_rows = $query->num_rows; - gets the number of rows in a results  
$result->row = isset($data[0]) ? $data[0] : array(); - get the first row data  
$result->rows = $data; - get an array of row results

Comments

Popular posts from this blog

Opencart error: Notice: Trying to access array offset on value of type null in ..../vendor/scss.inc.php on line 1753

Creating Class Templates

Fixed: Opencart installation error linux: warning: fopen(system/storage) failed to open stream: Permission denied