Zend Framework

Support default table in constructor of custom Row

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.9.1
  • Fix Version/s: 1.8.0
  • Component/s: Zend_Db_Table
  • Labels:
    None
  • Fix Version Priority:
    Nice to Have

Description

Jeff Bailey writes:

I have a Class that I'm inheriting from Zend_Table_Db_Row_Abstract to use as a customClass in a table, let's call it User. This works fine for retrieving rows and giving me all of my attached logic, update checks and whatnot.

What I'd like to be able to do is use it naturally for creating empty objects, so

$aUser = new User();

In order to do this, I've cooked up a constructor that will work:

class User extends Zend_Db_Table_Row_Abstract
{
    private $_tableName = "UserTable";

    public function __construct(array $config = array())
    {
        if (empty($config["table"])) {
            $config["table"] = new $this->_tableName();
            $info = $config["table"]->info();
            $keys = array_values($info[Zend_Db_Table_Abstract::COLS]);
            $vals = array_fill(0, count($keys), NULL);
            $config["data"] = array_combine($keys, $vals);
        }

        parent::__construct($config);
    }
}

I think that this type of logic would be nice to have as the default constructor for Zend_Db_Table_Row_Abstract.

Issue Links

Activity

Hide
Bill Karwin added a comment -

I've used the fetchNew method, but then it means that the business logic needs to know about the table subclasses.

$userTable = new UserTable();
$aUser = UserTable->fetchNew();

feels less natural than:

$aUser = new User();

and also makes for code that is slightly easier to test with null objects.

Show
Bill Karwin added a comment - I've used the fetchNew method, but then it means that the business logic needs to know about the table subclasses.
$userTable = new UserTable();
$aUser = UserTable->fetchNew();
feels less natural than:
$aUser = new User();
and also makes for code that is slightly easier to test with null objects.
Hide
Bill Karwin added a comment -

We should allow primary key fields to be set. This issue would be much easier to solve if that were true. So I'm marking it as a dependency.

Show
Bill Karwin added a comment - We should allow primary key fields to be set. This issue would be much easier to solve if that were true. So I'm marking it as a dependency.
Hide
Wil Sinclair added a comment -

Please categorize/fix as needed.

Show
Wil Sinclair added a comment - Please categorize/fix as needed.
Hide
Wil Sinclair added a comment -

This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.

Show
Wil Sinclair added a comment - This doesn't appear to have been fixed in 1.5.0. Please update if this is not correct.
Hide
Jurrien Stutterheim added a comment -

Resolved in r.13552

Show
Jurrien Stutterheim added a comment - Resolved in r.13552

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
1d
Original Estimate - 1 day
Remaining:
1d
Remaining Estimate - 1 day
Logged:
Not Specified
Time Spent - Not Specified