gpt4 book ai didi

php - 蛋糕PHP : Multi-Model Comment system

转载 作者:行者123 更新时间:2023-12-02 07:46:13 25 4
gpt4 key购买 nike

我正在使用 cakephp,我有一个带有两个字段的“评论”模型:“model_type”和“model_id”,以便用单评论模型。

我想知道怎么做。 (可以评论的 Controller 组件“评论”?)

最后我想用一个助手在 View 中列出评论:$comment->show('model_name', 'item_id');这将显示正确分页的评论和用于向项目添加新评论的表单。

谢谢。

最佳答案

编辑:也看到这个:http://bakery.cakephp.org/articles/AD7six/2008/03/13/polymorphic-behavior

解决方案

一个多模型评论系统和分页:

<?php echo $this->element('comments', 
array(
'model_type' => "model_name",
'model_id' => $data['model']['id'],
'order_field' => 'created',
'order' => 'asc')); ?>

模型

方案:

- id- model_type- model_id- content(optional:)- user_id- created- updated- rating- ...

The Comment Model :


// {app}/models/comment.php
class Comment extends AppModel{
public $name = 'Comment';

// List of model that could be commented
protected $model_list = array(
'news' => array(
'name' => 'News', // here it's the model's name
'field' => 'validated'), // A field for validation ( allow comments only on validaded items)
'articles' => array(
'name' => 'Article',
'field' => 'validated')
);

// This is an example
public $belongsTo = array(
'User' => array(
'conditions' => 'User.validated = true'
)
);

public $validate = array(
'model_type' => array(
'rule' => 'checkModelType',
'message' => "Something goes wrong !"
),
'model_id' => array(
'rule' => 'checkModelId',
'message' => "Something goes wrong !"
),
'content' => array(
'rule' => 'notEmpty',
'message' => "Empty content !"
)
);

// Check if the model is commentable
public function checkModelType($data){
$model_type = $data['model_type'];
return in_array($model_type, array_keys($this->model_list));
}

// Check if the item exists and is validated
public function checkModelId($data){
$model_id = intval($data['model_id']);

$model = $this->model_list[$this->data['Comment']['model_type']];

$params = array(
'fields' => array('id', $model['field']),
'conditions' => array(
$model['name'].'.'.$model['field'] => 1, // Validated item
$model['name'].'.id' => $model_id
)
);

// Binding model to Comment Model since there is no $belongsTo
return (bool) ClassRegistry::init($model['name'])->find('first', $params);
}
}

Controller


// {app}/controllers/comments_controller.php
class CommentsController extends AppController
{
public $name = 'Comments';

// Pagination works fine !
public $paginate = array(
'limit' => 15,
'order' => array(
'Comment.created' => 'asc')
);

// The action that lists comments for a specific item (plus pagination and order !)
public function view($model_type, $model_id, $order_field = 'created', $order = 'DESC'){
$conditions = array(
'Comment.model_type' => $model_type,
'Comment.model_id' => $model_id
);

// (optional)
if($order_field != 'created') {
$this->paginate['order'] = array(
'Comment.'.$order_field => $order,
'Comment.created' => 'asc');
}

// Paginate comments
$comments = $this->paginate($conditions);

// This allow to use paginator with requestAction
$paginator = ClassRegistry::getObject('view')->loaded['paginator'];
$paginator->params = $this->params;
return compact('comments', 'paginator');
}

public function add(){
// What you want !
}
}

观点

评论元素

/* {app}/views/elements/comments.ctp
@params : $model_type
* : $model_id
*
* */

$result = $this->requestAction("/comments/view/$model_type/$model_id/$order_field/$order/", $this->passedArgs);
$paginator = $result['paginator'];
$comments = $result['comments'];

$paginator->options(array('url' => $this->passedArgs));
?>
<h2>Comments</h2>

<fieldset class="commentform">
<legend>Add un commentaire</legend>
<?php
// Form
echo $form->create('Comment', array('action' => 'add'));
echo $form->hidden('model_type', array('value' => $model_type));
echo $form->hidden('model_id', array('value' => $model_id));
echo $form->input('content');
<?php
echo $form->end('Send');
?>
</fieldset>
<div class="paginationBar">
<?php
echo $paginator->prev('<< ', null, null, array('class' => 'disabled'));
echo '<span class="pagination">',$paginator->numbers(),'</span>';
echo $paginator->next(' >>', null, null, array('class' => 'disabled'));
?>
</div>
<?php
foreach($comments as $comment){
echo $this->element('comment', array('comment' => $comment));
}
?>
<div class="paginationBar">
<?php
echo $paginator->prev('<< ', null, null, array('class' => 'disabled'));
echo '<span class="pagination">',$paginator->numbers(),'</span>';
echo $paginator->next(' >>', null, null, array('class' => 'disabled'));
?>

<p><br />
<?php
echo $paginator->counter(array('format' => 'Page %page% on %pages%, displayi %current% items of %count%'));
?>
</p>
</div>

注释元素

//{app}/views/elements/comment.ctp
// A single comment view
$id = $comment['Comment']['id'];
?>
<div class="comment">
<p class="com-author">
<span class="com-authorname"><?=$comment['User']['name']?>&nbsp;</span>
<span class="com-date">(<?=$comment['Comment']['created']?>)</span>
</p>
<p class="com-content"><?=$comment['Comment']['content']?></p>
</div>

关于php - 蛋糕PHP : Multi-Model Comment system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6838423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com