Jump to content

model design problem


Destramic

Recommended Posts

hey guys here is my model which extended from every News_Model, User_Model etc.

 

the problem im facing is with the line

 

	$this->mysql = DB::factory($db_type, array('host'     => $db_host,
	                                           'username' => $db_username,
	                                           'password' => $db_password,
                                                   'database' => $db_name), $db_table);

 


becasue when defying the $this->mysql property it sets in the __set magic method which i dont want...but i need the $this->mysql property to be stored in the model class so it can be used in the __call and __set methods.

so basically somehow i wanna set $this->msyql without going troube the magic method __set

<?php

class Model
{		
public function __construct()
{
	$config = Registry::singleton()->config;

	$db_type     = $config->db_type;
	$db_host     = $config->db_host;
	$db_username = $config->db_username;
    $db_password = $config->db_password;
	$db_name     = $config->db_name;	
	$db_table    = $this->get_table_name();

	$this->mysql = DB::factory($db_type, array('host'     => $db_host,
	                                           'username' => $db_username,
	                                           'password' => $db_password,
                                                   'database' => $db_name), $db_table);
}

public function __set($column, $value)
{
	$this->mysql->$column = $value;
}

public function __call($method, $arguments)
{
	if (method_exists($this->mysql, $method))
	{
		$this->mysql->$method($arguments);
	}
}

protected function get_table_name()
{
	$config        = Registry::singleton()->config;

	$model_name    = get_class($this);
	$model_prefix  = $config->model_prefix;
	$prefix_length = strlen($model_prefix);

	$table_name    = substr($model_name, 0, - $prefix_length);
	$table_name    = strtolower($table_name);
	$table_name    = Inflection::pluralize($table_name);

	return $table_name;
}
}

 

how it will then be used to update columns

 


Class News_Model
{

}

Class Some_Controller
{
     $news = new News_Model();
     $news->status = 1;
     $news->save(3);    // will update news column status where id = 3

}

 

if anyone can help me please...i can't think how to tackle this

Link to comment
Share on other sites

ok cool thanks...so how would i do it if i wanted to do something like this

 

$var = 'mysql';
$this->$var = DB::factory($db_type, array('host'     => $db_host,
	                                           'username' => $db_username,
	                                           'password' => $db_password,
                                                   'database' => $db_name), $db_table);

 

cause i wouldnt know what the property name would be...

Link to comment
Share on other sites

Try the object-oriented solution, not the PHP "yeah sure, whatever you want" solution.

abstract class ModelProvider
{

public abstract function __call($method, $arguments);

public abstract function __set($column, $value);

}

class DatabaseModelProvider extends ModelProvider
{

protected $mysql;

public function __construct()
{
	// ...
}

public function __call($method, $arguments)
{
	if (method_exists($this->mysql, $method))
	{
		return $this->mysql->$method($arguments);
	}
}

public function __set($column, $value)
{
	$this->mysql->$column = $value;
}

}

Registry::singleton()->modelprovider = new DatabaseModelProvider();

abstract class Model
{

protected $provider;

public function __construct($provider = null)
{
	if ($provider === null) $provider = Registry::singleton()->modelprovider;
	$this->provider = $provider;
}

public abstract function save($id);

}

class News_Model extends Model
{

public $status;

public function save($id)
{
	// ...
}

}

Link to comment
Share on other sites

yeah thats what i meant haha sorry...but this is what i decided to use afterall instead of having $this->_db  as whatever the dabase type is ie. $this->_mysql etc

 

<?php

class Model
{		
protected $_db;

public function __construct()
{
	$config = Registry::singleton()->config;

	$db_type     = $config->db_type;
	$db_host     = $config->db_host;
	$db_username = $config->db_username;
    $db_password = $config->db_password;
	$db_name     = $config->db_name;	
	$db_table    = $this->get_table_name();

	$this->_db = DB::factory($db_type, array('host'     => $db_host,
	                                         'username' => $db_username,
	                                         'password' => $db_password,
                                                  'database' => $db_name), $db_table);
}

public function __set($column, $value)
{
	$this->_db->$column = $value;
}

public function __call($method, $arguments)
{
	if (method_exists($this->_db, $method))
	{	
		return $this->_db->$method($arguments);
	}
}

public function __get($property)
{
	$property = '_' . $property;

	if (property_exists($this, $property))
	{	
		if ($property !== '$this->_request' ||
		    $property !== '$this->_responce')
        {
			return 	$this->$property;
		}
	}
}

protected function get_table_name()
{
	$config        = Registry::singleton()->config;

	$model_name    = get_class($this);
	$model_prefix  = $config->model_prefix;
	$prefix_length = strlen($model_prefix);

	$table_name    = substr($model_name, 0, - $prefix_length);
	$table_name    = strtolower($table_name);
	$table_name    = Inflection::pluralize($table_name);

	return $table_name;
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.