大家好,我在这个问题上需要你的帮助,重点是:
我有两个相关实体“Cargo”和“CatVal”,见下图:
CARGO (Key: ShipCargoId) CATVAL (Key: CatValId)
+++++++++++++++++++++++++ ++++++++++++++++++++++++
+ ShipCargoId + CargoId + +CatValId + CatValDesc +
+++++++++++++++++++++++++ ++++++++++++++++++++++++
+ 3 + 1 + + 1 + Cargo 1 +
+++++++++++++++++++++++++ ++++++++++++++++++++++++
好的,在此图中注意这些相关实体在ShipCargoId.CargoId和CatVal.CatValId之间存在关系,这些关系在我的实体文件中配置如何从CatVal到Cargo的OneToMany关系,如下所示:
namespace OLO\CargoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cargo
*
* @ORM\Table(name="shipcargos")
* @ORM\Entity(repositoryClass="OLO\CargoBundle\Entity\CargoRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Cargo
{
/**
* @ORM\ManyToOne(targetEntity="OLO\CatalogBundle\Entity\CatVal", inversedBy="cargos")
* @ORM\JoinColumn(name="CargoId", referencedColumnName="CatValId")
*/
protected $cargoId;
/**
* @var integer
*
* @ORM\Column(name="ShipCargoId", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $shipCargoId;
/**
* Get id
*
* @return integer
*/
public function getShipCargoId()
{
return $this->shipCargoId;
}
/**
* Set cargoId
*
* @param \OLO\CatalogBundle\Entity\CatVal $cargoId
* @return Cargo
*/
public function setCargoId(\OLO\CatalogBundle\Entity\CatVal $cargoId = null)
{
$this->cargoId = $cargoId;
return $this;
}
/**
* Get cargoId
*
* @return \OLO\CatalogBundle\Entity\CatVal
*/
public function getCargoId()
{
return $this->cargoId;
}
}
namespace OLO\CatalogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* CatVal
*
* @ORM\Table(name="catval")
* @ORM\Entity(repositoryClass="OLO\CatalogBundle\Entity\CatValRepository")
*/
class CatVal
{
/**
* @ORM\OneToMany(targetEntity="OLO\CargoBundle\Entity\Cargo",
mappedBy="cargoId")
*/
protected $cargos;
/**
* @var integer
*
* @ORM\Column(name="CatValId", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $catValId;
/**
* @var string
*
* @ORM\Column(name="CatValDesc", type="string", length=50)
*/
private $catValDesc;
/**
* Constructor
*/
public function __construct()
{
$this->cargos = new ArrayCollection();
}
/**
* Get catValId
*
* @return integer
*/
public function getCatValId()
{
return $this->catValId;
}
/**
* Set catValDesc
*
* @param string $catValDesc
* @return CatVal
*/
public function setCatValDesc($catValDesc)
{
$this->catValDesc = $catValDesc;
return $this;
}
/**
* Get catValDesc
*
* @return string
*/
public function getCatValDesc()
{
return $this->catValDesc;
}
/**
* Add cargos
*
* @param \OLO\CargoBundle\Entity\Cargo $cargos
* @return CatVal
*/
public function addCargo(\OLO\CargoBundle\Entity\Cargo $cargos)
{
$this->cargos[] = $cargos;
return $this;
}
/**
* Remove cargos
*
* @param \OLO\CargoBundle\Entity\Cargo $cargos
*/
public function removeCargo(\OLO\CargoBundle\Entity\Cargo $cargos)
{
$this->cargos->removeElement($cargos);
}
/**
* Get cargos
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCargos()
{
return $this->cargos;
}
}
然后,我需要生成一个带有实体字段的表单,标签为“CatValDesc”,值为“ShipCargoId”
要实现此目的,我在FormType文件中配置查询构建器,如下所示:
>add('shipCargos', 'entity', array(
'class' => 'OLOCatalogBundle:CatVal',
'query_builder' => function (EntityRepository $er) use($ShipId){
return $er->createQueryBuilder('c')
->addSelect('c.shpCargoId','p.catValDesc')
->leftJoin('c.cargos', 'p', Join::WITH, 'c.catValId = p.cargoId')
->where('p.shipId = :id')
->setParameter('id', $ShipId);
},
'choice_label' => 'catValDesc',
))
但是我无法让它发挥作用,现在告诉我的错误是:
Warning: spl_object_hash() expects parameter 1 to be object, integer given
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 1189 -
*/
public function isScheduledForInsert($entity)
{
return isset($this->entityInsertions[spl_object_hash($entity)]);
}
/**
at ErrorHandler ->handleError ('2', 'spl_object_hash() expects parameter 1
to be object, integer given', '/home/ubuntu/workspace/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', '1189', array('entity' => '45'))
at spl_object_hash ('45')
如果我更改addSelect for - > addSelect('c','p')来检索intities而不是整数值,请显示以下错误:
Neither the property "shipCargoId" nor one of the methods "getShipCargoId()", "shipCargoId()", "isShipCargoId()", "hasShipCargoId()", "__get()" exist and have public access in class "OLO\CatalogBundle\Entity\CatVal".
那么,任何想法如何让它像魅力一样工作?最好的问候
答案 0 :(得分:0)
哟不需要选择ID而是整个实体,表单构建器会为您获取ID。您的查询构建器应如下所示:
'query_builder' => function (EntityRepository $er) use($ShipId){
return $er->createQueryBuilder('c')
->join('c.cargos', 'cg')
->where('cg.shipCargoId = :id')
->setParameter('id', $ShipId);
},
如果我做对了,shipCargoId是船的身份证? CatValId是CatVal实体的主键。通过选择CatVal实体,您还可以选择ID,因此您无需明确地执行此操作。