Doctrine & Symfony - No hydratation data from custom query - php

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

Related

Doctrine Collection Entities not fully populated

I made a mapping of 4 entities, but when I view the objects in the collection , all objects are populated except the last.
Here Entities
User.php
/**
* User
*
* #ORM\Table(name="`user`")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255,nullable=false)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=255,nullable=false)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $surname;
/**
*
* #ORM\OneToMany(targetEntity="Affectation",mappedBy="user",cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
public function __construct()
{
parent::__construct();
$this->affectations = new \Doctrine\Common\Collections\ArrayCollection();
}
...... getters and setters ....
Affectation.php
/**
* Affectation
*
* #ORM\Table(name="affectation")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\AffectationRepository")
*/
class Affectation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="User",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="uid",referencedColumnName="id")
*/
private $user;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="icgo\WorkPlaceBundle\Entity\Workplace",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="wid",referencedColumnName="id")
*/
private $workplace;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Role",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="rid",referencedColumnName="rid")
*/
private $role;
.... getters and setters .....
Workplace.php
/**
* workplace
*
* #ORM\Table(name="workplace")
* #ORM\Entity(repositoryClass="icgo\WorkPlaceBundle\Repository\WorkplaceRepository")
*/
class Workplace
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="wfa", type="string", length=1, nullable=false)
*/
private $famille;
/**
* #var string
*
* #ORM\Column(name="nickname", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $nickname;
/**
* #var \DateTime
*
* #ORM\Column(name="date_start", type="date")
* #Assert\Date()
*/
private $dateStart;
/**
* #var \DateTime
*
* #ORM\Column(name="date_end", type="date")
* #Assert\Date()
*/
private $dateEnd;
/**
* #var string
*
* #ORM\Column(name="label_short", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $labelShort;
/**
* #var string
*
* #ORM\Column(name="label_long", type="text")
*/
private $labelLong;
/**
*
* #ORM\OneToMany(targetEntity="icgo\AdminBundle\Entity\Affectation",mappedBy="workplace")
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
.... getters and setters .....
Role.php
/**
* Role
*
* #ORM\Table(name="role")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\RoleRepository")
*/
class Role
{
/**
* #var int
*
* #ORM\Column(name="rid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="label_short", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $labelShort;
/**
* #var string
*
* #ORM\Column(name="label_long", type="text", nullable=false)
*/
private $labelLong;
/**
*
* #ORM\OneToMany(targetEntity="Affectation",mappedBy="role")
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
.... getters and setters ......
UserRepository.php
class UserRepository extends \Doctrine\ORM\EntityRepository
{
public function getUsers(){
$query = $this->createQueryBuilder('u')
->select('u','a','r')
->addSelect('w')
->leftJoin('u.affectations','a')
->leftJoin('a.workplace','w')
->leftJoin('a.role','r')
->getQuery();
return $query->getResult();
}
}
Result dump
Dump image
As see, for the last object User , the table assignments, workplace and role objects are null. All the previous user are populated
Thanks for help

An index with name 'primary' was already defined on table 'sites'

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

Can't Create association on Symfony2

I'm trying to create a "OneToMany" bidirectional association in my project but when I execute "doctrine:schema:update" nothing happens.
If I create this association directly from Sequel Pro and run the update schema command, that changes dissapear... :/
The relations is:
- One "id" from Customers Table with many "customer_id" form Control table.
Here is the Customers code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Customers
*
* #ORM\Table()
* #ORM\Entity
*/
class Customers
{
/* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=100)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="address", type="text")
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=100)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="pass", type="string", length=100)
*/
private $pass;
/**
* #var string
*
* #ORM\Column(name="tasks", type="text")
*/
private $tasks;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=100)
*/
private $status;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=100)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="location", type="string", length=100)
*/
private $location;
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customers")
*/
private $customer_id;
public function __construct()
{
$this->customer_id = new ArrayCollection();
}
And the Control code:
<?php
namespace Ourentec\CustomersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Control
*
* #ORM\Table()
* #ORM\Entity
*/
class Control
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="control")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customerId;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
*/
private $userId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var integer
*
* #ORM\Column(name="seen", type="smallint")
*/
private $seen;
I followed the documentation from this 2 websites
http://symfony.com/doc/current/book/doctrine.html
http://librosweb.es/libro/symfony_2_x/capitulo_8/relaciones_y_asociaciones_de_entidades.html
But I don't know why it does not work..
Any idea will be appreciated :)
Mapping are not correct, I will try to explain how it works.
In Customers entity (you should rename it to Customer, entites names are singular)
/**
* #ORM\OneToMany(targetEntity="Control", mappedBy="customer")
*/
private $controls;
Mapped by option defines field name in the other entity.
/**
* #var integer
*
* #ORM\Column(name="customer_id", type="integer")
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="controls")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
Same thing with inversedBy.
In Customers entity you also need to init controls var as an ArrayCollection:
public function __construct()
{
$this->controls = new ArrayCollection();
}
With these mappings schema should be updated correctly.
For more info, check doctrine docs.

Doctrine - ManyToOne with table between

I want to create model which will have table users with reference to table CustomFieldValue nad table CustomField with reference to CustomFieldValue too. CustomFieldValue will have only id, value and two columns, one from users and second from CustomField. I want to have functionality like a dynamic adding a new fields in registration form. Is this good idea? If yes, please help me with this model, because it doesn't work:
User:
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* #ORM\Column(type="string", length=64)
*/
private $password;
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users",cascade={"persist"})
*
*/
private $roles;
/**
* #ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* #ORM\Column(type="string", length=60)
*/
private $sex;
/**
* #ORM\ManyToMany(targetEntity="Region", inversedBy="users")
* #ORM\JoinColumn(name="region", referencedColumnName="id")
*/
private $region;
/**
* #ORM\ManyToMany(targetEntity="Type", inversedBy="users")
* #ORM\JoinColumn(name="type", referencedColumnName="id")
*/
private $type;
/**
* #ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue",inversedBy="id")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $customValues;
CustomFieldValue:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="value", type="string", length=255)
*/
private $value;
/**
*ORM\OneToMany(targetEntity="User", mapped-by="id")
*#ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
*#ORM\OneToMany(targetEntity="CustomField", mapped-by="id" )
*#ORM\JoinColumn(name="field_id", referencedColumnName="id")
*/
private $field;
CustomField:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* #var boolean
*
* #ORM\Column(name="required", type="boolean")
*/
private $required;
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="id")
* #ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
*/
private $customValues;
Your mapping is a little off:
User Entity Should Be:
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="user")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $customValues;
CustomFieldValue Should Be:
/**
*ORM\OneToMany(targetEntity="User", mappedBy="customValues")
*/
private $user;
/**
*#ORM\OneToMany(targetEntity="CustomField", mappedBy="customValues" )
*/
private $field;
CustomField should be:
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="field")
* #ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
*/
private $customValues;
You dont need the join columns when you are calling the mappedBy this already tells doctrine to look for the join column declaration on that field. For the mappedBy and inversedBy fields these are the fields that link the 2 together NOT the actual join column name.

Testing a Doctrine2 Entity with assertEquals results in fatal out-of-memory error

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.

Categories