Reputation: 1946
In my model, I am extending Zend_Db_Table_Abstract. Inside, I have a protected $_name = 'table_name'
.
Now, if I define a constructor, and a private variable private $_var
and define it inside the constructor, the model does not work anymore! When I call $this->createRow()
or anything, nothing happens! Why is this constructor doing this?!
This is what I have:
<?php
class myClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function __construct($var)
{
$this->_var = $var;
}
public function getById($id)
{
$select = $this->select()->where('id =?',$id);
return $this->fetchRow($select);
}
}
This doesn't work! If I remove the __construct(), and the private variable, then it works! Why?
Thanks
Upvotes: 0
Views: 2599
Reputation: 8519
It has been implied in other answers but not stated... You class failed because you overrode the constructor and didn't call the parent constructor so the object did not get created properly.
Normally when these classes are constructed, they are constructed through setOptions() which takes an array in constructor. So if you really have to override the constructor you might try something like:
public function __construct($var, $config = array())
{
$this->_var = $var;
parent::__construct($config);
}
now you should be able to set your variable and pass any configuration values you need to pass.
However it would be best to avoid the constructor and use the supplied init()
method if at all possible.
<?php
class myClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function init()
{
$this->_var = $var;
}
public function getById($id)
{
$select = $this->select()->where('id =?',$id);
return $this->fetchRow($select);
}
}
Upvotes: 0
Reputation: 14184
If you override the constructor of Zend_Db_Table_Abstract
, you should probably call the parent constructor:
class MyClass extends Zend_Db_Table_Abstract
{
protected $_name = 'table_name';
private $_var;
public function __construct($var)
{
$this->_var = $var;
parent::__construct(); // add this
}
// the rest...
}
The parent constructor calls some protected methods _setup()
(which in turn calls _setupDatabaseAdapter()
and _setupTableName()
) and init()
(which is empty in the parent, but you can use to add some processing to the final step of object instantiation).
Upvotes: 2
Reputation: 3105
you have to be more clear when asking questions. write code if you have to.. No one can determine whats wrong with your application with only 2 piece of information.. with that said..
first yo should define your variable outside constructor and initialize inside the constructor if you have to. Your model will not magically know that a db abstract class exist which connects to your database. you will have to create an object inside the model constructor.
here is the code
// DB Abstract class
class Application_Model_DbTable_myTable extends Zend_Db_Table_Abstract
{
protected $_name = 'myTable';
}
// Your model class
class Application_Model_myModel {
private $_var;
public function __construct() {
$this->_var = new Application_Model_DbTable_myTable();
}
}
Hope this was what you were looking for.. and only thing I understood from your question.
dins
Upvotes: 0