David,
That's exactly how I do it. If my form needs an instance of the model I *
always* have a setter for the model (instead of requiring the form to create
it's own instance).
I do this a lot for forms like user registration which needs to have a
unique email address validator (which obviously needs access to the model to
check the DB).
/**
* User row to edit
* @var Users_Model_DbTable_Users
*/
protected $_usersModel;
/**
* Get the user model
*
* @return Users_Model_DbTable_Users
* @throws Zend_Form_Exception
*/
public function getUser()
{
if (!$this->_usersModel) {
throw new Zend_Form_Exception("No user model set");
}
return $this->_usersModel;
}
/**
* Set the users model
*
* @param Users_Model_DbTable_Users $usersModel
* @return Users_Form_EditUser for fluency
*/
public function setUser(Users_Model_Users_Row $usersModel)
{
$this->_usersModel = $usersModel;
return $this;
}
With this method, an exception is thrown if you forget to set the model.
Konr
On Fri, Jan 22, 2010 at 3:00 PM, David Mintz <[email protected]> wrote:
> I have a situation in which certain controller actions need to use an
> instance of my Model_DbTable_Foo (which extends Zend_Db_Table_Abstract), but
> those actions also instantiate my Zend_Form subclasses, which also need
> access to Model_DbTable_Foo for getting data to populate select menu
> options. It's a waste if each instantiates its own instance. On the other
> hand I haven't determined what would be the most sensible way for them to
> share. I suppose the controller could somehow pass the $table instance to
> the form's constructor.
>
> Suggestions?
>
> --
> Support real health care reform:
> http://phimg.org/
>
> --
> David Mintz
> http://davidmintz.org/
>
>
>