我在控制器操作中有以下代码:
public function someAction()
{
// ...
$promo_repo = $em->getRepository('AcmeContactlistBundle:Promotion');
$default_promo_code = $this->container->getParameter('promo_default_code');
$promo = $promo_repo->findOneByCode($default_promo_code);
if (empty($promo)) {
$promo = new Promotion();
$promo->setCode($default_promo_code);
$start_date = $this->container->getParameter('promo_default_start_date');
$expiry_date = $this->container->getParameter('promo_default_end_date');
$promo->setStartsAt(new \DateTime($start_date));
$promo->setExpiresAt(new \DateTime($expiry_date));
$em->persist($promo);
$em->flush();
}
$contact = new Contact();
$contact->setPromotion($promo); // <- Error here. See error msg below
// Some more code follows ...
}
错误消息
PHP Catchable致命错误:参数1传递给 Acme \ ContactlistBundle \ Entity \ Contact :: setPromotion()必须是 Acme \ ContactlistBundle \ Entity \ Promotion的实例,null给定, ...
我已检查上述语句生成的SQL,并检查以确保记录持久保存到促销表(如果不存在)。促销对象是正确创建并持久保存到数据库的,所以我不明白,当它将它分配给联系人变量时,它的值为null。
出了什么问题?