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


Messages In This Thread
Active Record methods ignore protected property - is this intended ? - by El Forum - 10-24-2012, 10:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB