I'm having some issues when trying to join two tables with Doctrine 2. When I attempt to add a new item to the table I get the error:
A new entity was found through a relationship that was not configured to cascade persist operations: #. Explicitly persist the new entity or configure cascading persist operations on the relationship.
When I try to update a row in the table I'm getting the error:
The given entity has no identity.
I'm trying to join the winner entities event id to the id of an event so i can gather data on the event for the view. I'm defiantly getting an id returned for from the form for the update but it doesn't seem to let me update it.
Update:
So having played around with things a little, when I add the cascade="{persist}" option I get a class not found error, removing it and adding the full namespace results in it claiming the entity does not exist...
<?php
namespace ZC\Entity;
/**
* ZC\Entity\Winner
*
* #Table(name="winner")
* #Entity(repositoryClass="ZC\Entity\Repository\Winner")
*/
class Winner
{
/**
* #var integer $id
*
* #Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
* #Id
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string $copy
*
* #Column(name="copy", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
*/
protected $copy;
/**
* #var string $url
*
* #Column(name="url", type="string", length="255", unique=false, nullable=true, precision=0, scale=0)
*/
protected $url;
/**
*
* #ManyToOne(targetEntity="Events")
* #JoinColumns=({
* #JoinColumn(name="event", referencedColumnName="id")
* })
*
*/
protected $event;
/**
* __get function.
*
* #access protected
* #param mixed $property
* #return void
*/
public function __get($property) {
return $this->$property;
}
/**
* __set function.
*
* #access protected
* #param mixed $property
* #param mixed $value
* #return void
*/
public function __set($property, $value) {
$this->$property = $value;
}
}
<?php
namespace ZC\Entity;
/**
* ZC\Entity\Events
*
* #Table(name="events")
* #Entity(repositoryClass="ZC\Entity\Repository\Events")
*/
class Events
{
/**
* #var integer $id
*
* #Column(name="id", type="integer", nullable=false, unique=false, precision=0, scale=0)
* #Id
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string $title
*
* #Column(name="title", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
*/
protected $title;
/**
* #var string $description
*
* #Column(name="description", type="text", length="", unique=false, nullable=true, precision=0, scale=0)
*/
protected $description;
/**
* #var string $status
*
* #Column(name="status", type="smallint", length="1", unique=false, nullable=false, precision=0, scale=0)
*/
protected $status = 0;
/**
* #var string $date
*
* #Column(name="date", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
*/
protected $date;
/**
* #var string $lang
*
* #Column(name="lang", type="string", length="255", unique=false, nullable=false, precision=0, scale=0)
*/
protected $lang;
/**
* __get function.
*
* #access protected
* #param mixed $property
* #return void
*/
public function __get($property) {
return $this->$property;
}
/**
* __set function.
*
* #access protected
* #param mixed $property
* #param mixed $value
* #return void
*/
public function __set($property, $value) {
$this->$property = $value;
}
}
Use cascade={"persist"}:
/**
*
* #ManyToOne(targetEntity="Acme\Bundle\AcmeBundle\Entity\Events", cascade={"persist"})
* #JoinColumns=({
* #JoinColumn(name="event", referencedColumnName="id")
* })
*
*/
protected $event;
Related
Good afternoon,
I try to get all data with one DQL query in a specific Repository.
The problem is, even if I have Host & Page[] (collection), the query returns null values for this entities.
This is my entities ([EDIT] after question by delboy1978uk):
/**
* #ORM\Entity(repositoryClass="App\Repository\WebsiteRepository")
*/
class Website
{
/**
* #var int|null $id
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $id;
/**
* #var string $domainName
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $domainName;
/**
* #var string $language
*
* #ORM\Column(type="string", length=2, nullable=false)
*/
private $language;
/**
* #var Host $host
*
* #ORM\ManyToOne(targetEntity="App\Entity\Host", cascade={"persist"})
* #ORM\JoinColumn(name="host_id", referencedColumnName="id", nullable=false)
*/
private $host;
/**
* #var ArrayCollection $pages
*
* #ORM\OneToMany(targetEntity="App\Entity\Page", mappedBy="website", cascade={"persist"})
*/
private $pages;
/**
* Website constructor.
*/
public function __construct()
{
$this->pages = new ArrayCollection();
}
}
/**
* #ORM\Entity(repositoryClass="App\Repository\HostRepository")
*/
class Host
{
/**
* #var int|null $id
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $legalName;
/**
* #var string|null
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $address;
/**
* #var string|null
*
* #ORM\Column(type="string", length=15, nullable=false)
*/
private $phoneNumber;
}
/**
* #ORM\Entity(repositoryClass="App\Repository\PageRepository")
*/
class Page
{
/**
* #var int|null $id
*
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #var string|null $title
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $title;
/**
* #var string|null $route
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $route;
/**
* #var string|null $template
*
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $template;
/**
* #var Website $website
*
* #ORM\ManyToOne(targetEntity="App\Entity\Website", inversedBy="pages")
*/
private $website;
}
This is my method to find configuration ([EDIT] after question by delboy1978uk):
class WebsiteRepository extends ServiceEntityRepository
{
/**
* WebsiteRepository constructor.
*
* #param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Website::class);
}
public function findConfiguration(): array
{
return $this->getEntityManager()->createQuery(
'SELECT w
FROM App\Entity\Website w
JOIN w.host h
LEFT JOIN w.pages p'
)->getResult();
}
}
I expect to returns Host & Page[] (collection) from findConfiguration method in WebsiteRepository.
Thanks you for your help.
This is the solution with findConfiguration method in WebsiteRepository for findConfiguration method:
SELECT w, h, p
FROM App\Entity\Website w
LEFT JOIN w.host h
LEFT JOIN w.pages p
In our symfony2 project we want to implement elasticsearch with ongr-io bundle, but our whole system is built on doctrine. Is it possible to somehow use ongr-io elasticsearch bundle without totally redoing whole database with documents instead of entities?
Entity that I want to index (fields for search would be: name, ChildCategory and ParentCategory):
/**
* Course
*
* #ORM\Table(name="course")
* #ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository")
* #ORM\HasLifecycleCallbacks
*/
class Course
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, unique=false)
*/
private $name;
/**
* #var ParentCategory
*
* #ORM\ManyToOne(targetEntity="ParentCategory")
* #ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id")
*/
private $parentCategory;
/**
* #var ChildCategory
*
* #ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses")
* #ORM\JoinColumn(name="childCategory_id", referencedColumnName="id")
*/
private $childCategory;
/**
* #var City
*
* #ORM\ManyToOne(targetEntity="City")
* #ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course")
* #ORM\JoinColumn(name="owner_id", referencedColumnName="id")
*/
private $owner;
/**
* #var text
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var Picture
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"})
*/
private $pictures;
/**
* #var Ratings
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid")
*/
private $ratings;
public $avgRating;
}
it's very interesting.
The first thing is Entity location. The document for ElasticsearchBundle has to be in Document directory. It's not a big deal. All you need to do is to create an empty class and extend entity with #Document annotation. Next are the fields. With name field there is no problem at all, just add #property annotation and you are good. More interesting is with relations. My suggestion would be to create separate property and in the getter return value from the relation for that field. I hope you got the idea.
Update:
Here's an example of documents:
AppBundle/Entity/Post
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use ONGR\ElasticsearchBundle\Annotation as ES;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ES\Property(type="string")
* #ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #var string
*
* #ES\Property(name="user", type="string")
*/
private $esUser;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return Post
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* #return string
*/
public function getEsUser()
{
return $this->esUser;
}
/**
* #param string $esUser
*/
public function setEsUser($esUser)
{
$this->esUser = $esUser;
}
}
AppBundle/Document/Post
<?php
namespace AppBundle\Document;
use ONGR\ElasticsearchBundle\Annotation as ES;
use AppBundle\Entity\Post as OldPost;
/**
* #ES\Document()
*/
class Post extends OldPost
{
}
I am using Doctrine 2 with ZF2.
I have a sites entity with the following primary key field.
/**
* #var string
* #ORM\Column(name="site_id", type="string", length=10, nullable=false)
* #ORM\Id
*/
private $siteId;
And the following index's
* #ORM\Table(name="sites", indexes={
* #ORM\Index(name="PRIMARY", columns={"site_id"}),
* #ORM\Index(name="country_id", columns={"country_id"}),
* #ORM\Index(name="timezone_id", columns={"timezone_id"}),
* #ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* #ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* #ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
When I run php ./vendor/doctrine/doctrine-module/bin/doctrine-module orm:validate-schema from the command line I get the following error message.
[Doctrine\DBAL\Schema\SchemaException]
An index with name 'primary' was already defined on table 'sites'.
However it also reports The mapping files are correct.
Does anyone know why this error is being generated?
Many thanks in advance.
EDIT
Full entity as requested
/**
* Sites Entity
*
* #author Garry Childs
*
* #ORM\Table(name="sites", indexes={
* #ORM\Index(name="country_id", columns={"country_id"}),
* #ORM\Index(name="timezone_id", columns={"timezone_id"}),
* #ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* #ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* #ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
* #ORM\Entity(repositoryClass="Application\Entity\Repository\SitesRepository")
* #ORM\HasLifecycleCallbacks
*/
class Sites extends AbstractEntity
{
/**
* #var string
* #ORM\Column(name="site_id", type="string", length=10, nullable=false)
* #ORM\Id
*/
private $siteId;
/**
* #var string
*
* #ORM\Column(name="domain_name", type="string", length=255, nullable=false)
*/
private $domainName;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=30, nullable=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="email_address", type="string", length=254, nullable=false)
*/
private $emailAddress;
/**
* #var string
*
* #ORM\Column(name="layout", type="string", length=30, nullable=true)
*/
private $layout;
/**
* #var string
*
* #ORM\Column(name="homepage", type="string", length=30, nullable=true)
*/
private $homepage;
/**
* #var string
*
* #ORM\Column(name="bookmark_icon", type="string", length=20, nullable=false)
*/
private $bookmarkIcon = 'bookmark.png';
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=200, nullable=false)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="town", type="string", length=30, nullable=false)
*/
private $town;
/**
* #var string
*
* #ORM\Column(name="county", type="string", length=30, nullable=false)
*/
private $county;
/**
* #var \Application\Entity\Countries
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Countries")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
* })
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="post_code", type="string", length=7, nullable=false)
*/
private $postCode;
/**
* #var \Application\Entity\Timezones
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Timezones")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="timezone_id", referencedColumnName="timezone_id")
* })
*/
private $timezone;
/**
* #var string
*
* #ORM\Column(name="locale", type="string", length=5, nullable=false)
*/
private $locale;
/**
* #var string
*
* #ORM\Column(name="currency_code", type="string", length=3, nullable=false)
*/
private $currencyCode;
/**
* #var string
*
* #ORM\Column(name="vat_number", type="string", length=10, nullable=true)
*/
private $vatNumber;
/**
* #var \Application\Entity\VatRates
*
* #ORM\ManyToOne(targetEntity="Application\Entity\VatRates", inversedBy="sites")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vat_rate_id", referencedColumnName="vat_rate_id")
* })
*/
private $vatRate;
/**
* #var \DateTime
*
* #ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* #var \Application\Entity\Users
*
* #ORM\ManyToOne(targetEntity="Application\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="created_by_user_id", referencedColumnName="user_id")
* })
*/
private $createdBy;
/**
* #var Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\Categories", mappedBy="site")
*/
private $categories;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SiteCountries", cascade="persist", mappedBy="site")
*/
private $siteCountries;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SiteShippingMethods", cascade="persist", mappedBy="site")
*/
private $shippingMethods;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\SitePaymentMethods", cascade="persist", mappedBy="site")
*/
private $sitePaymentMethods;
/**
* #var \Application\Entity\SiteModes
*
* #ORM\ManyToOne(targetEntity="Application\Entity\SiteModes")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="site_mode_id", referencedColumnName="site_mode_id")
* })
*/
private $siteMode;
/**
*
* #var integer
* #ORM\Column(name="payment_days", type="integer", nullable=false)
*/
private $paymentDays;
/**
*
* #var integer
* #ORM\Column(name="products_per_page", type="integer", nullable=false)
*/
private $productsPerPage;
/**
* #var \Doctrine\ORM\PersistentCollection
*
* #ORM\OneToMany(targetEntity="Application\Entity\Invoices", mappedBy="site")
* })
*/
private $invoices;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->siteCountries = new ArrayCollection();
$this->shippingMethods = new ArrayCollection();
$this->vatRate = NULL;
$this->sitePaymentMethods = new ArrayCollection();
$this->invoices = new ArrayCollection();
$this->productsPerPage = 15;
}
.... Getters & Setters
/**
* #ORM\PrePersist
* #return \Application\Entity\Users
*/
public function prePersist()
{
$this->dateCreated = $this->getCurrentDateTime();
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
$this->createdBy = $this->getAuthUser();
return $this;
}
/**
* #ORM\PreUpdate
* #return \Application\Entity\Users
*/
public function preUpdate()
{
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
return $this;
}
You already marked the site_id column as your primary column when you added #ORM\Id annotation to the $siteId property. It is not necessary to add the following line:
#ORM\Index(name="PRIMARY", columns={"site_id"}),
Remove that line and you will see that the site_id column will be indexed properly as PRIMARY index automatically in your database.
Read more on this in chapter 4.5. Identifiers / Primary Keys in the doctrine documentation
I am using doctrine 2 within zend framework 2. To generate methods from existing entities using database table, the console command used is:
php doctrine-module orm:generate-entities --generate-annotations="true" --generate-methods="true" module
I have two namespaces Blog and Location
My question is:
1. When I run above code, only blog entities get updated. I want to know why it is behaving like this?
2. If I want to update only a specific entity, how can i do it?
The blog has two entities: Post and cateogry
Category.php
<?php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Category
*
* #ORM\Table(name="Categories")
* #ORM\Entity
*/
class Category implements InputFilterAwareInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
protected $inputFilter;
/**
* Get Id
*
* #param integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Post.php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="posts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="content", type="text", precision=0, scale=0, nullable=false, unique=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $createdDate;
/**
* #var \Blog\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Blog\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true)
* })
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return Post
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set category
*
* #param \Blog\Entity\Category $category
* #return Post
*/
public function setCategory(\Blog\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Blog\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
The Location has Country.php
<?php
namespace Country\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* #ORM\Table(name="countries")
* #ORM\Entity
*/
class Country
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=55, nullable=false)
*/
private $name;
}
OF Course everyone need a function on ZF2 that's turns to possible you generate a single entity. but those times you can't do it just by doctrine-orm-module. When you try to run doctrine in Symphony it's acceptable. I'm using doctrine2 with zf2 having the same issue.
I create a PHP Script that I call
Script to Create a Single Entity:
Choose Entity Name.
Generate All Entities on a Temp Folder.
Exclude All non-needed entities from folder.
Copy Single Entity to "src/$module/Entity" Folder.
Is a hack that i got to make it work property as you need.
Use the --filter option to do this.
php doctrine-module orm:generate-entities --filter="Post" ...
I have a PHPUnit test that's using a Doctrine2 custom repository and Doctrine Fixtures. I wanted to test that a query gave me back an expected entity from my fixture.
But when I try $this->assertEquals($expectedEntity, $result);, I get Fatal error: out of memory. I'm guessing it is recursing into all the relations and the entity manager and whatnot.
Is there a good way to test this equality? Should I just assertEquals on the IDs of the entities?
Edit:
Here is the test code
<?php
use Liip\FunctionalTestBundle\Test\WebTestCase;
class AbstractRepositoryTestCase extends WebTestCase
{
/**
* #var Doctrine\ORM\EntityRepository
*/
protected $repo;
/**
* #var Doctrine\Common\DataFixtures\Executor\AbstractExecutor
*/
protected $fixtureExecutor;
/**
* #var string Which repository to load, overriden by derived class
*/
protected $repoName;
/**
* #var array Fixture classes to load on setup
*/
protected $fixtures = array();
public function setUp()
{
$kernel = static::createKernel();
$this->repo = $kernel->boot();
$this->repo = $kernel->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository($this->repoName);
$this->fixtureExecutor = $this->loadFixtures($this->getFixtures());
}
public function getFixtures()
{
return $this->fixtures;
}
}
class ArticleRepositoryTest extends AbstractRepositoryTestCase
{
/**
* #var string Which repository to load, overriden by derived class
*/
protected $repoName = 'MyMainBundle:Article';
/**
* #var array Fixture classes to load on setup
*/
protected $fixtures = array(
'My\MainBundle\DataFixtures\ORM\LoadUserData',
'My\MainBundle\DataFixtures\ORM\LoadArticleData',
'My\MainBundle\DataFixtures\ORM\LoadFeedsData',
'My\MainBundle\DataFixtures\ORM\LoadFeedDataData',
'My\MainBundle\DataFixtures\ORM\LoadUserReadArticleData',
);
public function testGetNextArticle_ExpectCorrect()
{
/** #var Doctrine\Common\DataFixtures\ReferenceRepository **/
$refRepo = $this->fixtureExecutor->getReferenceRepository();
/** #var FeedStream\MainBundle\Entity\Article **/
$curr = $refRepo->getReference('feed-1-article-3');
$expected = $refRepo->getReference('feed-1-article-2');
$expected2 = $refRepo->getReference('feed-1-article-1');
$next = $this->repo->getNextArticle($curr->getFeed()->getId(), $curr);
$this->assertNotNull($next);
// this is the part that doesn't work
$this->assertEquals($expected, $next);
// this is the code I've used instead
$this->assertEquals($expected->getId(), $next->getId());
}
}
Here is the entity (getters/setters omitted to save space)
<?php
namespace My\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* My\MainBundle\Entity\Article
*
* #ORM\Table(name="articles", uniqueConstraints={
* #ORM\UniqueConstraint(name="feed_guid", columns={"feed_id", "guid"}),
* #ORM\UniqueConstraint(name="article_slug_unique", columns={"feed_id", "slug"})
* })
* #ORM\Entity(repositoryClass="My\MainBundle\Repository\ArticleRepository")
*/
class Article
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $guid
*
* #ORM\Column(name="guid", type="string", length=255, nullable=false)
*/
private $guid;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* #var datetime $pubDate
*
* #ORM\Column(name="pub_date", type="datetime", nullable=true)
*/
private $pubDate;
/**
* #var text $summary
*
* #ORM\Column(name="summary", type="text", nullable=true)
*/
private $summary;
/**
* #var text $content
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string $sourceUrl
*
* #ORM\Column(name="source_url", type="string", length=255, nullable=true)
*/
private $sourceUrl;
/**
* #var string $commentUrl
*
* #ORM\Column(name="comment_url", type="string", length=255, nullable=true)
*/
private $commentUrl;
/**
* #var string $slug
*
* #ORM\Column(name="slug", type="string", length=64, nullable=false)
*/
private $slug;
/**
* #var string $thumbnailFile
*
* #ORM\Column(name="thumbnail_file", type="string", length=64, nullable=true)
*/
private $thumbnailFile;
/**
* #var My\MainBundle\Entity\ArticleEnclosure
*
* #ORM\ManyToOne(targetEntity="My\MainBundle\Entity\ArticleEnclosure")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="thumbnail_enclosure_id", referencedColumnName="id")
* })
*/
private $thumbnailEnclosure;
/**
* #var My\MainBundle\Entity\ArticleImageScrape
*
* #ORM\ManyToOne(targetEntity="My\MainBundle\Entity\ArticleImageScrape")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="thumbnail_scrape_id", referencedColumnName="id", unique=false)
* })
*/
private $thumbnailScrape;
/**
* #var My\MainBundle\Entity\ArticleBitly
*
* #ORM\OneToOne(targetEntity="My\MainBundle\Entity\ArticleBitly", mappedBy="article", orphanRemoval=true)
*/
private $bitly;
/**
* #var My\MainBundle\Entity\ArticleEnclosure
*
* #ORM\OneToMany(targetEntity="My\MainBundle\Entity\ArticleEnclosure", mappedBy="article", orphanRemoval=true)
*/
private $enclosures;
/**
* #var My\MainBundle\Entity\ArticleImageScrape
*
* #ORM\OneToMany(targetEntity="My\MainBundle\Entity\ArticleImageScrape", mappedBy="article", orphanRemoval=true)
*/
private $scrapes;
/**
* #var My\MainBundle\Entity\ArticleLink
*
* #ORM\OneToMany(targetEntity="My\MainBundle\Entity\ArticleLink", mappedBy="article", orphanRemoval=true)
*/
private $links;
/**
* #var My\MainBundle\Entity\Feed
*
* #ORM\ManyToOne(targetEntity="My\MainBundle\Entity\Feed", inversedBy="articles")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="feed_id", referencedColumnName="id", nullable=false)
* })
*/
private $feed;
/**
* #var My\MainBundle\Entity\ArticleAuthor
*
* #ORM\ManyToOne(targetEntity="My\MainBundle\Entity\ArticleAuthor", inversedBy="articles")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="author_id", referencedColumnName="id")
* })
*/
private $author;
public function __construct()
{
$this->links = new \Doctrine\Common\Collections\ArrayCollection();
}
}
You should also test the class in addition to the ID.