Question

After annotating an OneToOne Unidirectional, i want to output the joined column, without using a form.

One People Entity has got a column to store the id of Country Entity.

What i can do: I can store the id of the country into the People Entity using a form with a dropdown select, which is bound to the Country Entity.

Problem: I can not enter the value country of the, hopefully correct, joined table.

People Entity:

<?php 
namespace People\Entity;

use Doctrine\ORM\Mapping as ORM;
// ...

/**
* A people entity.
*
* @ORM\Entity
* @ORM\Table(name="icd_people")
* @property int $id
// ...
* @property string $ic_hq_country
*/
class People implements InputFilterAwareInterface
{
protected $inputFilter;

/**
* @ORM\Id
* @ORM\Column(type="integer");
*/
protected $id;

/**
* @ORM\Column(type="integer")
* @ORM\OneToOne(targetEntity="Country")
* @ORM\JoinColumn(name="ic_hq_country", referencedColumnName="id")
*/
protected $ic_hq_country;

// getter and setter
}

The Country Entity:

<?php 
namespace People\Entity;

use Doctrine\ORM\Mapping as ORM;
//...

/**
* A Country entity.
*
* @ORM\Entity
* @ORM\Table(name="pre_country")
* @property int $id
* @property string $country
*/
class Country implements InputFilterAwareInterface
{
protected $inputFilter;

 /**
 * @ORM\Id
 * @ORM\Column(type="integer");
 */
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $country;

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set country
 *
 * @param string $country
 * @return Country
 */
public function setCountry($country)
{
    $this->country = $country;

    return $this;
}

/**
 * Get country
 *
 * @return string
 */
public function getCountry()
{
    return $this->country;
}

/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy()
{
    return get_object_vars($this);
}

public function setInputFilter(InputFilterInterface $inputFilter)
{
    throw new \Exception("Not used");
}

public function getInputFilter()
{
    throw new \Exception("Not used");
}
}

The Controller Action:

public function indexAction()
{

    $userid =  $this->zfcUserAuthentication()->getIdentity()->getId();

     return new ViewModel(array(
        'pea' => $this->getEntityManager()->find('People\Entity\People', $userid),
));
}

The View giving the id of the country, but not the name:

<?php echo $this->escapeHtml($pea->ic_hq_country);?>

I actually expected something like this being possible, to output the country name and not the id:

<?php echo $this->escapeHtml($pea->country);?>

Thank you for reading, and for any help, which could lead me into the right direction!

Was it helpful?

Solution

You should not use the @Column anotation in the $ic_hq_country field of the Peopleentity.

/**
* @ORM\OneToOne(targetEntity="Country")
* @ORM\JoinColumn(name="ic_hq_country", referencedColumnName="id")
*/
protected $ic_hq_country;

like this, hopefully ic_hq_country will be a proxy to the entity instead of the id.

so in your view you can use:

<?php echo $pea->ic_hq_country->getId();?>

and also

<?php echo $this->escapeHtml($pea->ic_hq_country->getCountry());?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top