Doctrine Proxy does not initialize

When I upload a file to Symfony, it loads as it should. I used the symfony file download tutorial and modified it to suit my needs.

if($form->isValid())
{
    $em = $this->oStarter->getEntityManager();

    // Save file to database
    $uploadedFile = new ProfilePicture();
    $uploadedFile->setFile($formData["profile_picture"]);
    $user->setProfilePicture($uploadedFile);
    $uploadedFile->setUser($user);

    $em->persist($uploadedFile);
    $em->persist($user);

    $em->flush();

    // Other things like Twig templates etc..

This code is used to upload an image and set it as a user profile image. The user is located $this->getUser()in the controller. When I pull out Entity after flushing, it shows me a dump of the actual Entity, as expected.

, . MySQL, ProfilePicture . ProfilePicture, . :

$avatar = $user->getProfilePicture();
$path = $avatar->getWebPath();
Debug::dump($avatar);


object(stdClass)#938 (8) 
{
  ["__CLASS__"]=>
  string(42) "Takeabyte\CoreBundle\Entity\ProfilePicture"
  ["__IS_PROXY__"]=>
  bool(true)
  ["__PROXY_INITIALIZED__"]=>
  bool(false)
  ["id"]=>
  NULL
  ["user"]=>
  object(stdClass)#1011 (52) {
    ["__CLASS__"]=>
    string(32) "Takeabyte\CoreBundle\Entity\User"
    ["id"]=>
    int(11)
    // lots of user info
    }
  ["file"]=>
  NULL
  ["path"]=>
  NULL
  ["temp"]=>
  NULL
}

, . Proxy , , . ?

:

/**
 * @author Tim Cocu
 * @author Rick Slinkman
 *
 * @ORM\Entity
 * @ORM\Table(name="profilepictures")
 * @Database(target="client")
 */
class ProfilePicture extends Image
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="\Takeabyte\CoreBundle\Entity\User", mappedBy="profilePicture")
     */
    private $user;

    // accessors and mutators
}

/**
 * Description of Image
 *
 * @ORM\MappedSuperclass
 * @Database(target="client")
 * @author Rick Slinkman (r.slinkman@take-a-byte.eu)
 */
class Image extends MediaFile
{
    /**
     * @param ClassMetadata $metadata
     */
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('file', new Assert\File(array(
            'maxSize' => 6000000,
            'mimeTypes' => array(
                "image/jpeg",
                "image/png",
                "image/gif"
            ),
        )));
    }

    // other functions
}

/**
 * Standard container of an uploaded media file
 * @author Rick Slinkman
 * @author Tim Cocu
 * 
 * @ORM\HasLifecycleCallbacks
 * @ORM\MappedSuperclass
 * @Database(target="client")
 * 
 * Based on:
 * http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
 */
class MediaFile 
{
    /**
     * @Assert\File(maxSize="6000000")
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;

    /**
     * Temporary storage on file moving.
     */
    protected $temp;

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) 
        {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) 
        {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);

        // check if we have an old image
        if (isset($this->temp)) 
        {
            // delete the old image
            unlink($this->getUploadRootDir().'/'.$this->temp);
            // clear the temp image path
            $this->temp = null;
        }
        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) 
        {
            unlink($file);
        }
    }

    // other functions 
}

/**
 * @author: Jordy - j.deruijter@take-a-byte.eu
 * @author: Rick - r.slinkman@take-a-byte.eu
 * @author: Tim - t.cocu@take-a-byte.eu
 * @since: 25-10-13
 *
 * @ORM\Entity
 * @ORM\Table(name="fos_user_user")
 * @Database(target="client")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    // Lots of data

    /**
     * @var ProfilePicture
     * @ORM\OneToOne(targetEntity="\Takeabyte\CoreBundle\Entity\ProfilePicture", inversedBy="user")
     */
    protected $profilePicture;

    // Even more data
}
+4
1

, .

, () User. , User. ProfilePicture , . ...

, , $profilePicture NULL.

?

IDEA # 1:

  • EAGER User .
  • always_authenticate_before_granting: true confir.yml (security)

, User ...

IDEA # 2:

ProfilePicture . , ?

+4

All Articles