Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine Proxy does not initialize
    text
    copied!<p>When I upload a file to Symfony, it is uploads like it should. I have used the Symfony tutorial on file upload and modified it to fit my needs.</p> <pre><code>if($form-&gt;isValid()) { $em = $this-&gt;oStarter-&gt;getEntityManager(); // Save file to database $uploadedFile = new ProfilePicture(); $uploadedFile-&gt;setFile($formData["profile_picture"]); $user-&gt;setProfilePicture($uploadedFile); $uploadedFile-&gt;setUser($user); $em-&gt;persist($uploadedFile); $em-&gt;persist($user); $em-&gt;flush(); // Other things like Twig templates etc.. </code></pre> <p>This code is used to upload an image and set it as the user's profile picture. The user is found through <code>$this-&gt;getUser()</code> in the Controller. When I output the Entity after flushing, it shows me the dump of a valid Entity, like I would expect.</p> <p>When I visit the profile page for this user, the image was not found. When I check the MySQL table, I find a valid entry for the ProfilePicture with the correct ID and path. The user also has a reference to the ProfilePicture's ID, as you would expect. Instead, the page shows me the following dump:</p> <pre><code>$avatar = $user-&gt;getProfilePicture(); $path = $avatar-&gt;getWebPath(); Debug::dump($avatar); object(stdClass)#938 (8) { ["__CLASS__"]=&gt; string(42) "Takeabyte\CoreBundle\Entity\ProfilePicture" ["__IS_PROXY__"]=&gt; bool(true) ["__PROXY_INITIALIZED__"]=&gt; bool(false) ["id"]=&gt; NULL ["user"]=&gt; object(stdClass)#1011 (52) { ["__CLASS__"]=&gt; string(32) "Takeabyte\CoreBundle\Entity\User" ["id"]=&gt; int(11) // lots of user info } ["file"]=&gt; NULL ["path"]=&gt; NULL ["temp"]=&gt; NULL } </code></pre> <p>The dump shows that there is no path set. Even after calling a function of the Proxy, the real data seems not to be loaded. What am I doing wrong?</p> <p><strong>EDIT</strong> The entities are as follows:</p> <pre><code>/** * @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-&gt;addPropertyConstraint('file', new Assert\File(array( 'maxSize' =&gt; 6000000, 'mimeTypes' =&gt; 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-&gt;getFile()) { // do whatever you want to generate a unique name $filename = sha1(uniqid(mt_rand(), true)); $this-&gt;path = $filename.'.'.$this-&gt;getFile()-&gt;guessExtension(); } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this-&gt;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-&gt;getFile()-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;path); // check if we have an old image if (isset($this-&gt;temp)) { // delete the old image unlink($this-&gt;getUploadRootDir().'/'.$this-&gt;temp); // clear the temp image path $this-&gt;temp = null; } $this-&gt;file = null; } /** * @ORM\PostRemove() */ public function removeUpload() { if ($file = $this-&gt;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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload