繁体   English   中英

在symfony2中保留之前如何设置列值?

[英]how to set column value before persist in symfony2?

假设我必须将列值公式设置为1。那么在持久之前如何做。 持久后,我应该在数据库中得到1。

 $f=1;
 $product->setFormula($f);
 $em->persist($product); 

如果我在上面的行中使用它会给出一个错误

关联字段“ Nimo \\ MrmdBundle \\ Entity \\ Product#$ basedOn”的类型为“ Nimo \\ MrmdBundle \\ Entity \\ Product”的期望值,取而代之的是“整数”

这是实体代码

/**
 * @ORM\ManyToOne(targetEntity="Product")
 * @ORM\JoinColumn(name="formula", referencedColumnName="someothercolumn",nullable=true)
 **/
private $formula = null;

您必须先更正实体定义 ,但是这是您在控制器中需要做的事情。 不会工作,直到你确保你的实体是正确定义。 (我不能,因为我不知道您的实体定义)

$f=1;
$em = $this->container->get('doctrine.orm.entity_manager');
$repo = $em->getRepository('AppBundle:Formula'); //This should be your referred entity
//You can also do findOneByName below
$formula= $repo->findOneById($id); //This should be the primary key of the referred entity NOT 1
$formula->setFormula($f);
$em->persist($formula);

在两个实体之间创建关系时,不能传递单个值或包含单个值的变量。

实体在对象上工作。 因此,请尝试传递某个实体的对象或创建具有一定作用的对象。 在传递单个值时,我也遇到相同的错误。 只需传递一个实体关系注解的对象,便会拾取其他实体的连接列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM