Destramic Posted September 27, 2011 Share Posted September 27, 2011 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 https://forums.phpfreaks.com/topic/247957-model-design-problem/ Share on other sites More sharing options...
requinix Posted September 27, 2011 Share Posted September 27, 2011 Declare $mysql in the class. class Model { protected $mysql; // ... } __set doesn't fire if the variable exists (and is accessible). Link to comment https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1273279 Share on other sites More sharing options...
Destramic Posted September 27, 2011 Author Share Posted September 27, 2011 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 https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1273282 Share on other sites More sharing options...
requinix Posted September 27, 2011 Share Posted September 27, 2011 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 https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1273287 Share on other sites More sharing options...
Destramic Posted September 27, 2011 Author Share Posted September 27, 2011 ummm seems a bit long winded...but maybe the only option i have...thank you...is there way of setting a static property without it being defined as a property?...cause that would help haha Link to comment https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1273336 Share on other sites More sharing options...
requinix Posted September 27, 2011 Share Posted September 27, 2011 That's the proper way to do it. That's how you'd do it in C# and Java and every other language that does object-oriented programming. If you don't care about proper and want quick and easy, that's another thing. What are you doing with static properties? Link to comment https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1273342 Share on other sites More sharing options...
Destramic Posted September 29, 2011 Author Share Posted September 29, 2011 oh ok thanks...i was just wondering if i could assing a value to a static property without actually defying it like protected static $_static; Link to comment https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1274168 Share on other sites More sharing options...
SparK_BR Posted September 29, 2011 Share Posted September 29, 2011 if by "defying" you mean "defining" ok =D a nice concept you should take a look at is dependency injection. either by constructors, by setters, or by configuration objects/injection containers. it's simple and will keep your code decoupled Link to comment https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1274209 Share on other sites More sharing options...
Destramic Posted October 4, 2011 Author Share Posted October 4, 2011 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 https://forums.phpfreaks.com/topic/247957-model-design-problem/#findComment-1275637 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.