沃梦达 / 编程问答 / php问题 / 正文

Symfony 插件 sfDoctrineActAsTaggablePlugin 不起作用

Symfony plugin sfDoctrineActAsTaggablePlugin not working(Symfony 插件 sfDoctrineActAsTaggablePlugin 不起作用)

本文介绍了Symfony 插件 sfDoctrineActAsTaggablePlugin 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给我的学说模型的一些对象添加一些标签.

I want to attribute some tags to some of the objects of my doctrine model.

我发现 sfDoctrineActAsTaggablePlugin 这似乎正是我所需要的.

I found sfDoctrineActAsTaggablePlugin which seems to be precisely what I need.

问题是当我想保存一个 Taggable 对象时,我得到了这个错误:

The problem is that when I want to save a Taggable object, I get this error:

Unknown record property / related component "saved_tags" on "Mishidea"

Mishidea 是我希望成为可标记的类/表的名称.

Mishidea is the name of the class/table that I want to be Taggable.

这是我的 schema.yml 文件的相关部分:

Here is the related portion of my schema.yml file:

Mishidea:
  connection: doctrine
  actAs: {Timestampable: ~ , Taggable: ~ }      
  tableName: mishidea
  columns:
    idea_id:
      type: integer(4)
      primary: true
      autoincrement: true
    idea_title:
      type: string()
      notnull: true
      primary: false
    idea_summary:
      type: string()
      notnull: true
      primary: false
    idea_description:
      type: string()
      notnull: true
      primary: false
    idea_up:
      type: integer(8)
      notnull: true
      default: 0
      primary: false
    idea_down:
      type: integer(8)
      notnull: true
      default: 0
      primary: false
    idea_confirmation:
      type: boolean(1)
      default: false
      primary: false
    group_id:
      type: integer(4)
      notnull: false
      primary: false
  relations:
    Mishgroup:
      local: group_id
      foreign: group_id
      type: one
    Ideacomment:
      local: idea_id
      foreign: idea_id
      type: many
    Mishdocument:
      local: idea_id
      foreign: idea_id
      type: many
    RelIdeafollower:
      local: idea_id
      foreign: idea_id
      type: many

我还为类的actAs属性尝试了其他类型的语法,例如:

I also tried other types of syntax for the actAs attribute of the class such as:

actAs:
    Timestampable: ~
    Taggable: ~

和:

actAs:[Timestampable,Taggable]

但这不是我得到的问题:

But it's not the problem as I get :

$timestampable0 = new Doctrine_Template_Timestampable();
$taggable0 = new Taggable();
$this->actAs($timestampable0);
$this->actAs($taggable0);

在我的模型的 BaseMishidea.php 类中.

in the BaseMishidea.php class of my model.

我不明白为什么 Mishidea 类没有获得 Taggable 类应该添加的属性,因为模型似乎通过 $this->actAs() 语句添加了行为.

I don't understand why the Mishidea class doesn't get the attributes that the Taggable class should add to it since the model seems to add the behaviour via the $this->actAs() statement.

如何让这个插件工作?

如果有帮助,我将 Symfony 1.4 与 MySQL/InnoDB 一起使用,并且我之前尝试过使用 PostgreSql DBMS,结果相同.

If that helps, I use Symfony 1.4 with MySQL/InnoDB and I had tried before with the PostgreSql DBMS, with the same result.

非常感谢您的帮助.

谢谢

FuzzyTern

EDIT1 - 关于 johnwards 所说的话:任何人都可以确认可标记行为是通过actAs:"而不是templates:"添加到 schema.yml 文件中的吗?这就是我在这里和那里读到的内容(请参阅我对 johnwards 帖子的评论),但由于这两种解决方案都不适合我,我不确定.使用模板"关键字时,我得到:

EDIT1 - Regarding what said johnwards : Can anyone confirm that the Taggable behaviour is added in the schema.yml file via "actAs:", not "templates:"? That's what I read here and there (see my comment on johnwards' post) but since neither of both solutions work for me, I'm not sure. When using the "templates" keyword I get:

Unknown method Mishidea::addTag

并且没有生成模型的具体类.

and the specific classes of the model aren't generated.

EDIT2 - 经过进一步调查,我发现 Symfony 堆栈跟踪很有趣.确实,当我这样做时:

EDIT2 - After a further investigation, I found that the Symfony stack trace is interresting. Indeed, when I do:

$idea = new Mishidea();
$idea->save();

TaggableListener 被调用(下面的堆栈跟踪中的第 8 点),这表明关系正在工作.但是后来我不明白发生了什么并导致异常.

the TaggableListener is called (point 8 in the stack trace below), which shows that the relation is working. But then I don't understand what's going on and leads to the exception.

堆栈跟踪:

1. at ()
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record/Filter/Standard.php line 55 ...

    public function filterGet(Doctrine_Record $record, $name)

    {

        throw new Doctrine_Record_UnknownPropertyException(sprintf('Unknown record property / related component "%s" on "%s"', $name, get_class($record)));

    }

}

2. at Doctrine_Record_Filter_Standard->filterGet(object('Mishidea'), 'saved_tags')
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1374 ...
3. at Doctrine_Record->_get('saved_tags', 1)
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1333 ...
4. at Doctrine_Record->get('saved_tags')
in n/a line n/a ...
5. at call_user_func_array(array(object('Mishidea'), 'get'), array('saved_tags'))
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php line 212 ...
6. at sfDoctrineRecord->__call('getSavedTags', array())
in n/a line n/a ...
7. at Mishidea->getSavedTags()
in SF_ROOT_DIR/plugins/sfDoctrineActAsTaggablePlugin/lib/TaggableTemplate.class.php line 93 ...
8. at TaggableListener->postSave(object('Doctrine_Event'))
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record/Listener/Chain.php line 237 ...
9. at Doctrine_Record_Listener_Chain->postSave(object('Doctrine_Event'))
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 355 ...
10. at Doctrine_Record->invokeSaveHooks('post', 'save', object('Doctrine_Event'))
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/UnitOfWork.php line 112 ...
11. at Doctrine_Connection_UnitOfWork->saveGraph(object('Mishidea'))
in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php line 1691 ...
12. at Doctrine_Record->save()
in SF_ROOT_DIR/apps/frontend/modules/idea/actions/actions.class.php line 24 ...

如果有人想查看堆栈跟踪中任何一点的代码(此处仅扩展了 1.),请询问.欢迎提出任何建议.

If someone wants to see the code for any point of the stack trace (only 1. is expanded here), just ask. Any suggestion is welcomed.

推荐答案

哎呀哎呀!!我终于解决了这个问题!

Whoop whoop!! I solved this problem, at last!

嗯...问题是 sfDoctrineActAsTaggablePlugin 插件仅适用于具有精确称为id"的 id 属性的可标记对象.我喜欢将实体的名称放在我的属性名称中,这就是为什么我将我的可标记实体的想法称为idea_id".

Well... The problem is that the sfDoctrineActAsTaggablePlugin plugin does work only for taggable objects which have an id property called precisely "id". I like to put the name of the entity in the name of my properties, that's why I called the idea of my taggable entity "idea_id".

一旦我将idea_id"替换为id",一切都会像魅力一样发挥作用(您可能需要在 schema.yml 甚至稍后在 routing.yml 文件中进行一些更改).

As soon as I replaced "idea_id" by "id" everything worked like a charm (you may have several changes to do in the schema.yml and maybe even in the routing.yml file later on).

我不知道我是否足够清楚,所以请不要犹豫,问我进一步的解释.

I don't know if I'm clear enough so don't hesitate to ask me further explanations.

总之这个插件有点遗憾,它不够通用(将向开发人员报告).

Anyway that's a bit of a shame for this plugin, it is not generic enough (will report this to the developers).

顺便说一句,我在第一篇文章中所写的所有 actAs 语法都是等效的,请随意使用.

BTW, all the syntaxes for actAs written in my first post are equivalent, use whichever you prefer.

感谢大家的帮助,每一个回复都是一种鼓励,没有它,我就没有力气找到错误;).

Thank you all for your help, each reply was an encouragement without which I wouldn't have had the strength to find the bug ;).

干杯

这篇关于Symfony 插件 sfDoctrineActAsTaggablePlugin 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Symfony 插件 sfDoctrineActAsTaggablePlugin 不起作用

基础教程推荐