Php 正在尝试获取symfony中图像表中的用户id存储

Php 正在尝试获取symfony中图像表中的用户id存储,php,mysql,symfony,Php,Mysql,Symfony,错误:无法访问中受保护的属性UserBundle\Entity\User::$id C:\wamp\www\LoveMates\src\ProfileBundle\Controller\ProductController.php 第20行 这是我的控制器 use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkB

错误:无法访问中受保护的属性UserBundle\Entity\User::$id C:\wamp\www\LoveMates\src\ProfileBundle\Controller\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;
    }

}
用户id的字段未保存正确的值,并且始终为空。
非常感谢您的帮助。

您需要这样做才能获得
用户的id

$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();

确保在用户类中有用于
$id
的getter,并改用
getToken()->getUser()->getId()
;或者将用户类中的
$id
设置为
公共
谢谢。将id更改为getId后,得到一个新警告:从空值创建默认对象。还有一个用户正在登录,所以我猜它不能为空。将id更改为getId后,得到一个新警告:从空值创建默认对象