I am using cakephp2.0 and want to integrate comment plugin but i got nothing .I was using commentDc plugin but its not working as my requirements.Because i am integreating my users login system with xenforo and commentDc plugin use Auth component so its not working properly.
Please let me know is there any simple comment plugin which i can integrate and modify as my needs.
Thanks,
Here's how I set up comments:
Comments table fields:
id
parent_type, matches the model name of the parent
parent_id
content
user_id, the sender
In any model that you want to be commentable, at this your associations:
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'parent_id',
'conditions' => array('Comment.parent_type' => 'question')
)
);
This is a view element:
<?php
/*
set variables:
$data : data of the parent
$type : the type of the parent
*/
if(!isset($name)) {
$name = 0;
}
foreach($data['Comment'] as $comment){
echo '<div class="comment">'.$comment['content'].
' - '.$this->Html->link($comment['User']['username'],array('controller'=>'users','action'=>'view',$comment['User']['id']))
.'</div>';
}
echo $this->Form->create(null, array('url' => '/comments/add','id'=>'qCommentForm'));
echo $this->Form->input('Comment.parent_id', array('type'=>'hidden','value'=>$data[$type]['id']));
echo $this->Form->input('Comment.parent_type', array('type'=>'hidden','value'=>$type));
echo $this->Form->textarea('Comment.content',array('div'=>'false','class'=>'small','label'=>false));
echo $this->Form->submit(__('Leave comment'),array('div'=>'false','class'=>'small'));
echo $this->Form->end();
?>
Then, in the view view for your model, add this (assuming you named the element comment.ctp:
<?php echo $this->element('comment',array('data'=>$modelData,'type'=>'MyModel')) ?>
Related
i am trying to make an simple cakephp aplication!
I have a form that creates a new article.. my problem is that i have an input field for the artice slug but when i cakephp submits te form the slug field in database remains empty..
here is the add method from my articleController
public function add(){
$article = $this->Articles->newEntity(); //gffdgfd
if ($this->request->is('post')){
$this->Articles->patchEntity($article, $this->request->data());
if($this->Articles->save($article)){
$this->Flash->success(__('Your Article has been saved!'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Cannot save article! Please try again!!'));
}
$this->set('article', $article);
}
and my add.ctp
<h1>Add Article</h1>
<?php
echo $this->Form->create($article);
echo $this->Form->control('user_id', ['type' => 'hidden', 'value'=> 1 ]);
echo $this->Form->control('published', ['type' => 'hidden', 'value'=> 1 ]);
echo $this->Form->control('title');
echo $this->Form->control('slug');
echo $this->Form->control('body', ['rows' => 5 ]);
echo $this->Form->button(__('Save Article'), ['class' => 'button', 'style' => 'margin-right:10px; margin-left:10px']);
echo $this->Html->link('Back', ['action' => 'index'], ['class' => 'button']);
echo $this->Form->end();
?>
If slug field is present in your request data, then you should check if this field is accessible for assignment in your entity. Look at file src/Model/Entity/Article.php, on top of class body you will have an array named $_accessible - check if your slug field is present, and if not, set it to true:
protected $_accessible = [
/* other fields */
'slug' => true
];
Please check more about assignment of properties in docs: CakePHP 3 Entities - Mass Assignment
I have a form in CakePHP as following
addticket.ctp
<html>
<?php
echo $this->Form->create('Ticket', array('url' => array('controller' => 'tickets', 'action' =>'addtickets'),
'enctype' => 'multipart/form-data'));
echo $this->Form->input('title',array('label'=>'Title'));
echo $this->Form->input('attachment', array('between'=>'<br />','type'=>'file',
'label'=>'Attachment'));
echo $this->Form->input('stepstoreproduce',array('label'=>'Steps To Reproduce'));
echo $this->Form->input('category',array(
'label'=>'Category',
'options'=>array(
'IT Support',
'IT HelpDesk'
)));
echo $this->Form->input('priority',array(
'label'=>'Priority',
'options'=>array(
'Low',
'Medium',
'High'
)));
echo $this->Form->input('Comment.comment',array(
'type'=>'textarea',
'label'=>'Comments'
));
echo $form->input('public',array('type'=>'radio',
'options' => array(
'1'=>'Yes',
'0'=>'No',
),
'default'=>'0'));
echo $form->input('created_by',array('value'=>$_SESSION['Auth']['User']['id'],'type'=>'hidden'));
echo $this->Form->end('Submit Ticket');
?>
</html>
And I have model ticket.php with following code
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'ticket_id'
)
);
And I have tickets_controller with addticket() function as follows
if ($this->Ticket->saveAll($this->data)){
$this->Session->setFlash('Ticket created');
}
else {
$this->Session->setFlash('Cannot create a ticket');
}
Problem
The problem is that there are two tables in my database:
1.tickets
2.Comments (ticket_id is foreign key )
I want to insert tickets data to tickets table and comments data to comments table. As I know that changing name to Comment.comment will insert data in comments table.
But I want to add userid and ticket_id in comment table and
created_by in tickets & comments both.
Please Help
Thanks in advance
I think you need to explicitly put a Model name in front of every input field you have. For example:
echo $this->Form->input('Ticket.title',array('label'=>'Title'));
echo $this->Form->input('Ticket.attachment', array('between'=>'<br />','type'=>'file',
'label'=>'Attachment'));
CakePHP will automatically set ticket_id in your Comment data.
See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array for more detail.
And I think you should set the user_id in the controller instead of set it as a hidden field in a view like:
$this->data['Ticket']['created_by'] = $this->Auth->user('id');
$this->data['Comment']['created_by'] = $this->Auth->user('id');
Hello my problem is i try to save a new relation between a shop and a payment method
the relation is habtm... shop and payment already exist. i want to ad more payment methods.
But always when i save ,the old payment realtion in the shop_payment table is only updated, not a second one saved....
i read a lot i set unique to false but nothing changes that.
Anyone got an idea?
Model
class Payment extends AppModel {
var $hasAndBelongsToMany = array(
'Mainshop'=>array('className'=>'Mainshop', 'unique'=>'false')
);
}
View
echo $this->Form->create('Mainshop');
echo $this->Form->input('name',array('default'=>$mainshop['Mainshop']['name']));
echo $this->Form->input('Payment.id', array(
'type' => 'select',
'options' => array($payments),
));
echo $this->Form->input('id', array('type'=>'hidden','value'=>$mainshop['Mainshop'] ['id']));
echo $this->Form->end('Edit Shop');?>
Controller
if (!empty($this->data)){
$this->Mainshop->save($this->data);
$this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}
My recommendation defines the relationship with all fields in model:
var $hasAndBelongsToMany = array(
'Mainshop'=>array(
'className'=>'Mainshop',
'unique'=>'false',
'joinTable' => 'shop_payments',
'foreignKey' => 'payments_id',
'associationForeignKey' => 'shop_id'
)
);
In the controller add create():
if (!empty($this->data)){
$this->Mainshop->create();
$this->Mainshop->save($this->data);
$this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}
I am writing a simple poll for Cakephp. I have created tables polls and poll_votes. Poll contains 7 columns: id,question,numberofanswers,answer1,answer2,answer3,answer4. PollVote contains 4: id,poll_id,ip,vote. If this works, I might be adding a new table called PollAnswers as a way to get rid of the answer-limit.
Unfortunately the view I made causes security errors. I would prefer to use the form-helper as a whole, but I have not figured out how since I need radio-buttons with multiple options that have id's 1 to 4, but show answer1 to answer4.
[daweb#directadmin01 public_html]$ cat models/poll.php
<?php
class Poll extends AppModel {
var $name = 'Poll';
var $displayField = 'question';
var $hasMany = array(
'PollVote' => array(
'className' => 'PollVote',
'foreignKey' => 'poll_id',
'dependent' => false
)
);
}
?>
[daweb#directadmin01 public_html]$ cat models/poll_vote.php
<?php
class PollVote extends AppModel {
var $name = 'PollVote';
var $belongsTo = array(
'Poll' => array(
'className' => 'Poll',
'foreignKey' => 'poll_id'
)
);
}
?>
[daweb#directadmin01 public_html]$ cat views/polls/view.ctp
<h2><?=$polls[0]['Poll']['question']?></h2>
<?php
echo $this->Form->create('Poll');
?>
<br /><p>
<select name="PollName" id="PollFieldId">
<option value="0"><?=$polls[0]['Poll']['answer1']?></option>
<option value="1"><?=$polls[0]['Poll']['answer2']?></option>
<option value="2"><?=$polls[0]['Poll']['answer3']?></option>
<option value="3"><?=$polls[0]['Poll']['answer4']?></option>
</select>
<?php
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Submit');
?>
</p>
[daweb#directadmin01 public_html]$
I am honestly not sure I am creating the Poll application with the right design, so general advice is welcome. Unfortunately I was unable to find examples of polls written in Cakephp to help me.
your design is fine, as long as you only need 4 questions for each poll. Change numberofanswers to pollVote_count and use counterCache
Take a look at Database design for a survey, although that might be overkill for your purpose.
for the radio input, that should be in the pollVotes controller/add action, not in the polls/view:
echo $this->Form->create('PollVote');
echo $this->Form->input('poll_id');
echo $this->Form->input('vote',array('type' => 'radio','options' => $polls[0]['Poll']));
echo $this->Form->end('Submit');
Is there a way to paginate comments in cakePHP with a tree behavior? Should I use tree behavior anyway or to make my own code to view the comments?
Sorry, this has been asked before. I found an article: Managing Hierarchical Data in MySQL
...and I will post my solution.
Nope, I didn't find a good solution. I don't want to reinvent the wheel, I do want to make it in the cake way.
I wrote a helper for recursively print the comments:
# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){
if (empty($comments) || !is_array($comments)) return false;
echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
if (is_array($comments)){
foreach($comments as $comment):
?>
<li id="<?php echo $comment['Forum']['id']; ?>">
<div class="inner">
<h4><?php echo $comment['Forum']['title']; ?></h4>
<div class="meta">
<?php echo $comment['User']['first_name']; ?>
</div>
<div class="content">
<?php echo $comment['Forum']['content']; ?>
</div>
</div>
<?php
if (isset ($comment['children']))
if (is_array($comment['children'])){
if (!empty($comment['children'])) $this->printComments($comment['children']);
}
echo '</li>';
endforeach;
}
else{
echo '<li>'.$comment['Forum']['title'].'</li>';
}
echo '</ul>';
}
Here is how I retrieve the data from the database:
# controllers/forums.php
$this->set('tree', $this->Forum->find('threaded'););
The problem is how to paginate the comments?
Currently I'm using 2 tables, one for the root and 2nd for the threaded comments. I didn't find a solution.
You have to do it manually in controller by paginate.
The key is properly order mysql query to find comment not by id by firstly by parent_id and then id (columns are just example) - that would work for 2 level comments:
$this->paginate = array(
'recursive' => -1,
'conditions' => array(
'Comment.blog_id' => 0,
),
'order' => array('parent_id', 'id'),
'limit' => 10
);
$this->set('comments', $this->paginate('Comment'));
You can't. What I did is a link which switched between full threaded comments view AND flat paginated list.
IMHO: do you think that paginate comments is a good idea? just minimize space comments take on the page, and put them into one float. don't make your user to click too many links...
You can achieve this as
Your modal code:
var $hasMany = array(
'Pages' => array(
'className' => 'Page',
'foreignKey' => 'parent_id',
)
);
Your Controller code:
$this->paginate = array(
'Page' => array(
'contain' => array(
'Pages'
),
'conditions' => array(
'Page.parent_id' => null
)
)
);
$pages = $this->paginate();
You should use tree behavior and order by lft.