|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
The Joy of Colletion::appendNew()I just discovered another Solar gem - Solar_Sql_Model_Collection::appendNew()
Up to now, I have been adding related records to an item by interacting with the related model. For example, if I am adding a new comment to a post, I would typically do something like this: $comment = $this->_model->comments->fetchNew(); $data = $this->_request->post('comments', array()); $comment->load($data); $comment->post_id = $post_id; $comment->userid = $this->user->auth->handle; if ($this->_isProcess('save')) { $comment->save(); // yada yada yada. } This works great, but I just discovered that appendNew() can save a some time and takes advantage of the benefits of models. I imagine most of you already know this, but it was a revelation to me. So instead of the above, you can do this instead: // get the post based on id $post = $this->_model->posts->fetch($post_id); // A new related record, with preset data $comment = $post->comments->appendNew(array('userid'=>$this->user->auth->handle)); $data = $this->_request->post('comments', array()); // Load posted data in to the comment record $comment->load($data); // Save. Note that we save the $post, not the $comment. if ($this->_isProcess('save')) { $post->save(); } $this->form = $comment->newForm(array('comment')); This works perfectly, complete with the correct form and form validation based on the comments table. All the related data (post_id, in this case) is automatically set. This might not seem like a really big benefit, but I also experimented with a many-to-many relationship. Imagine that posts and comments were a many-to-many with a through table called posts_comments. The same code above would create a new record in the comments table, plus the required join record in the through table. Hard to beat, if you ask me. Jon _______________________________________________ Solar-talk mailing list Solar-talk@... http://mailman-mail5.webfaction.com/listinfo/solar-talk |
|
|
Re: The Joy of Colletion::appendNew()On Sep 25, 2009, at 20:54 , Jon Elofson wrote: > I just discovered another Solar gem - > Solar_Sql_Model_Collection::appendNew() ... > This works great, but I just discovered that appendNew() can save a > some time and takes advantage of the benefits of models. I imagine > most of you already know this, but it was a revelation to me. > So instead of the above, you can do this instead: > > // get the post based on id > $post = $this->_model->posts->fetch($post_id); > > // A new related record, with preset data > $comment = $post->comments->appendNew(array('userid'=>$this->user- > >auth->handle)); > $data = $this->_request->post('comments', array()); > // Load posted data in to the comment record > $comment->load($data); > > // Save. Note that we save the $post, not the $comment. > if ($this->_isProcess('save')) { > $post->save(); > } > $this->form = $comment->newForm(array('comment')); > > This works perfectly, complete with the correct form and form > validation based on the comments table. > All the related data (post_id, in this case) is automatically set. > This might not seem like a really big benefit, but I also experimented > with a many-to-many relationship. Imagine that posts and comments were > a many-to-many with a through table called posts_comments. The same > code above would create a new record in the comments table, plus the > required join record in the through table. Hard to beat, if you ask > me. Jon -- thanks so much, I'm glad you like it and find it useful. :-) -- Paul M. Jones http://paul-m-jones.com/ _______________________________________________ Solar-talk mailing list Solar-talk@... http://mailman-mail5.webfaction.com/listinfo/solar-talk |
| Free embeddable forum powered by Nabble | Forum Help |