也许我正在完全错误地解决我的整个问题,但希望有人能理解我的奋斗并能指出正确的方向。
我想从我的实体Student中的实体Student \ Preference中获取一些值,以便可以将它们加载到Listmapper中。
在我的学生实体中,我有以下内容:
/**
* @ORM\OneToMany(targetEntity="Map\Bundle\StudentBundle\Entity\Student\Preference", mappedBy="student")
*/
protected $status;
/**
* Get Status
*
* @param \Map\Bundle\StudentBundle\Entity\Student\Preference $preference
* @return string
*/
// When I change $preference to null here I get an error that my instance must be an object of Entity\Student\Preference , but none was given
public function getStatus(\Map\Bundle\StudentBundle\Entity\Student\Preference $preference = null)
{
$string = '';
$string .= ($preference->getVoorrangChecked() ? "Declined" : "Accepted");
$string .= "by";
$string .= $preference->getControleur();
$string .= "at";
$string .= $preference->getDateUpdated();
return $string;
}
但是不知何故,它无法识别$ preference。它给了我这个错误:
Error: Call to a member function getPreferenceChecked() on null
但是,在我的实体Student \ Preference中,我当然具有所有这些功能。有人可以指出我正确的方向吗?我真的被困在这里。.我在监督什么吗,我忘了什么吗?
这是我的listMapper:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('remote')
->add('name')
->add('dobformatted', null, array('label' => 'Birthday'))
->add('status'); //Here is where the string generated by getStatus should be
}
您应该将函数声明更改为:
public function getStatus(\Map\Bundle\StudentBundle\Entity\Student\Preference $preference)
通过= null
在参数末尾添加,您可以将Preference设为可选参数。如果您不传入任何内容,则值$preference
将为null
。因此,此时您没有Preference对象,也无法调用成员函数。
由于您的函数需要存在一个Preference对象,因此您不想使其成为可选对象。另外,在构建字符串时,我注意到您没有适当的间距。您将获得类似的字符串,DeclinedbySomethingat2015-01-01
而不是Declined by Something at 2015-01-01
。您将需要添加它。您还可以使用sprintf:
$string .= ($preference->getVoorrangChecked() ? "Declined" : "Accepted");
$string .= " by ";
$string .= $preference->getControleur();
$string .= " at ";
$string .= $preference->getDateUpdated();
或者
return sprintf("%s by %s at %s",
($preference->getVoorrangChecked() ? "Declined" : "Accepted"),
$preference->getControleur(),
$preference->getDateUpdated()
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句