我正在尝试更新一个 id 等于 3 的表,并增加一个名为 updatecount 的列,例如,如果该列中的值为 3,让它加一并使其成为 4。这是我的表结构
图片
id | photo | updatecount |
1 xyz | 3
如果我再次更新 id 等于 1 的照片,则结果变为
id | photo | updatecount |
1 xyz | 4
这是我的 Controller 定义
public function upDateAction(Request $request)
{
$intid = (int)$request->request->get('imageid'); //convert the request object to int
$image = new Image();
... //to do updating here
已编辑
这是我的模型>>>
/**
* @var string
*
* @ORM\Column(name="photo", type="string", length=255, nullable=true)
*/
private $photo;
/**
* @var integer
*
* @ORM\Column(name="updatecount", type="integer", nullable=true)
*/
private $updatecount;
最佳答案
希望这应该完成所需的工作
public function upVoteAction(Request $request)
{
$intid = (int)$request->request->get('imageid'); //convert the request object to int
$em = $this->getContainer()->get('doctrine')->getManager();
$image = $em->getRepository('YourBundle:Image')->find($intid);
$image->setUpdatecount($image->getUpdatecount() + 1);
$em->persist($image);
$em->flush();
}
首先使用图像 ID 从存储库 getRepository()
中获取您的图像对象。然后通过增加现有值将updatecount
设置为setUpdatecount()
关于php - 更新 id 等于 3 的表,并在 updatecount 列中的现有值上加一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47553559/