views:

115

answers:

4

I understand that you can have class Users extends Zend_Db_Table_Abstract and class User extends Zend_Db_Table_Row_Abstract, setting the $_rowClass property of Users to User.

To create and save a new row, I only know how to do the following:

$users = new Users()
$user = $users->createRow();
$user->name = 'name';
$user->save();

Can you tell me how I can do the follwing instead?

$user = new User();
$user->name = 'name';
$user->save();

As a side note, I'm also considering trying to use the following syntax to get a specific row:

$user = new User($userID);

Given however that this isn't trying to create a new User but to get an existing user, I think the following syntax may be more readable - what do you think?

$users = new Users();
$user = $users->fetchRow($userID);
A: 

well, there are three options:

  • hack this into the zend framework base classes
  • subclass them to your own abstract baseclasses to provide this feature
  • implement this for every class on its own ...

needless to say, the 2nd option is quite the best ... note that this is not so cool:

$user = new User($userID);

you will have to forward the constructor parameter to the abstract super class every time ... this can be quite annoying ...

the last option, of course, would be to use something different, that already works the way you described it, for example motion-twins SPOD, which i personally like a lot more, because it is very lightweight, small and simple and thus integrates well with other frameworks, plus it is better designed and centralized and the code you write with it looks more intuitive ... or it could inspire you, how to patch the zend framework to work for you the way you'd like ...

hope that helps ...

back2dos
A: 

Since I don't know how the Zend DB Table classes and the like work, I offer another option:

It's cumbersome and probably too much work to do for many different classes but you can do something like

class User {
    public function __construct($userID) {
        //initialize the db
        $db = Zend_Db::factory(...);

        $sql = $db->quoteInto("select * from users where user_id = ?", $userID);

        $userObject = $db->query($sql)->fetchObject();

        //now $userObject has all the data of that particular user
    }

    public function save() {
        //first gather this user's data into an array, $userData
        //put the user id into $user_id

        $db->update('users', $userData, 'user_id = $user_id);

    }
}

It's sloppy but that's the only alternative I can come up with off the bat.

Jake
A: 
 class User extends UserModel {

        function __construct($userid=NULL){
           parent::__construct()

           if($userid!=NULL){
                return $this->find($userid);
           }else{
                return $this->createRow();
           }
        }
     }

But honestly, the difference is not worth it.

Rufinus
+1  A: 

Here's how to construct a new row instance without a Table object:

class User extends Zend_Db_Table_Row_Abstract
{
  protected $_tableClass = "Users";
}

$user = new User();
$user->name = "name";
$user->save();  // performs an INSERT

Here's how to construct a row instance for a given row in the database, without a Table object:

class User extends Zend_Db_Table_Row_Abstract
{
  protected $_tableClass = "Users";

  public function __construct(array $config = array())
  {
    if (isset($config["key"]) {
      $table = $this->_getTableFromString($this->_tableClass);
      $rowset = call_user_func_array(array($table, "find"), (array) $config["key"]);
      $row = $rowset->current();
      if ($row != false) {
        $config["data"] = $row->toArray();
        $config["stored"] = true;
      }
    }
    parent::__construct($config);
  }
}

$user = new User(array("key"=>$userid));
$user->name = "name";
$user->save(); // performs an UPDATE

NB: these code samples are not tested, I'll leave that to you.

Bill Karwin