Skip to content

Instantly share code, notes, and snippets.

@nWidart
Last active August 29, 2015 14:12
Show Gist options
  • Save nWidart/1eb6b6ee63499e0569d2 to your computer and use it in GitHub Desktop.
Save nWidart/1eb6b6ee63499e0569d2 to your computer and use it in GitHub Desktop.
Doctrine relations

When going to /post-create the post and category are getting created correctly with the associated category id.

After the creation, going to /post returns me the following error:

Doctrine\Common\Proxy\AbstractProxyFactory::getProxyDefinition(): Failed opening required '/var/tmp/__CG__ModulesBlogEntitiesCategory.php' (include_path='/Users/nicolaswidart/Sites/Testing/DoctrineInLaravel/vendor/phpseclib/phpseclib/phpseclib:.:/Applications/MAMP/bin/php/php5.6.2/lib/php') 

Any suggestions ? :)

Thanks!

<?php namespace Modules\Blog\Entities;
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="categories")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="Modules\Blog\Entities\Post", mappedBy="category")
**/
private $posts;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return Post
*/
public function getPosts()
{
return $this->posts;
}
}
<?php namespace Modules\Blog\Entities;
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="posts")
*/
class Post
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="Modules\Blog\Entities\Category", inversedBy="posts")
*/
private $category;
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return Category
*/
public function getCategory()
{
return $this->category;
}
/**
* @param Category $category
*/
public function setCategory(Category $category)
{
$this->category = $category;
}
}
<?php namespace Modules\Blog\Http\Controllers;
use Doctrine\ORM\EntityManagerInterface;
use Illuminate\Routing\Controller;
use Modules\Blog\Entities\Category;
use Modules\Blog\Entities\Post;
class PostController extends Controller
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function index()
{
$post = $this->entityManager->find('Modules\Blog\Entities\Post', 2);
dd($post->getTitle());
}
public function create()
{
$category = new Category();
$category->setName('Laravel');
$this->entityManager->persist($category);
$post = new Post();
$post->setTitle('My First Post');
$post->setCategory($category);
$this->entityManager->persist($post);
$this->entityManager->flush();
dd('Post created');
}
}
Route::get('post', ['as' => 'blog', 'uses' => 'Modules\Blog\Http\Controllers\PostController@index']);
Route::get('post-create', ['uses' => 'Modules\Blog\Http\Controllers\PostController@create']);
@nWidart
Copy link
Author

nWidart commented Dec 31, 2014

After some research, apparently proxies need to be generated. Found that strange since, Cribb doesn't have the config set for proxies, while still having relations and all tests are green.

I had to add a proxy path the in doctrine package configuration. Which troubles me since, that's a fixed path for all 'Modules'
For this test my directory structure looks like the following: https://cloudup.com/cEdvAMc3aP0

Don't know if I missed something that can be done to have the proxies in the correct folder (ideally the Modules/Blog/Proxies folder I presume)

@philipbrown
Copy link

What's stopping you from using Modules/Blog/Proxies?

@nWidart
Copy link
Author

nWidart commented Dec 31, 2014

That would work yes, by setting proxy.directory in the laravel-doctrine config to base_path('Modules/Blog/Proxies).
But what if I have multiple modules ? The proxy directory setting is global for the application by the looks of it.

@philipbrown
Copy link

Ahh right, yeah I see what you mean. Yeah it looks like Doctrine wants you to have all the proxies in the same location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment