Friday, May 28, 2010

Re: HABTM Question.

It isn't just restricted to HABTM, it's a standard thing. But here's a page that refers specifically to HABTM:

http://book.cakephp.org/view/1390/Automagic-Form-Elements

Jeremy Burns
jeremyburns@me.com


On 29 May 2010, at 06:10, Roland Pish wrote:

It worked!!
Finally!!

There was something weird in the database. I submitted the form and
magically the select was populated and funny tags were selected.

I really appreciate your help Jeremy.

One last question, where is in cake manual the convention you advised
to fix this (the use of $funnyTags)?

Best regards.


On 28 mayo, 22:57, Roland Pish <rolandp...@gmail.com> wrote:
Thanks, We are getting closer!!

After using $funnyTags now the select is populated correctly but the
funny tags aren't selected according to what is on the database.

I tried this: echo $form->input('FunnyTag'); but the funny tags aren't
still selected.

On 28 mayo, 22:51, Jeremy Burns <jeremybu...@me.com> wrote:

Ah. Try this:

$funnyTags = $this->Post->FunnyTag->find('list',array('fields'=>array('id','name')));

If that doesn't fix it, try this as well:

echo $form->input('FunnyTag');

Jeremy Burns
jeremybu...@me.com

On 29 May 2010, at 05:46, Roland Pish wrote:

I think I'll post the code and information here. I still don't know
what is happening.

Any help is hugely appreciated.

The tables:
funny_tags: id,name
funny_tags_posts: id,funny_tag_id,post_id
posts: id,name,body

The files:
+ Under models: post.php and funny_tag.php
+ Under controllers: posts_controller.php
+ Under views/posts: form.ctp

Contents of funny_tag.php:
class FunnyTag extends AppModel {
   var $name = 'FunnyTag';
}

Contents of post.php:
class Post extends AppModel {
   var $name = 'Post';

   var $hasAndBelongsToMany = array(
           'FunnyTag' => array(
                   'className' => 'FunnyTag',
                   'joinTable' => 'funny_tags_posts',
                   'foreignKey' => 'post_id',
                   'associationForeignKey' => 'funny_tag_id',
                   /*'with' => 'PostToTag',*/
           ),
   );

}

Contents of posts_controller.php:
class PostsController extends AppController {
   var $name = 'Posts';

   function form($id = null) {
           if (!empty($this->data)) {
                   // form sends data as:
                   // $this->data['Post']['Tag']['Tag'][1] = 1;
                   // $this->data['Post']['Tag']['Tag'][3] = 3;

                   // save the data (auto-handles habtm save)
                   $this->Post->create();
                   if ($this->Post->save($this->data)) {
                           $this->Session->setFlash(__('The Post has been saved.',true));
                           $this->redirect(array('action'=>'form',$this->Post->id));
                   }
                   else {
                           $this->Session->setFlash(__('The Post could not be saved. Please,
try again.'),true);
                   }
           }
           if (empty($this->data)) {
                   $this->data = $this->Post->read(null, $id);
           }
           $funny_tags = $this->Post->FunnyTag-
find('list',array('fields'=>array('id','name')));
           $this->set(compact('funny_tags'));
   }
}

Contents of form.ctp:
<?php echo $form-
create('Post',array('url'=>array('action'=>'form')));?>
   <fieldset>
           <legend><?php __('Post Details');?></legend>
           <?php
           echo $form->input('id');
           echo $form->input('name');
           echo $form->input('body');
           echo $form->input('FunnyTag.FunnyTag');
           ?>
   </fieldset>
<?php echo $form->end('Submit');?>

On 28 mayo, 22:08, Roland Pish <rolandp...@gmail.com> wrote:
Thanks Jeremy.
In fact I did that and didn't solve the problem.

I'm still trying but with no success yet.

Regards.

On 28 mayo, 21:24, Jeremy Burns <jeremybu...@me.com> wrote:

I am *guessing* that your variable that contains your funny_tags is not being set correctly. It is set here:

$tags = $this->Post->Tag->find('list',array('fields'=>array('id','name')));

...so change that to:

$funny_tags = $this->Post->FunnyTag->find('list',array('fields'=>array('id','name')));

...and change this:
$this->set(compact('tags'));

...to this:
$this->set(compact('funny_tags'));

Does that resolve it?

Jeremy Burns
jeremybu...@me.com

On 29 May 2010, at 04:20, Roland Pish wrote:

Hi!
I've been struggling for a long time with something and I don't seem
to get it.
I did the Posts/Tags HABTM Select example from this page:

http://mrphp.com.au/code/working-habtm-form-data-cakephp

So far so good, everything works ok.

I wanted to try the same example but asumming that the table tags is
now called funny_tags (just to make a test with a more complex table
names), so the model now is FunnyTag and the join table is
funny_tags_posts and I did the all the changes needed according to
cake naming conventions.
The problem is that this example no longer populates the select list.

Has anyone looked at this example before?
How can this must be modified to work with table complex names as
funny_tags_posts?

I really appreciate you help.

Regards.

Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpotherswith their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: