Welcome Guest, Not a member yet? Register   Sign In
Active Record methods ignore protected property - is this intended ?
#1

[eluser]alexbol99[/eluser]
Hi, I'm just learning CI, I'm trying to code some example of models with inheritance.

It works but I would like to know why.

Let we have two model classes "cds" and "books", which of them is stored in its own table in db, and a base class "items" where I implement the common functionality - insert, updated, delete. These methods accept tableName as parameter, so I defined it as base class property which is set in both child classes to "cds" and "books".

It works only when I declare tableName as protected member, otherwise it trying to push tableName as a field into the sql query.

The question is : is this an intended behavior or a trick?

Code:
// Item_model.php
class Item_model extends CI_Model {
var $id = null;
var $title = "";
var $price = 0;

protected $tableName = "";

public function Item_model()
{
  parent::__construct();
  $this->load->database();
}
  
public function add() {
  $this->db->insert($this->tableName, $this);
}

public function update() {
  $this->db->update($this->tableName, $this, array('id' => $this->id) );
}

public function delete($id) {
  $this->db->delete($this->tableName, array('id' => $id) );
}
}

// Cd_model.php

require_once("item_model.php");

class Cd_model extends Item_model {
var $id = null;
var $band = "";

public function Cd_model()
{
  parent::__construct();
  $this->tableName = "cds";
}

public function init($id, $title, $band, $price) {
  $this->id = $id;
  $this->title = $title;
  $this->band = $band;
  $this->price = $price;
}  
}

// Book_model.php

require_once("item_model.php");

class Book_model extends Item_model {
var $id = null;
var $author = "";

public function Book_model()
{
  parent::__construct();
  $this->tableName = "books";
}

public function init($id, $title, $author, $price) {
  $this->id = $id;
  $this->title = $title;
  $this->author = $author;
  $this->price = $price;
}  
}

Thanks
#2

[eluser]Aken[/eluser]
What are you setting that doesn't work? Are you trying to use private or public?
#3

[eluser]alexbol99[/eluser]
When I declare it public:
Code:
public $tableName = "";
it gives error message like this:
Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'tableName' in 'field list'

INSERT INTO `cds` (`id`, `band`, `title`, `price`, `tableName`) VALUES (NULL, 'Madona', 'MDNA', '15', 'cds')

Filename: C:\xampp\htdocs\TowerRecords\system\database\DB_driver.php

Line Number: 330

This is explainable. I can't understand why it works when I set protected.
#4

[eluser]Beginers[/eluser]
Hi alex i think the problem is you don't have a tableName column on your database.

Code:
INSERT INTO `cds` (`id`, `band`, `title`, `price`, `tableName`) VALUES (NULL, ‘Madona’, ‘MDNA’, ‘15’, ‘cds’)
#5

[eluser]alexbol99[/eluser]
Surely this is the reason when I set public.
My question is: why does it works when I set protected:
Code:
protected $tableName = "";

Is this intended behavior for Active Records methods?




Theme © iAndrew 2016 - Forum software by © MyBB