Skip to content

Instantly share code, notes, and snippets.

@sheeep
Created November 12, 2012 11:05
Show Gist options
  • Select an option

  • Save sheeep/4058710 to your computer and use it in GitHub Desktop.

Select an option

Save sheeep/4058710 to your computer and use it in GitHub Desktop.
Doctrine Many-to-Many, Repository->findBy()
{
"name": "symfony/framework-standard-edition",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-0": { "": "src/" }
},
"repositories": [ { "type": "composer", "url": "http://composer.2mu.ch/" } ],
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.1.*",
"doctrine/orm": ">=2.2.3,<2.4-dev",
"doctrine/doctrine-bundle": "1.0.*",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"twig/extensions": "1.0.*",
"symfony/assetic-bundle": "2.1.*",
"symfony/swiftmailer-bundle": "2.1.*",
"symfony/monolog-bundle": "2.1.*",
"sensio/distribution-bundle": "2.1.*",
"sensio/framework-extra-bundle": "2.1.*",
"sensio/generator-bundle": "2.1.*",
"jms/security-extra-bundle": "1.2.*",
"jms/di-extra-bundle": "1.1.*",
"fzaninotto/faker": "1.1.*",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"stof/doctrine-extensions-bundle": "dev-master",
"friendsofsymfony/user-bundle": "dev-master",
"hwi/oauth-bundle": "*"
},
"scripts": {
"post-install-cmd": [
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
],
"post-update-cmd": [
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
]
},
"config": {
"bin-dir": "bin"
},
"minimum-stability": "dev",
"extra": {
"symfony-app-dir": "app",
"symfony-web-dir": "web"
}
}
weide:community-dev sheeep [master] $ phpunit -c app/
PHPUnit 3.6.12 by Sebastian Bergmann.
Configuration read from /home/sheeep/dev/community-dev/app/phpunit.xml.dist
.........................E....................................... 65 / 67 ( 97%)
..
Time: 6 seconds, Memory: 124.25Mb
There was 1 error:
1) Oneup\BlogBundle\Tests\ORM\PostTest::testPlatformConnection
ErrorException: Notice: Undefined index: joinColumns in /home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1495
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1495
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1452
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1525
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1018
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:842
/home/sheeep/dev/community-dev/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:157
/home/sheeep/dev/community-dev/src/Oneup/BlogBundle/Tests/ORM/PostTest.php:102
--
<?php
namespace Oneup\CommunityBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Oneup\CommunityBundle\Entity\Repository\PlatformRepository")
* @ORM\Table(name="platforms")
*/
class Platform
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// [...] other field stuff
}
<?php
namespace Oneup\BlogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Oneup\BlogBundle\Entity\Repository\PostRepository")
* @ORM\Table(name="posts")
*/
class Post implements Likeable, Commentable, Taggable, PlatformAware
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Oneup\CommunityBundle\Entity\Platform", cascade={"persist"})
* @ORM\JoinTable(name="map_post_platform",
* joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="platform_id", referencedColumnName="id")}
* )
*/
protected $platforms;
// [...] other fields
/**
* Constructor
*/
public function __construct()
{
// [...]
$this->platforms = new ArrayCollection();
}
}
<?php
namespace Oneup\BlogBundle\Tests\ORM;
use Oneup\BlogBundle\Entity\Post;
use Oneup\BlogBundle\Entity\Category;
use Oneup\TestBundle\Model\ORM\ModelTest;
class PostTest extends ModelTest
{
public function testPlatformConnection()
{
// get repositories
// functions are part of TestBundle\Model\ORM\ModelTest
$postRepo = $this->getRepository();
$platformRepo = $this->getRepository('OneupCommunityBundle:Platform');
// get a platform for filtering
$platform = $platformRepo->findOneByName('platformName');
// every one of these methods will throw the same error
$posts = $postRepo->findBy(array('platforms' => array($platform)));
$posts = $postRepo->findByPlatforms($platform);
$posts = $postRepo->findByPlatforms(array($platform));
$this->assertInstanceOf('Oneup\BlogBundle\Entity\Post', $posts[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment