获取Doctrine实体内的最大id [英] Get maximum id inside Doctrine entity
本文介绍了获取Doctrine实体内的最大id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在symfony 2.7实体中获取表的最大ID。
注意:未定义的属性:AppBundle\Entity\BlogPost :: $容器
这是我的BlogPost实体,
<?php
命名空间AppBundle\Entity;
使用Doctrine\ORM\Mapping作为ORM;
使用Symfony\组件\HttpFoundation\File\UploadedFile;
/ **
* BlogPost
*
* @ ORM\Table()
* @ ORM\Entity
* /
类BlogPost {
const SERVER_PATH_TO_IMAGE_FOLDER ='/ uploads';
/ **
* @var整数
*
* @ ORM\Column(name = id,type = integer)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy = AUTO)
* /
private $ id;
/ **
* @变量字符串
*
* @ ORM\Column(name = title,type = string,length = 255 )
* /
私人$ title;
/ **
* @变量字符串
*
* @ ORM\Column(name = body,type = text)
* /
私人$ body;
/ **
* @变量字符串
*
* @ ORM\Column(name = filename,type = text)
* /
私有$文件名;
/ **
*设置文件名
*
* @param字符串$ filename
* @return BlogPost
* /
公共函数setFilename($ filename){
$ this-> filename = $ filename;
返回$ this;
}
公共函数setUploader(UploadedFile $ file){
$ em = $ this->容器-> get(’doctrine.orm.entity_manager’);
$ highest_id = $ em-> createQueryBuilder()
-> select('MAX(b.id)')
-> from('AppBundle:BlogPost','b ')
-> getQuery()
-> getSingleScalarResult();
var_dump($ highest_id);
exit(); //支票价值
$ url =上传/事件退出;
$ file_name = fsdf。。 $ file-> guessExtension();
$ file-> move($ url,$ file_name);
}
/ **
*获取文件名
*
* @返回字符串
* /
公共函数getFilename( ){
返回$ this->文件名;
}
/ **
* @var boolean
*
* @ ORM\Column(name = draft,type = boolean )
* /
私人$ draft;
/ **
*获取ID
*
* @返回整数
* /
公共函数getId(){
返回$ this-> id;
}
/ **
*设置标题
*
* @param string $ title
* @return BlogPost
* /
公共功能setTitle($ title){
$ this-> title = $ title;
返回$ this;
}
/ **
*获取标题
*
* @返回字符串
* /
public function getTitle( ){
返回$ this-> title;
}
/ **
*设置主体
*
* @param string $ body
* @return BlogPost
* /
公共功能setBody($ body){
$ this-> body = $ body;
返回$ this;
}
/ **
*获取正文
*
* @返回字符串
* /
公共函数getBody( ){
return $ this-> body;
}
/ **
*设置草稿
*
* @param boolean $ draft
* @return BlogPost
* /
公共功能setDraft($ draft){
$ this-> draft = $ draft;
返回$ this;
}
/ **
*获取草稿
*
* @返回布尔值
* /
公共函数getDraft( ){
return $ this-> draft;
}
/ **
* @ ORM\ManyToOne(targetEntity = Category,inversedBy = blogPosts)
* /
private $ category;
公共功能setCategory(Category $ category){
$ this-> category = $ category;
}
公共函数getCategory(){
return $ this-> category;
}
/ **
*处理文件上载的未映射属性
* /
public $ file;
/ **
*设置文件。
*
* @param UploadedFile $ file
* /
公共函数setFile(UploadedFile $ file = null){
$ this-> file = $ file;
}
/ **
*获取文件。
*
* @return UploadedFile
* /
公共函数getFile(){
return $ this-> file;
}
/ **
*管理文件到服务器上相关位置的复制
* /
public function upload(){
//如果不需要该字段,文件属性可以为空
if(null === $ this-> getFile()){
return;
}
//我们在这里使用原始文件名,但是您应该
//对其进行清理,至少避免任何安全问题
//采取目标目录和目标文件名作为参数
$ this-> getFile()-> move(
self :: SERVER_PATH_TO_IMAGE_FOLDER,$ this-> getFile()-> getClientOriginalName()
);
//将path属性设置为文件保存位置的文件名
$ this-> filename = $ this-> getFile()-> getClientOriginalName();
//清理文件属性,因为您将不再需要它
$ this-> setFile(null);
}
/ **
*将文件上传到服务器的生命周期回调
* /
公共功能lifecycleFileUpload(){
$ this-> upload();
}
/ **
*更新哈希值以强制触发preUpdate和postUpdate事件,以触发
* /
公共函数refreshUpdated(){
// $ this-> setUpdated(new \DateTime());
}
// ...其余的类都位于此处,包括生成的字段
// //如文件名和更新的
}
这是我要获取最大ID的部分,
公共函数setUploader(UploadedFile $ file){
$ em = $ this->容器-> get('doctrine.orm.entity_manager');
$ highest_id = $ em-> createQueryBuilder()
-> select('MAX(b.id)')
-> from('AppBundle:BlogPost','b ')
-> getQuery()
-> getSingleScalarResult();
var_dump($ highest_id);
exit(); //支票价值
$ url =上传/事件退出;
$ file_name = fsdf。。 $ file-> guessExtension();
$ file-> move($ url,$ file_name);
}
借助评论和少量研究,我发现问题出在实体管理器部分。
解决方案
如果我是你,我会做这样的事情:
解决方案
p>
控制器:
$ blogPost = new BlogPost();
$ em = $ this-> getDoctrine()-> getManager();
// ..您的代码
//我假设您要在表单发布
$ blogPost = $ form-> getData();之后执行此操作
$ id = $ em-> getRepository(’AppBundle:BlogPost’)-> getMaxId();
$ blogPost-> setUploader($ id);
// ...
存储库:
公共函数getMaxId()
{
$ qb = $ this-> createQueryBuilder('u');
$ qb-> select(‘u,MAX(id)as idMax’);
return $ qb-> getQuery()-> getSingleResult();
}
实体:
公共函数setUploader(UploadedFile $ file,$ id)
{
var_dump($ id);
$ url =上传/事件;
$ file_name =‘fsdf。’。$ id。$ file-> guessExtension();
$ file-> move($ url,$ file_name);
}
它应该可以工作
I need to get maximum ID of a table inside of symfony 2.7 entity. But instead of having the id I'm getting this issue.
Notice: Undefined property: AppBundle\Entity\BlogPost::$container
This is my BlogPost entity,
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* BlogPost
*
* @ORM\Table()
* @ORM\Entity
*/
class BlogPost {
const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads';
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="body", type="text")
*/
private $body;
/**
* @var string
*
* @ORM\Column(name="filename", type="text")
*/
private $filename;
/**
* Set filename
*
* @param string $filename
* @return BlogPost
*/
public function setFilename($filename) {
$this->filename = $filename;
return $this;
}
public function setUploader(UploadedFile $file) {
$em = $this->container->get('doctrine.orm.entity_manager');
$highest_id = $em->createQueryBuilder()
->select('MAX(b.id)')
->from('AppBundle:BlogPost', 'b')
->getQuery()
->getSingleScalarResult();
var_dump($highest_id);
exit();// exit for check value
$url = 'uploads/events';
$file_name = 'fsdf.' . $file->guessExtension();
$file->move($url, $file_name);
}
/**
* Get filename
*
* @return string
*/
public function getFilename() {
return $this->filename;
}
/**
* @var boolean
*
* @ORM\Column(name="draft", type="boolean")
*/
private $draft;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return BlogPost
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle() {
return $this->title;
}
/**
* Set body
*
* @param string $body
* @return BlogPost
*/
public function setBody($body) {
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody() {
return $this->body;
}
/**
* Set draft
*
* @param boolean $draft
* @return BlogPost
*/
public function setDraft($draft) {
$this->draft = $draft;
return $this;
}
/**
* Get draft
*
* @return boolean
*/
public function getDraft() {
return $this->draft;
}
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="blogPosts")
*/
private $category;
public function setCategory(Category $category) {
$this->category = $category;
}
public function getCategory() {
return $this->category;
}
/**
* Unmapped property to handle file uploads
*/
public $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile() {
return $this->file;
}
/**
* Manages the copying of the file to the relevant place on the server
*/
public function upload() {
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and target filename as params
$this->getFile()->move(
self::SERVER_PATH_TO_IMAGE_FOLDER, $this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->filename = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->setFile(null);
}
/**
* Lifecycle callback to upload the file to the server
*/
public function lifecycleFileUpload() {
$this->upload();
}
/**
* Updates the hash value to force the preUpdate and postUpdate events to fire
*/
public function refreshUpdated() {
// $this->setUpdated(new \DateTime());
}
// ... the rest of your class lives under here, including the generated fields
// such as filename and updated
}
This is the part I'm trying to get max id,
public function setUploader(UploadedFile $file) {
$em = $this->container->get('doctrine.orm.entity_manager');
$highest_id = $em->createQueryBuilder()
->select('MAX(b.id)')
->from('AppBundle:BlogPost', 'b')
->getQuery()
->getSingleScalarResult();
var_dump($highest_id);
exit();// exit for check value
$url = 'uploads/events';
$file_name = 'fsdf.' . $file->guessExtension();
$file->move($url, $file_name);
}
With the help of comments and few research I figured out the issue is in 'entity_manager' part. Is there a way to call doctrine queries inside Entity?
解决方案
If I were you, i would do something like that :
Controller:
$blogPost= new BlogPost () ;
$em = $this->getDoctrine()->getManager();
//..your code
// I assume you want to do that after a form post
$blogPost = $form->getData();
$id = $em->getRepository('AppBundle:BlogPost')->getMaxId();
$blogPost->setUploader($id);
//...
Repository:
public function getMaxId()
{
$qb = $this->createQueryBuilder('u');
$qb->select('u, MAX(id) as idMax');
return $qb->getQuery()->getSingleResult();
}
Entity:
public function setUploader(UploadedFile $file, $id)
{
var_dump($id);
$url = 'uploads/events';
$file_name = 'fsdf.'.$id.$file->guessExtension();
$file->move($url, $file_name);
}
It should work
这篇关于获取Doctrine实体内的最大id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文