Reputation: 4142
Maybe I'm approaching my whole problem completely wrong, but hopefully someone will understand my struggle and can point me in the right direction.
I want to get some values from my entity Student\Preference in my entity Student so I can load them in a Listmapper.
In my entity Student I have this:
/**
* @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;
}
But somehow it doesn't recognize $preference. It gives me this error:
Error: Call to a member function getPreferenceChecked() on null
But in my entity Student\Preference I certainly have all those functions. Can someone point me in the right direction? I'm really stuck here.. Am I overseeing something, did I forget something?
This is my 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
}
Upvotes: 1
Views: 84
Reputation: 8276
You should change your function declaration to:
public function getStatus(\Map\Bundle\StudentBundle\Entity\Student\Preference $preference)
By adding the = null
at the end of your parameter, you are making Preference an optional argument. If you don't pass anything in, the value of $preference
will be null
. So at that point you don't have a Preference object and cannot call member functions to it.
Since your function requires the exist of a Preference object, you don't want to make it optional. Also, when you're building the string I noticed you don't have the appropriate spacing. You'll get strings like DeclinedbySomethingat2015-01-01
instead of Declined by Something at 2015-01-01
. You'll want to add that in. You could also use sprintf:
$string .= ($preference->getVoorrangChecked() ? "Declined" : "Accepted");
$string .= " by ";
$string .= $preference->getControleur();
$string .= " at ";
$string .= $preference->getDateUpdated();
or
return sprintf("%s by %s at %s",
($preference->getVoorrangChecked() ? "Declined" : "Accepted"),
$preference->getControleur(),
$preference->getDateUpdated()
);
Upvotes: 1
Reputation: 4142
I fixed it a bit different,
In my listMapper I added:
$listMapper
->add('remote')
->add('name')
->add('dobformatted', null, array('label' => 'Birthday'))
->add('preference', 'string', array('template' => 'StudentBundle:CRUD:list_field_status.html.twig'));
In my entity:
/**
* @ORM\OneToOne(targetEntity="Map\Bundle\StudentBundle\Entity\Student\Preference", mappedBy="student")
* @ORM\OrderBy({"preference_id" = "ASC"})
*/
protected $preference;
/**
* @param mixed $preference
*/
public function setPreference($preference)
{
$this->preference = $preference;
}
/**
* @return mixed
*/
public function getPreference()
{
return $this->preference;
}
And in a new twig for this field:
{% if object.preference.getControleur is not null %}
<td>{{ "%s by %s at %s"| format((object.preference.getPreferenceChecked ? "Declined" : "Accepted") , object.preference.getControleur, object.preference.getDateUpdated|date('Y-m-d')) }}</td>
{% else %}
<td>Not checked yet.</td>
{% endif %}
I hope this will help someone out one day.
Upvotes: 1