教义协会只需填写 2 个表格中的 1 个

  • 本文关键字:表格 php symfony doctrine
  • 更新时间 :
  • 英文 :


我仍然在没有提供任何手册的情况下将开发人员背后的代码带到后面。 我想知道是否可以对学说不要将数据持久化在 2 个表中的持久数据中。

2桌 :

  • 规则 => 存储规则的所有元素(名称、端口、服务、源地址、状态等(

  • 状态 => 存储规则的状态(已询问、生产中、已拒绝(

规则实体:

/**
*@ORMManyToOne(targetEntity="status", inversedBy="statuses", cascade={"persist"})
*@ORMJoinColumn("name="status_id", referencedColumnName="id", nullable=false)
*/
private $status

状态实体:

/**
*@ORMOneToMany(targetEntity="rule", mappedBy="status")
*/
private $statuses

表:

----------                      ---------
Rule                              Status
----------                       ---------- 
ID                               ID
Name                             Name
Status_Id                        Colour
SourcePort                      ----------
DestinationPort
Protocol
----------

问题是,每次我尝试保存新规则时,Doctrine 都会尝试将status_in_rule_id的 ID 保留在表状态中,例如 status_id。

我想知道是否有办法告诉教义,只在规则 tble 中保存status_id,而不试图在状态表中保留任何内容

规则控制器:

$namespace = 'FwBundle';
$em = $this->getDoctrine()->getManager($this->getUser()->getUserService()->getName());
$repositoryFw = $em->getRepository($namespace.':Fw');   
$repositoryStatus = $em->getRepository($namespace.':Status');
$liste_fws = $repositoryFw->myFindVisible();
$rule = new rule();
$form = $this->createForm(RuleType::class, $rule,array('fw'=>$id));
if ($form->isSubmitted() && $form->isValid()) {
$fw = $repositoryFw->findOneById($id);
$status = $repositoryStatus->findOneByShortName('startvalue');
$rule->setStatus($status);
$em->persist($rule);
$em->flush();

转储消息:

status {#1509 ▼
-id: 1
-name: "just posted by customer"
-shortName: "startvalue"
-colour: "blue"
-statuses: PersistentCollection {#1511 ▼
-snapshot: []
-owner: status {#1509}
-association: array:15 [ …15]
-em: EntityManager {#449 …11}
-backRefFieldName: "status"
-typeClass: ClassMetadata {#463 …}
-isDirty: false
#collection: ArrayCollection {#1512 ▶}
#initialized: false
}
}

听起来好像有一个Status实体的新实例。 您需要自己加载Status实体

$status = $entityManager->getRepository(Status::class)->find($id);

并将其设置为Rule

$rule->setStatus($status)

或者,确保您使用的是与当前正在编辑Rule关联的Status。 请记住,您使用的是对象而不是值。 所以$rule->setStatus(1);是无效的。还要检查以确保您

$entityManager->persist($rule);

最新更新