围绕以下情况我有些麻烦.
我正在尝试创建一个树结构,在那里我将能够为节点之间的连接提供自定义名称.
所以我想拥有Node和Relation模型.每
Node
has_many :relations
Run Code Online (Sandbox Code Playgroud)
每
Relation
has_many :nodes
Run Code Online (Sandbox Code Playgroud)
节点可以是父母或孩子..到目前为止一切都方便,还有吨的例子展示了如何做一个自我指涉的has_many表...问题是,我希望能够给予名称的关系,以便我可以做类似的事情:
relation1 = node1.relations.create(:name => "relation_name", :child => node2)
Run Code Online (Sandbox Code Playgroud)
并在结果得到类似的东西:
relation1.name == "relation_name"
relation1.parent == node1
relation1.child == node2
Run Code Online (Sandbox Code Playgroud)
所有的创作都发生在模型中,如果重要的话,这个活动并没有真正暴露给用户.谢谢!
EDIT2:现在它是如何工作的:
class Node < ActiveRecord::Base
belongs_to :sentence
has_one :parent_relation, :foreign_key => "child_id", :class_name => "Relation"
has_many :child_relations, :foreign_key => "parent_id", :class_name => "Relation"
has_one :parent, :through => :parent_relation
has_many :children, :through => :child_relations, :source => :child
has_many :relations, :foreign_key => "child_id"
has_many :relations, :foreign_key => "parent_id"
class Relation < ActiveRecord::Base …Run Code Online (Sandbox Code Playgroud) model relational-database has-many self-reference ruby-on-rails-3
好的,这就是我想做的.
Class Container<T>
{
T contained;
public void ContainObject(T obj)
{
contained = obj;
if(/*Magical Code That Detects If T Implemtns IContainableObject*/)
{
IContainableObect c = (IContainableObject)obj;
c.NotifyContained(self);
}
}
}
interface IContainableObject
{
public void NotifyContained(Container<REPLACE_THIS>);//This line is important, see below after reading code.
}
Class ImplementingType : IContaiableObject
{
public Container<ImplementingType> MyContainer;
public void NotifyContained(Container<ImplmentingType> c)
{
MyContainer = c;
}
}
Class Main
{
public static void Main(args)
{
ImplementingType iObj = new ImplementingType();
Container<ImplementingType> container = …Run Code Online (Sandbox Code Playgroud) 与此问题一样,当在其自己的赋值中使用未定义的局部变量时,将对其进行求值nil.
x = x # => nil
Run Code Online (Sandbox Code Playgroud)
但是当局部变量的名称与现有方法名称冲突时,它更棘手.为什么下面的最后一个例子会返回nil?
{}.instance_eval{a = keys} # => []
{}.instance_eval{keys = self.keys} # => []
{}.instance_eval{keys = keys} # => nil
Run Code Online (Sandbox Code Playgroud) 我试过按照Doctrines手册上的说明进行操作:http: //docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-self-referencing
我正在尝试设置一个页面实体,它有许多子页面,我也希望能够访问页面parentPage.
按照上面的说明,symfony2告诉我,由于语义错误,我需要设置"使用".
任何人都可以告诉我该做什么让我这样做,因为我很困难.
示例代码是:
namespace Pages\Bundle\PageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Page
*
* @ORM\Table(name="Page")
* @ORM\Entity(repositoryClass="Pages\Bundle\PageBundle\Entity\PageRepository")
*/
class Page
{
/**
* Constructor
*/
public function __construct()
{
$this->subPages = new \Doctrine\Common\Collections\ArrayCollection();
$this->parentPages = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @ManyToMany(targetEntity="Page", mappedBy="subPages")
**/
private $parentPages;
/**
* @ManyToMany(targetEntity="Page", inversedBy="parentPages")
* @JoinTable(name="sub_pages",
* joinColumns={@JoinColumn(name="parent_page_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="sub_page_id", referencedColumnName="id")}
* )
**/
private $subPages;
Run Code Online (Sandbox Code Playgroud)
...(其他变量如下,但是内容/元数据)
运行时的错误响应是:
[Semantical Error] The annotation "@ManyToMany" in property
Pages\Bundle\PageBundle\Entity\Page::$parentPages was …Run Code Online (Sandbox Code Playgroud) 我试图在实体框架中首先使用代码进行设置并遇到困难.描述我想要完成的事情:
拥有产品实体.该产品可任选地具有一个或多个相关的"儿童"产品.产品可以是一个或多个母产品的孩子.
当我去生成一个绑定到模型类"产品"的控制器时,我收到一个错误:(更新,更具体,匹配下面的代码)
Run Code Online (Sandbox Code Playgroud)There was an error running the selected code generator: 'Unable to retrieve metadata for 'ProductCatalog.Models.Product'. Multiple object sets per type are not supported. The object sets 'Product' and 'Products' can both contain instances of type 'ProductCatalog.Models.Product'.
这是不工作的模型类:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ProductCatalog.Models
{
// Product
public class Product
{
[Key]
public int ProductId { get; set; } // ProductID (Primary key)
public string ProductName { get; set; } // ProductName
public string …Run Code Online (Sandbox Code Playgroud) 我正在尝试设计一个可以在 Python 中轻松扩展的 C 接口(使用ctypes)。我在 C 中使用了自然习语:
struct format {
int (*can_open)(const char *filename);
struct format * (*open)(const char *filename);
void (*delete)(struct format *self);
int (*read)(struct format *self, char *buf, size_t len);
};
Run Code Online (Sandbox Code Playgroud)
如果我想直接从 C 扩展这个接口,它工作得很好:
struct derived /* concrete implementation */
{
struct format base;
};
Run Code Online (Sandbox Code Playgroud)
但我真正想做的是使用 ctypes 从 Python 实现这个接口。这是我到目前为止所拥有的:
CANOPENFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
#OPENFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p)
#OPENFUNC = ctypes.CFUNCTYPE(ctypes.POINTER( python_format ), ctypes.c_char_p)
#DELETEFUNC = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
#READFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p)
def py_canopen_func( …Run Code Online (Sandbox Code Playgroud) 我有一些共同的关系和属性.所以,我想使用继承映射来简化我的模式.
我创建了一个BaseData mappedsuperclass,并让我的其他实体扩展它.此BaseData类具有每个实体所需的公共关系.
它适用于多对一关系,例如
/**
* @ORM\MappedSuperclass
*/
class BaseData
{
/**
* @ORM\ManyToOne(targetEntity="Service")
* @ORM\JoinColumn(name="service_id", referencedColumnName="id")
*/
protected $service;
Run Code Online (Sandbox Code Playgroud)
但是自我引用变得有点棘手.
例如,因为我想创建一个父引用,我尝试了:
/**
* @ORM\MappedSuperclass
*/
class BaseData
{
/**
* @ORM\ManyToOne(targetEntity="BaseData")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*/
protected $parent;
Run Code Online (Sandbox Code Playgroud)
显然,当我尝试查询此实体时,它会导致TableNotFoundException : QLSTATE[42S02]: Base table or view not found: 1146 Table 'project.base_data' doesn't exist.
所以,我尝试了AssociationOverrides,但似乎不允许更改目标实体.
那么,有没有办法在MappedSuperclass上构建一些自引用?顺便说一句,它甚至有意义吗?
提前谢谢了 !
这是anwser:
我按计划在我的BaseData mappedSuperClass中定义了protected $parent和 protected $children.我用他需要的其他信息注释了它们.例如:
/**
* @ORM\MappedSuperclass
*/
class BaseData
{
/**
* @Datagrid\Column(field="parent.id", title="datagrid.parent_id", visible=false, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用下面提到的代码将 ProtoBuf 转换为 Map。
Map map = objectMapper.convertValue(obj, Map.class);
导致此问题的示例 PB 数据:id:“1”元数据 { type:“UNIT” isValidated:false status { status:ACTIVE } }
我收到错误 java.lang.IllegalArgumentException:直接自引用导致循环(通过引用链:com.sample.samplePB["unknownFields"]->com.google.protobuf.UnknownFieldSet["defaultInstanceForType"])
谁能告诉我如何快速创建一个以节点为结构、以链表为类的链表。
我试图创建一个引用自身的节点 Node 并遇到错误“值类型‘节点’不能有一个引用自身的存储属性”
为了解决这个问题,我又创建了一个 Next 类,现在我无法继续。
self-reference ×10
doctrine ×2
orm ×2
symfony ×2
c ×1
c# ×1
containers ×1
creation ×1
ctypes ×1
definition ×1
generics ×1
has-many ×1
inheritance ×1
interface ×1
ios ×1
linked-list ×1
many-to-many ×1
model ×1
objectmapper ×1
payload ×1
python ×1
quine ×1
ruby ×1
structure ×1
swift ×1