2008年12月2日星期二

Re: [fw-mvc] Zend_Form and Doctrine Integration

Looks good. There's still no reason why you need to *extend* Doctrine_Record I think. You can have your itself implement an isValid() method which calls the forms isValid() method as well. Ditto for the populate() method.


On Mon, Dec 1, 2008 at 3:40 AM, Емил Иванов / Emil Ivanov <emil.vladev@gmail.com> wrote:
2008/11/28 Abraham Block <atblock@gmail.com>:
> Also, although both you and I extended Zend_Form for our purposes, its
> probably better to not extend zend form and instead inject it...I realized
> this just now because i'm extending *my* version of Zend_From which
> introduces an dependency making it useless for others...

Well, this is not possible - I need to override the isValid() and
populate() methods so that the data is validated against the model.
Also, this is a form for doctrine model - It's natural to have
Doctrine_Record as a dependency.

Here are some use cases:

class Book extends Doctrine_Record
{
   public function setTableDefinition()
   {
       $this->hasColumn('title', 'string', 128, array ('notnull' => true));
       $this->hasColumn('authorId', 'integer', array ('notnull' => false));
   }

   public function setUp()
   {
       $this->hasOne('Author', array (
           'local' => 'authorId',
           'foreign' => 'id'
       ));
   }

   public function __toString()
   {
       return $this->title . ($this->authorId ? ' ' . (string)
$this->Author : '');
   }
}

class Author extends Doctrine_Record
{
   public function setTableDefinition()
   {
       $this->hasColumn('firstName', 'string', 32, array ('notnull' => true));
       $this->hasColumn('lastName', 'string', 32, array ('notnull' => true));
   }

   public function setUp()
   {
       $this->hasMany('Book as Books', array (
           'local' => 'id',
           'foreign' => 'authorId'
       ));
   }

   public function __toString()
   {
       return $this->firstName . ' ' . $this->lastName;
   }
}

Consider the two model above - a Book that has one author, and an
Author that has many books.

Now, if I generate a form for the book with
$form = new ZendX_Form_Doctrine(new Book());
I will get a form that has a text field for the title of the book and
a select box with all (overridable) the Authors in the system to
choose an author from.

If I create a form for an author - I will get two text fields for the
names, and a multi-select for the books.

Even if I get the author or the book from the database and create a
form for that instance - I will get the form pre-populated with the
data from the model - even having the related options in the select
selected.

Taking this further I can do:

$form = new ZendX_Form_Doctrine(new Author());

if ($form->isValid($data)) {
  $form->getModel()->save();
}

What that will do is create a form, validate it against the MODEL (it
will see if the model is valid, not just the form itself) and save it
to the database - in 3 lines.

No duplication!

Hope that will get you more interested. :)
--
My place to share my ideas:
http://vladev.blogspot.com
http://carutoo.com

没有评论: