English 中文(简体)
理论理学2 - 使用外国密钥列作为正常字段
原标题:Doctrine2 - using a foreign key column as a normal field

在学说中有种方法可以有这样的东西:

class Entity {

    /**
     * @Column(name="related_entity_id")
     */
    private $relatedEntityId;

    /**
     * @ManyToOne(targetEntity="RelatedEntitiy")
     * @JoinColumn(name="related_entity_id", referencedColumnName="id")
     */ 
    private $relatedEntity;
}

我想做的就是做这样的事:

call Entity::setRelatedEntityId($someId), and persist the entity, and have the entity return the related entity by calling Entity::getRelatedEntity().

有关实体是从一个严格限定的表格中挑选出来的,该表格在运行时决不会动态增长,因此相关实体有一定数目的代号。

在创建新实体时,我想确定相关实体的代号,但不必从数据库中取出整个相关实体。

只要我可以测试这一点,它就行不通,因为如果我设定相关的实体Id,而不是相关的实体,“理论”就自动将相关实体的栏目设置为无效,因为基本上没有建立任何关系。

我也曾尝试过做这样的事情:

删除相关的 EnitityId 属性和使用

Entity::setRelatedEntity(new RelatedEntity($relEntId))

the constructor of the RelatedEntity will set the id, but not other values. I do not want to persist the RelatedEntity (it s values are already set in the DB for the given $relEntId), but this time Doctrine signals an error at flush, because it has an unpersisted entity.

简而言之,我想做的就是在不知道相关实体的编号的情况下 建立某种关系。如果有其他方法可以做到这一点,请分享。

提前感谢

编辑:

我找到了一种变通办法。因为相关的实体将是一组有限的不可改变的物体,我做了以下工作:

  1. use the entityManager to find all RelatedEntities;
  2. inject the list to the object that will be creating new Entities
  3. when creating a new Entity, select one of the RelatedEntities from the list as its RelatedEntity

我会把问题保留一两天 以防有人想出更好的办法

最佳回答

使用实体代理代理 :

Entity::setRelatedEntity($entityManager->getReference( RelatedEntity , $relEntId))
问题回答

我不认为这应该像你描述的那样工作:)

您添加的实体必须是一个理论管理对象, 这意味着您必须首先使用 findd () columme of houses company.

根据我在这里进一步阐述的关于理论2的经验,http://smusoke.wordpress.com/2012/03/25/doctrine2-day-3-proxies-assessions-consociations- relationships/

我的做法如下:

(a) 只拥有与美元有关的实体财产

(b) 添加一个获取相关实体 Id () 函数,该函数从 $ 相关实体返回 ID 值

(c) 添加一个设定 $EntitityId () 的设置 $Entity 对象 - 您可能需要从数据库中装入它, 当 i 只拥有相关实体的代号时, 使您免于污染其它层层 。

(d) 添加获取相关()和设定相关()功能

您不能拥有外国密钥列的属性, 且已映射的属性会随着“ 理论” 的混淆而被混淆





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签