How to invoke the remove() method?
I am testing the models in zend project, I have a question about how to invoke the remove() method?
this is the find method I am testing:
<?
class Admin_Model_Member2 extends Custom_Model_Base {
protected function __construct() {
parent::__construct();
}
static function load($id) {
return self::_selectAndBind(
get_class(),
self::getDefaultAdapter()
->select()
->from('member')
->where('id = ?', array($id)),
true);
}
开发者_C百科
function remove() {
return $this->delete();
}
}
Admin_Model_Member2 extends Custom_Model_Base, this is the Custom_Model_Base,
abstract class Custom_Model_Base {
static public function init($default_adapter = null)
{
if (self::$_db_default === null)
{
if (!is_null($default_adapter))
{
if (!$default_adapter instanceof Zend_Db_Adapter_Abstract)
{
throw new Exception('Provided adapter does not extend Zend_Db_Adapter_Abstract');
}
self::$_db_default = $default_adapter;
}
else if (Zend_Registry::isRegistered('db'))
{
self::$_db_default = Zend_Registry::get('db');
}
else
{
throw new Exception('No default adapter provided for the model layer');
}
}
}
public function delete()
{
$where = array();
foreach($this->_primary as $column)
{
$where[$column] = $this->_data[$column];
}
if ($this->_db->delete($this->_table, $where) != 0)
{
foreach($this->_primary as $column)
{
$this->_data[$column] = null;
}
return true;
}
return false;
}
}
this is the test case I write,
public function testCanRemove() {
$data = "80176";
Admin_Model_Member2::init();
$this->_model = Admin_Model_Member2::load($data);
$this->assertTrue($this->_model->remove());
}
I want to test remove() method, so I load($id) a object, but when I invoke $this->_model->remove(), it tells me " Call to a member function delete() on a non-object" in 113 of class Custom_Model_Base which is "if ($this->_db->delete($this->_table, $where) != 0)", what is the problem and how to invoke the remove() method? Admin_Model_Member2::init() can not be called in constructor of Admin_Model_Member2, because the constructor is protected.
$this->_db is not initialized in Custom_Model_Base. You need to call Admin_Model_Member2::init() at some point before you try to delete the record. Perhaps in the constructor of Admin_Model_Member2.
精彩评论