gpt4 book ai didi

php - 在 ZF2 中使用表单注释和 DoctrineORM 在 composedObject 中呈现值

转载 作者:可可西里 更新时间:2023-10-31 23:40:37 25 4
gpt4 key购买 nike

好的,我在使用 composedObject 时遇到问题,在 OneToMany 关系中,编辑表单没有呈现我的实体的值,我认为这是因为我的其他类正在返回一个集合

这是我的第一个包含组合对象的类

<?php 
namespace Nomina\Entity\Empleado;
use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;


/**
* Empleado2
*
* @ORM\Table(name="empleado2")
* @ORM\Entity
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
* @Annotation\Name("EmpleadoForm")
*/
class Empleado2
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=255, nullable=true)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Numero de empleado"})
* @Annotation\Attributes({"class":"form-control"})
*/
private $nombre;

/**
* @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
* @Annotation\ComposedObject("Nomina\Entity\Empleado\EmpleadoDetalles2",is_collection=true,options={"count":1})
*/
private $detalles;

/**
* Annotation\ComposedObject("Nomina\Entity\Empleado\EmpleadoDetalles2")
*/
private $detalle;

/**
* @Annotation\Type("Zend\Form\Element\Submit")
* @Annotation\Attributes({"value":"Procesar"})
* @Annotation\Attributes({"class":"btn btn-primary"})
*/
public $submit;

public function __construct()
{
$this->detalles = new ArrayCollection();
}

public function addDetalles(Collection $detalles)
{
foreach ($detalles as $detalle) {
$detalle->setEmpleado($this);
$this->detalles->add($detalle);
}
}

public function removeDetalles(Collection $detalles)
{
foreach ($detalles as $detalle) {
$detalle->setEmpleado(null);
$this->detalles->removeElement($detalle);
}
}

public function getDetalles()
{
return $this->detalles;
}
.....

这是我的类(class),可以有很多类(class)

<?php
namespace Nomina\Entity\Empleado;
use Nomina\Entity\Empleado\Empleado2;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* EmpleadoDetalles
*
* @ORM\Table(name="empleadoDetalles2")
* @ORM\Entity
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
* @Annotation\Name("EmpleadoDetallesForm")
*/
class EmpleadoDetalles2
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="salario", type="decimal", precision=10, scale=2, nullable=false)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Salario Diario"})
* @Annotation\Attributes({"class":"form-control"})
*/
private $salario = '0.00';

/**
* @var string
*
* @ORM\Column(name="banco", type="string", length=255, nullable=true)
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true"})
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Banco"})
* @Annotation\Attributes({"class":"form-control"})
*/
private $banco;

/**
* @ORM\Column(name="idEmpleado", type="integer", length=11, nullable=false)
* Annotation\Type ("Zend\Form\Element\Hidden")
*/
private $idEmpleado;

/**
* @ORM\ManyToOne(targetEntity="Nomina\Entity\Empleado\Empleado2", inversedBy="detalles")
* @ORM\JoinColumn(name="idEmpleado", referencedColumnName="id")
* Annotation\Type ("Zend\Form\Element\Hidden")
*/
private $empleado;




/**
* Allow null to remove association
*
* @param Empleado2 $empleado
*/
public function setEmpleado(Empleado2 $empleado = null)
{
$this->empleado = $empleado;
}

/**
* @return Empleado2
*/
public function getEmpleado()
{
return $this->empleado;
}

我有这个想法

echo $this->formRow($form->get('nombre'));
echo $this->formRow($form->get('detalles')->get('salario'));
echo $this->formRow($form->get('detalles')->get('banco'));

我正确地从数据库中获取字段,但我在 composedObject 中没有得到任何东西,在我的例子中是“detalles”,我认为这是因为 detalles 可以是相同类型的许多对象,但我'我不确定如何让它工作

最佳答案

目前只有部分答案。

条目的语法:

/**
* @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
* @Annotation\ComposedObject("Nomina\Entity\EmpleadoEmpleadoDetalles2",is_collection=true,options={"count":1})
*/

需要

/**
* @ORM\OneToMany(targetEntity="Nomina\Entity\Empleado\EmpleadoDetalles2", mappedBy="empleado")
* @Annotation\ComposedObject({"target_object":"Nomina\Entity\EmpleadoEmpleadoDetalles2",
* "is_collection":"true",
* "options":{"count":1} })
*/

“@Annotations\ComposedObject”注释可以采用字符串作为目标类名称,也可以采用数组,如我上面所示。

我无法让 "count":1 或任何 "count"起作用,但如果你将 "label":"something"放在你的选项子数组中,你可以看到它正在被读取。

现在,一旦我成功指定我的组合对象是一个集合,我就开始收到其他错误....

关于php - 在 ZF2 中使用表单注释和 DoctrineORM 在 composedObject 中呈现值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23227254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com