我在应用程序中添加了cakePHP标记功能。我有一个名为项目的表,可以关联许多标签。所以我有一个带有id和tag varchar字段的标签表。带有标签varchar字段的项目表,以及具有id,tag_id和project_id字段的链接表projects_tags。当我将标签输入添加到我的视图时,由于某种原因,存在的标签会在下拉框中填充。任何人都可以弄明白我在这里做错了吗?
这是我的标签模型:
<?php
class ProjectTag extends AppModel {
var $name = 'ProjectTag';
var $hasAndBelongsToMany = array('Tag' =>
array('className' => 'Tag',
'joinTable' => 'tags',
'foreignKey' => 'tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => '',
),
array('className' => 'Project',
'joinTable' => 'projects',
'foreignKey' => 'project_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => '',
)
);
}
?>
这是我的添加项目视图add.ctp:
<?php
echo $form->create('Project');
echo $form->input('title', array('label' => 'Title'));
echo $form->input('website', array('label' => 'Website'));
echo $form->input('description', array('label' => 'Description'));
echo $form->input('language_id', array('label' => 'Language'));
echo $form->input('tags');
echo $form->end('Post project');
?>
以下是标记行为模型:
<?php
class TagBehavior extends ModelBehavior {
/**
* Initiate behaviour for the model using specified settings.
*
* @param object $model Model using the behaviour
* @param array $settings Settings to override for model.
*
* @access public
*/
function setup(&$model, $settings = array()) {
$default = array( 'table_label' => 'tags', 'tag_label' => 'tag', 'separator' => ',');
if (!isset($this->settings[$model->name])) {
$this->settings[$model->name] = $default;
}
$this->settings[$model->name] = array_merge($this->settings[$model->name], ife(is_array($settings), $settings, array()));
}
/**
* Run before a model is saved, used to set up tag for model.
*
* @param object $model Model about to be saved.
*
* @access public
* @since 1.0
*/
function beforeSave(&$model) {
// Define the new tag model
$Tag =& new Tag;
if ($model->hasField($this->settings[$model->name]['table_label'])
&& $Tag->hasField($this->settings[$model->name]['tag_label'])) {
// Parse out all of the
$tag_list = $this->_parseTag($model->data[$model->name][$this->settings[$model->name]['table_label']], $this->settings[$model->name]);
$tag_info = array(); // New tag array to store tag id and names from db
foreach($tag_list as $t) {
if ($res = $Tag->find($this->settings[$model->name]['tag_label'] . " LIKE '" . $t . "'")) {
$tag_info[] = $res['Tag']['id'];
} else {
$Tag->save(array('id'=>'',$this->settings[$model->name]['tag_label']=>$t));
$tag_info[] = sprintf($Tag->getLastInsertID());
}
unset($res);
}
// This prepares the linking table data...
$model->data['Tag']['Tag'] = $tag_info;
// This formats the tags field before save...
$model->data[$model->name][$this->settings[$model->name]['table_label']] = implode(', ', $tag_list);
}
return true;
}
/**
* Parse the tag string and return a properly formatted array
*
* @param string $string String.
* @param array $settings Settings to use (looks for 'separator' and 'length')
*
* @return string Tag for given string.
*
* @access private
*/
function _parseTag($string, $settings) {
$string = strtolower($string);
$string = preg_replace('/[^a-z0-9' . $settings['separator'] . ' ]/i', '', $string);
$string = preg_replace('/' . $settings['separator'] . '[' . $settings['separator'] . ']*/', $settings['separator'], $string);
$string_array = preg_split('/' . $settings['separator'] . '/', $string);
$return_array = array();
foreach($string_array as $t) {
$t = ucwords(trim($t));
if (strlen($t)>0) {
$return_array[] = $t;
}
}
return $return_array;
}
}
?>
这是我的project_controller add:
function add() {
$this->set("Languages", $this->Project->Language->find("list"));
if (!empty($this->data)) {
if ($this->Project->save($this->data)) {
$this->Session->setFlash('Project added.');
$this->redirect(array('action' => 'index'));
}
}
}
答案 0 :(得分:1)
这是因为模型中的关系。 CakePHP将自动关联标签并为您创建下拉列表。这是拥有相关领域的目的。它基本上是在说,“这个标签需要存在于这个'标签'表中。”因此,它会使用标签中的数据预先填充它。
取出HABTM关系,它将返回测试盒。
答案 1 :(得分:1)
cdburgess关于下拉列表出现的原因是正确的,但您可以通过调整Cake“magic”字段来覆盖合理的默认值以满足您自己的需求:
echo $form->input('tags', array( 'type' => 'text' ) );
我不会发誓语法,但这会将标签输入创建为文本框。
IMO,这是一个比在其他地方放弃拥有如此强大力量的协会更好的选择。