How did you setup the database for the comments field? It must of
included the model and foreign_key name i take it? Did you include
this in all your db tables? and if so why?
On Jan 2, 1:12 pm, cricket <zijn.digi...@gmail.com> wrote:
> On Sun, Jan 2, 2011 at 12:46 PM, sixthpoint <sixthpo...@gmail.com> wrote:
> > I am having troubles setting up this association for my application. I
> > am trying to do it in the least complicated way possible, but I am
> > rather new to cake.
>
> > I have 2 models (Documents, Courses) which each need to have comments.
> > I do no want to use multiple tables I would rather store all the
> > comments in the same table in the database using cakes HABTM
> > association. So I would only need one model (Comments) to essentially
> > keep track of both (Documents, Courses) models.
>
> > How is the best way to accomplish this?
>
> You could use hasMany/belongsTo instead. Just include columns "model"
> & "foreign_key" in the comments table. This is how I did that in an
> old app (updated with your model names):
>
> Course model:
>
> public $hasMany = array(
> 'Comment' => array(
> 'className' => 'Comment',
> 'foreignKey' => 'foreign_key',
> 'conditions' => array('Comment.model' => 'Course'),
> 'dependent' => true
> )
> );
>
> (Document would be the same idea)
>
> Comment model:
>
> public $belongsTo = array(
> 'Course' => array(
> 'className' => 'Course',
> 'conditions' => array(
> 'Comment.foreign_key' => 'Course.id',
> 'Comment.model' => 'Course'
> ),
> 'order' => '',
> 'foreignKey' => false
> ),
> 'Document' => array(
> 'className' => 'Document',
> 'conditions' => array(
> 'Comment.foreign_key' => 'Document.id',
> 'Comment.model' => 'Document'
> ),
> 'order' => '',
> 'foreignKey' => false
> ),
> 'User'
> );
>
> Comment also included this threaded() method, which is called from an
> associated controller. For example, in CoursesController::view, we
> want a specific Course plus all of the related Comments.
> $comments = $this->Course->Comment->threaded($data['Course']['id'], 'Course');
>
> public function threaded($key = null, $model = null)
> {
> if (!$key || is_null($model)) return null;
>
> $model = ucfirst($model);
>
> $filters = array(
> 'conditions' => array(
> $this->alias.'.model' => $model,
> $this->alias.'.foreign_key' => $key
> ),
> 'fields' => array(
> $this->alias.'.id',
> $this->alias.'.created',
> $this->alias.'.parent_id',
> $this->alias.'.lft',
> $this->alias.'.rght',
> $this->alias.'.model',
> $this->alias.'.foreign_key',
> $this->alias.'.user_id',
> $this->alias.'.title',
> $this->alias.'.body'
> ),
> 'order' => array($this->alias.'.lft' => 'ASC'),
> 'contain' => array(
> 'User' => array(
> 'fields' => array('User.id', 'User.name')
> ),
> $model => array(
> 'fields' => array("${model}.id", "${model}.user_id")
> )
> )
> );
>
> $data = $this->find('threaded', $filters);
>
> $data['total'] = $this->_getRecordCount($data);
>
> return $data;
>
> }
>
> All of this worked fine, although I've since updated everything to use
> a Comments plugin that I wrote.
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:
Post a Comment