错误:无法访问受保护的属性UserBundle \ Entity \ User :: $ id in C:\ WAMP \ WWW \ LoveMates的\ src \ ProfileBundle \控制器\ ProductController.php 第20行
这是我的控制器
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class ProductController extends Controller
{
public function productAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$product->user_id = $this->get('security.context')->getToken()->getUser()->id;
$product = new Product();
$form = $this->createFormBuilder($product)
// ...
->add('image', 'file')
->add('imagename', 'text')
->add('upload', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$response= new Response('cool image upload successfully');
return $response;
}
return $this->render('LoveMatesProfileBundle:Default:product.html.twig',
array( 'form' => $form->createView()));
}
}
这是我的实体
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @Vich\Uploadable
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// ..... other fields
/**
* @Assert\File(
* maxSize="1M",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
* )
* @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
*
* @var File $image
*/
protected $image;
/**
* @ORM\Column(type="string", length=255, name="image_name")
*
* @var string $imageName
*/
protected $imageName;
/**
*@ORM\Column(type="integer")
*/
public $user_id;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImage(File $image)
{
$this->image = $image;
}
/**
* @return File
*/
public function getImage()
{
return $this->image;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get user_id
* Get the current logged in User Object
* @return \Symfony\Component\Security\Core\User\User
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set user_id
*
* @param integer $userId
* @return Product
*/
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
}
user_id的字段不保存正确的值,并且始终为null。 你的帮助将不胜感激。
答案 0 :(得分:1)
您需要执行此操作才能获取User
:
$userId = $this->get('security.context')->getToken()->getUser()->getId();
编辑:您需要更改此行:
$product->user_id = $this->get('security.context')->getToken()->getUser()->id;
$product = new Product();
进入这个:
$product = new Product();
$product->user_id = $this->get('security.context')->getToken()->getUser()->getId();