简体   繁体   中英

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 (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:

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"

To achive this I configure a query builder in FormType file, as you can see next:

>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:

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. 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? The CatValId is the primary key of the CatVal entity. By selecting the CatVal Entities you also select the IDs, so you don't need to do that explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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