如何在控制器功能中访问实体变量?

时间:2016-06-24 08:31:39

标签: symfony controller

我在实体中有一个变量private $groupId;。还有一个控制器函数public function settingsListAction(Request $request)

我想在控制器功能中访问groupId。如何实现?

修改

控制器功能

/**
 * Get all settings content list
 * @Route("settings", name="settings_list")
 */
public function settingsListAction(Request $request)
{
    // Cannot edit at all.
    //Fetch Current Financial Year target data to display that on "Road to paradise tab"
    $entity = new J1Setting();
    //do smthing 
    $groupIdValue = $entity->getGroup();

    $settingsGroup = $this->getDoctrine()->getRepository('AppBundle:J1SettingGroup')->findAll();

    $settingsList = $this->getDoctrine()->getRepository('AppBundle:J1Setting')->findBy(array('groupId' => groupIdValue));

    return $this->render('default/settingsList.html.twig', array('settingsGroup' => $settingsGroup, 'settingsList' => $settingsList));
}

实体是:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Component\Annotation\J1Entity;
use Doctrine\Common\Collections\ArrayCollection;

/**
* J1Setting
*
* @ORM\Table(name="j1_settings", indexes={@ORM\Index(name="IDX_SName", columns={"name"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\J1SettingRepository")
* @J1Entity(auditClass="AppBundle\Component\Audit\J1AuditSetting")
*/
class J1Setting
{
/**
 * @var integer
 *
 * @ORM\Column(name="setting_id", type="integer")
 * 
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $settingid;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=100, nullable=true)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="value", type="string", length=250, nullable=true)
 */
private $value;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="string", length=255)
 */
private $description;

/**
 * @var integer
 *
 * @ORM\Column(name="group_id", type="integer")
 * @ORM\Id
 */
private $groupId;

/**
 * @var J1SettingGroup
 * 
 * @ORM\ManyToOne(targetEntity="J1SettingGroup", inversedBy="settings")
 * @ORM\JoinColumns({
 * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
 * })
*/
private $group;

/**
 * @var string
 *
 * @ORM\Column(name="setting_type", type="string", length=100)
 */
private $settingType;



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

/**
 * Set name
 *
 * @param string $name
 * @return J1Setting
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set value
 *
 * @param string $value
 * @return J1Setting
 */
public function setValue($value)
{
    $this->value = serialize($value);

    return $this;
}

/**
 * Get value
 *
 * @return string
 */
public function getValue()
{
    return unserialize($this->value);
}

/**
 * Return name replacing underscores with spaces
 *
 * @return mixed
 */
public function getFriendlyName(){
    return str_replace('_', ' ', $this->getName());
}
/**
 * Set description
 *
 * @param string $description
 * @return J1Setting
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

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

/**
 * Set groupId
 *
 * @param integer $groupId
 * @return J1Setting
 */
public function setGroupId($groupId)
{
    $this->groupId = $groupId;

    return $this;
}

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

/**
 * Set settingType
 *
 * @param string $settingType
 * @return J1Setting
 */
public function setSettingType($settingType)
{
    $this->settingType = $settingType;

    return $this;
}

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


/**
* Set group
*
* @param $group
* @return J1Setting
*/
public function setGroup($group = null)
{
$this->group = $group;

return $this;
}

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

2 个答案:

答案 0 :(得分:0)

您需要在实体中创建一个getter

public function getGroupId(){
   return $this->groupId;
}
public function setGroupId($groupId){
   $this->groupId = $groupId;
}

并在您的控制器中:

$entity = new J1Setting();
$entity->setGroupId(123);
$groupIdValue = $entity->getGroupId();
var_dump($goupIdValue) //outputs 123

答案 1 :(得分:0)

使用Doctrine时,不应将外键放入实体。 这意味着,groupId不应该是您的J1Setting实体的一部分。如果要访问关联组的ID,则必须使用$entity->getGroup()->getId()。显然,只有当实体 一个群组时才有效!