简体   繁体   English

使用查询构建器结果设置表单实体

[英]Setup a form entity flield with query builder results

Hello everyone I need your help in this issue, the point is: 大家好,在这个问题上我需要您的帮助,重点是:

I have two related entities "Cargo" and "CatVal", see next figure: 我有两个相关实体“ Cargo”和“ CatVal”,请参见下图:

CARGO (Key: ShipCargoId)   CATVAL (Key: CatValId)
+++++++++++++++++++++++++  ++++++++++++++++++++++++
+ ShipCargoId + CargoId +  +CatValId + CatValDesc +
+++++++++++++++++++++++++  ++++++++++++++++++++++++
+     3       +     1   +  +    1    + Cargo 1    +
+++++++++++++++++++++++++  ++++++++++++++++++++++++

Ok, in this figure note these related entities have a relation between ShipCargoId.CargoId and CatVal.CatValId and these relation es configure in my Entity file how OneToMany relation from CatVal To Cargo as you can see at next: 好的,在此图中请注意,这些相关实体在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;
}
}

Then, I need to achieve generate a form with entity field which the label be "CatValDesc" and the value be "ShipCargoId" 然后,我需要实现生成一个带有实体字段的表单,该字段的标签为“ CatValDesc”,值为“ ShipCargoId”

To achive this I configure a query builder in FormType file, as you can see next: 为此,我在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',
        ))

But I can't make it work, the error that show me now is: 但是我无法使其正常工作,现在显示给我的错误是:

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') 

If I change the addSelect for ->addSelect('c','p') to retrieve intities instead integer value, show me this error: 如果我将addSelect更改为-> addSelect('c','p')以获取整数而不是整数值,请显示此错误:

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".

So, any idea how I can make it work like a charm?, best regards 那么,有什么主意可以让我像魅力一样工作吗?

Yo don't need to select the ID but the entire Entity, the form builder takes the ID for you. 不需要选择ID,而是选择整个实体,表单构建器将为您选择ID。 Your query builder should be like this: 您的查询生成器应如下所示:

'query_builder' => function (EntityRepository $er) use($ShipId){
    return $er->createQueryBuilder('c')
        ->join('c.cargos', 'cg')
        ->where('cg.shipCargoId = :id')
        ->setParameter('id', $ShipId);
},

If I get it right, the shipCargoId is the id of the ship? 如果我理解正确,则shipCargoId是船的ID? The CatValId is the primary key of the CatVal entity. CatValId是CatVal实体的主键。 By selecting the CatVal Entities you also select the IDs, so you don't need to do that explicitly. 通过选择CatVal实体,您还可以选择ID,因此您无需明确地进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM