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();
$uploadedFile = new ProfilePicture();
$uploadedFile->setFile($formData["profile_picture"]);
$user->setProfilePicture($uploadedFile);
$uploadedFile->setUser($user);
$em->persist($uploadedFile);
$em->persist($user);
$em->flush();
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)
{
["__CLASS__"]=>
string(42) "Takeabyte\CoreBundle\Entity\ProfilePicture"
["__IS_PROXY__"]=>
bool(true)
["__PROXY_INITIALIZED__"]=>
bool(false)
["id"]=>
NULL
["user"]=>
object(stdClass)
["__CLASS__"]=>
string(32) "Takeabyte\CoreBundle\Entity\User"
["id"]=>
int(11)
}
["file"]=>
NULL
["path"]=>
NULL
["temp"]=>
NULL
}
, . Proxy , , . ?
:
class ProfilePicture extends Image
{
private $id;
private $user;
}
class Image extends MediaFile
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('file', new Assert\File(array(
'maxSize' => 6000000,
'mimeTypes' => array(
"image/jpeg",
"image/png",
"image/gif"
),
)));
}
}
class MediaFile
{
protected $file;
protected $path;
protected $temp;
public function preUpload()
{
if (null !== $this->getFile())
{
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->getFile()->guessExtension();
}
}
public function upload()
{
if (null === $this->getFile())
{
return;
}
$this->getFile()->move($this->getUploadRootDir(), $this->path);
if (isset($this->temp))
{
unlink($this->getUploadRootDir().'/'.$this->temp);
$this->temp = null;
}
$this->file = null;
}
public function removeUpload()
{
if ($file = $this->getAbsolutePath())
{
unlink($file);
}
}
}
class User extends BaseUser
{
protected $id;
protected $profilePicture;
}