解析商店实体“生产者”,其中包括带有属于该实体的翻译的数组。
保存数据库成功。但是,在包含翻译的表中缺少指向“生产者”表的链接。
这个结果:
我的制作人:
+----+----+
| id |code|
+----+----+
| 1 |abcd|
+----+----+
my_producer_translations
+----+-----------+----+------+
| id |id_producer|name|locale|
+----+-----------+----+------+
| 1 | NULL |abcd| en |
+----+-----------+----+------+
| 2 | NULL |abcd| de |
+----+-----------+----+------+
实体生产者翻译
ProducerTranslation
{
protected $id;
protected $name;
protected $producer;
protected $locale;
... getters and setters
}
实体生产者
Producer
{
protected $id;
protected $code;
protected $translations;
public function __construct()
{
$this->translations = new ArrayCollection;
}
... getters and setters, method for adding translations (entity ProductTranslation)
}
制作人YML
My\ProducerBundle\Entity\Producer:
type: entity
table: my_producer
id:
id:
type: integer
generator:
strategy: AUTO
fields:
code:
type: string
length: 50
nullable: true
oneToMany:
translations:
targetEntity: ProducerTranslation
mappedBy: producer
cascade: ["persist", "remove"]
ProducerTranslation YML
My\ProducerBundle\Entity\ProducerTranslation:
type: entity
table: my_producer_translations
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 100
locale:
type: text
length: 2
manyToOne:
producer:
targetEntity: Producer
inversedBy: translations
joinColumn:
name: producer_id
referencedColumnName: id
onDelete: CASCADE
最佳答案
在 ProducerTranslation 中保存设置 Producer 之前 $ProducerTranslation->setProducer($Producer)
为什么允许 ProducerTranslation.id_producer 为 null?
关于php - Doctrine YML 映射引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380601/
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e
我正在尝试将一个资源属性的默认值设置为另一个属性的值。我正在为我正在构建的tomcat说明书定义一个资源,其中包含以下定义。我想要可以独立设置的“名称”和“服务名称”属性。当未设置服务名称时,我希望它默认为为“名称”提供的任何内容。以下不符合我的预期:attribute:name,:kind_of=>String,:required=>true,:name_attribute=>trueattribute:service_name,:kind_of=>String,:default=>:name注意第二行末尾的“:default=>:name”。当我在Recipe的新block中引用我
如thisanswer中所述,Array.new(size,object)创建一个数组,其中size引用相同的object。hash=Hash.newa=Array.new(2,hash)a[0]['cat']='feline'a#=>[{"cat"=>"feline"},{"cat"=>"feline"}]a[1]['cat']='Felix'a#=>[{"cat"=>"Felix"},{"cat"=>"Felix"}]为什么Ruby会这样做,而不是对object进行dup或clone? 最佳答案 因为那是thedocumenta
假设我有一个可枚举对象enum,现在我想获取第三个项目。我知道一种通用方法是转换成数组,然后使用索引访问,如:enum.to_a[2]但这种方式会创建一个临时数组,效率可能很低。现在我使用:enum.each_with_index{|v,i|breakvifi==2}但这非常丑陋和多余。执行此操作最有效的方法是什么? 最佳答案 你可以使用take剥离前三个元素,然后剥离last从take给你的数组中获取第三个元素:third=enum.take(3).last如果您根本不想生成任何数组,那么也许:#Ifenumisn'tanEnum
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
在此处阅读有关SO的各种解释,它们是这样描述的:map:Themapmethodtakesanenumerableobjectandablock,andrunstheblockforeachelement注入(inject):Injecttakesavalueandablock,anditrunsthatblockonceforeachelementofthelist.希望你明白为什么我觉得它们表面上看起来很相似。我什么时候会选择一个而不是另一个,它们之间有什么明显的区别吗? 最佳答案 如果您认为inject也别名为reduce,这
代码:threads=[]Thread.abort_on_exception=truebegin#throwexceptionsinthreadssowecanseethemthreadseputs"EXCEPTION:#{e.inspect}"puts"MESSAGE:#{e.message}"end崩溃:.rvm/gems/ruby-2.1.3@req/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:inload_missing_constant':自动加载常量MyClass时检测到循环依赖稍加研究后,
我正在跟踪我们的应用程序(ruby2.1)中的内存泄漏问题。我正在使用这两种技术:ObjectSpace.dump_all将所有对象转储到JSON流,然后进行离线分析。我使用的第二种技术是使用ObjectSpace.reachable_objects_from进行实时分析。在这两种方式中,我发现我泄漏的对象被一个对象RubyVM::Env引用。任何人都可以向我解释什么是RubyVM::Env。如何删除这些引用? 最佳答案 RubyVM::Env是一个包含变量引用的内部ruby类。这是我的测试:require'objspace'a
这个问题不是很有用因为themethodreferenceoperatorwasremovedfromRuby2.7.0发布前。由于历史原因,这个问题被搁置了。Ruby2.7.0-preview1引入了方法引用运算符.:作为实验性功能。(更多here和here)。有一些抽象示例可用于说明如何使用这个新运算符:method=42.:to_s=>#method.receiver=>42method.name=>:to_smethod.call=>"42"和:method=File.:read=>#method.call('/Users/foo/.zshrc')=>"exportZSH=$H