2

我是 Yii 初学者,目前正在开发一个标签系统,其中有 3 个表:

  • 问题(id、content、create_d、...等)

  • 标签(id,标签)

  • issue_tag_map (id,tag_id_fk,issue_id_fk)

在我的/Views/Issue/_form中,我添加了一个MultiComplete Extension 来检索多个标签 ID 和标签,

我使用了一个afterSave函数来直接将Issue_id和 autocompleted存储Tag_idIssue_tag_map表中,它是一个HAS_MANY关系。

不幸的是,没有任何东西被退回。

我想知道是否有办法将自动完成的 Tag_ids 存储在临时属性中,然后将其传递给模型的afterSave() 函数。

我一直在寻找一段时间,这一直让我发疯,因为我觉得我错过了一个非常简单的步骤!

任何帮助或任何形式的建议都非常感谢!

多完成Views/Issue/_form

    <?php

     echo $form->labelEx($model, 'Tag');
     $this->widget('application.extension.MultiComplete', array(
    'model' => $model,
    'attribute' => '', //Was thinking of creating a temporary here
    'name' => 'tag_autocomplete',
    'splitter' => ',',
    'sourceUrl' => $this->createUrl('Issue/tagAutoComplete'),
    // Controller/Action path for action we created in step 4.
    // additional javascript options for the autocomplete plugin
    'options' => array(
        'minLength' => '2',
    ),
    'htmlOptions' => array(
        'style' => 'height:20px;',
    ),
));
echo $form->error($model, 'issue_comment_id_fk');
?>

保存后/model/Issue

    protected function afterSave() {
    parent::afterSave();

    $issue_id = Yii::app()->db->getLastInsertID();

    $tag; //here I would explode the attribute retrieved by the view form
    // an SQL with two placeholders ":issue_id" and ":tag_id"
    if (is_array($tag))
        foreach ($tag as $tag_id) {
            $sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";     

            $command = Yii::app()->db->createCommand($sql);
           // replace the placeholder ":issue_id" with the actual issue value

            $command->bindValue(":issue_id", $issue_id, PDO::PARAM_STR);
           // replace the placeholder ":tag_id" with the actual tag_id value

            $command->bindValue(":tag_id", $tag_id, PDO::PARAM_STR);
            $command->execute();
        }
}

这是问题模型中用于填充标签的自动完成 sourceUrl:

    public static function tagAutoComplete($name = '') {

    $sql = 'SELECT id ,tag AS label FROM tag WHERE tag LIKE :tag';
    $name = $name . '%';
    return Yii::app()->db->createCommand($sql)->queryAll(true, array(':tag' => $name));

/controllers/IssueController 中的 actionTagAutoComplete:

// This function will echo a JSON object 
// of this format:
// [{id:id, name: 'name'}]
function actionTagAutocomplete() {

    $term = trim($_GET['term']);
    if ($term != '') {
        $tags = issue::tagAutoComplete($term);
        echo CJSON::encode($tags);
        Yii::app()->end();
    }
}

编辑

窗体中的小部件:

   <div class="row" id="checks" >
    <?php
    echo $form->labelEx($model, 'company',array('title'=>'File Company Distrubution; Companies can be edited by Admins'));

    ?>
   <?php
    $this->widget('application.extension.MultiComplete', array(
        'model' => $model,
        'attribute' => 'company',
        'splitter' => ',',
        'name' => 'company_autocomplete',
        'sourceUrl' => $this->createUrl('becomEn/CompanyAutocomplete'),
        'options' => array(
            'minLength' => '1',
        ),
        'htmlOptions' => array(
            'style' => 'height:20px;',
            'size' => '45',
        ),
    ));
    echo $form->error($model, 'company');
    ?>
</div>

更新功能:

    $model = $this->loadModel($id);
    .....
      if (isset($_POST['News'])) {
         $model->attributes = $_POST['News'];
        $model->companies = $this->getRecordsFromAutocompleteString($_POST['News']  
       ['company']);
    ......
     ......
      getRecordsFromAutocompleteString():
  public static cordsFromAutocompleteString($string) {
    $string = trim($string);
    $stringArray = explode(", ", $string);
    $stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
    $criteria = new CDbCriteria();
    $criteria->select = 'id';
    $criteria->condition = 'company =:company';
    $companies = array();
    foreach ($stringArray as $company) {
        $criteria->params = array(':company' => $company);
        $companies[] = Company::model()->find($criteria);
    }
    return $companies;
}

更新 ,因为“值”属性在此扩展中未正确实现,我提到将此功能扩展到模型:

    public function afterFind() {
       //tag is the attribute used in form
      $this->tag = $this->getAllTagNames();
      parent::afterFind();
   }
4

1 回答 1

1

您应该在模型IssueTag模型中定义的问题和标签之间存在关系(应该是 many_many 关系)。

因此,IssueController当您将数据发送到模型createupdate问题时,您将获得相关的标签(在我的情况下,我得到一个字符串,如“错误,问题,...”)。然后你需要在你的控制器中解析这个字符串,获取相应的模型并将它们分配给相关的标签。

这是一个通用示例:

//In the controller's method where you add/update the record
$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');

这里我调用的方法:

//parse your string ang fetch the related models
public static function getRecordsFromAutocompleteString($string, $model, $field)
        {
            $string = trim($string);
            $stringArray = explode(", ", $string);
            $stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
            return CActiveRecord::model($model)->findAllByAttributes(array($field => $stringArray));
        }

所以现在你的 $issue->tags 是一个包含所有相关标签对象的数组。

在您的afterSave方法中,您将能够:

protected function afterSave() {
    parent::afterSave();

    //$issue_id = Yii::app()->db->getLastInsertID(); Don't need it, yii is already doing it
    foreach ($this->tags as $tag) {
        $sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";     
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValue(":issue_id", $this->id, PDO::PARAM_INT);
        $command->bindValue(":tag_id", $tag->id, PDO::PARAM_INT);
        $command->execute();
        }
}

现在上面的代码是一个基本的解决方案。我鼓励您使用activerecord-relation-behavior 的扩展来保存相关模型。使用此扩展,您无需在afterSave方法中定义任何内容,您只需要做:

$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');
$issue->save(); // all the related models are saved by the extension, no afterSave defined!

然后,您可以通过在自动完成中获取 id 以及标签来优化脚本,并将选定的 id 存储在 Json 数组中。这样您就不必执行 sql 查询getRecordsFromAutocompleteString来获取 id。使用上面提到的扩展,您将能够:

$issue->tags = CJSON::Decode($_POST['idTags']);//will obtain array(1, 13, ...)
$issue->save(); // all the related models are saved by the extension, the extension is handling both models and array for the relation!

编辑:

如果要填写自动完成字段,可以定义以下函数:

public static function appendModelstoString($models, $fieldName)
    {
        $list = array();
        foreach($models as $model)
        {
            $list[] = $model->$fieldName;
        }
        return implode(', ', $list);
    }

您提供字段的名称(在您的情况下tag)和相关模型的列表,它将生成适当的字符串。然后将字符串传递给视图并将其作为自动完成字段的默认值。

回答您的编辑:

在您的控制器中,您说此模型的公司是您从自动完成表单中添加的公司:

$model->companies = $this->getRecordsFromAutocompleteString($_POST['News']  
   ['company']);

因此,如果关联公司不在表单中,则不会将其保存为关联模型。您有 2 个解决方案:

  1. 每次在显示之前将已经存在的相关模型放入表单中的自动完成字段中,这样它们就会再次保存为相关模型,并且不会从相关模型中消失

    $this->widget('application.extensions.multicomplete.MultiComplete', array(
                    'name' => 'people',
                    'value' => (isset($people))?$people:'',
                    'sourceUrl' => array('searchAutocompletePeople'),
    ));
    
  2. 在您的控制器中,在调用之前getRecordsFromAutocompleteString添加模型的现有模型。

    $model->companies = array_merge(
        $model->companies, 
        $this->getRecordsFromAutocompleteString($_POST['News']['company'])
    );
    
于 2012-11-23T09:48:21.440 回答