Really this sounds a little like what you want are OneToMany relationships
between Location -> Vote and Article -> Vote BUT you can use inheritance to
manage things properly.
Something like:
I'm using the following without issue:
<?php
abstract class EntitAbstract
{
public function __get($name) { return $this->$name; }
public function __set($name, $value) { $this->$name = $value; }
public function __call($name, $args) { /* .. some nice logic to make
getters & setters work wonders .. */ }
/* .. other nice common helper functions .. **/
}
/** @appropriate namespace and use statements@ **/
/**
* @ORM\Table(name="location")
* @ORM\Entity
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Location extends EntityAbstract
{
/* ... */
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="VoteLocation", mappedBy="location")
*/
protected $votes;
public function __construct()
{
parent::__construct();
$this->votes = new ArrayCollection();
}
}
/**
* @ORM\Table(name="article")
* @ORM\Entity
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Article extends EntityAbstract
{
/* ... */
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="VoteArticle", mappedBy="article")
*/
protected $votes;
public function __construct()
{
parent::__construct();
$this->votes = new ArrayCollection();
}
}
/**
* @ORM\Table(name="vote")
* @ORM\Entity
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="_discr", type="string", length=10)
* @ORM\DiscriminatorMap({
* "location" = "VoteLocation",
* "article" = "VoteArticle",
* })
*
*/
class Vote extends EntityAbstract
{
/* .. your COMMON fields go here .. */
}
/**
* @ORM\Entity
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class VoteLocation extends Vote
{
/**
* @var Location
* @ORM\ManyToOne(targetEntity="Location", inversedBy="votes")
*/
protected $location;
}
/**
* @ORM\Entity
* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class VoteArticle extends Vote
{
/**
* @var Article
* @ORM\ManyToOne(targetEntity="Article", inversedBy="votes")
*/
protected $artcile;
}
/* To use this wonderful stuff */
$location = LoadEntity('Location', $id);
// Returns an ArrayCollection of VoteLocation entities
$votes = $location->votes;
// Returns the integer (this is the same as calling $votes->count() after
the above line)
$voteCount = $location->votes->count();
// etc..
/* The same can be repeated for article.. */
$article = LoadEntity('Article', $id);
// Returns an ArrayCollection of VoteLocation entities
$votes = $article->votes;
// Returns the integer (this is the same as calling $votes->count() after
the above line)
$voteCount = $article->votes->count();
// etc..
/* Say you have a specific vote.. */
$vote = LoadEntity('Vote', $id); // Notice I asked for the BASE entity Vote
instead of the spcific ones, through the magic of the ORM it will
automatically create the appropriate sub entity for me
// You can now use a switch to determine the kind of vote that it is.. or
we can add a helper function to the Vote* classes:
class VoteLocation ...
{
// ....
public function getParent()
{
return $this->location;
}
}
// Now we can do:
$parent = $vote->getParent(); // Now we'll have a reference to the parent,
you'll still need to check at some point what kind of class it is though to
do anything intelligent with it
/* Creation of a new vote.. */
$location = LoadEntity('Location', $id);
$vote = new VoteLocation();
$vote->location = $location;
$vote->dataPoint = $dataValue;
/* etc etc */
$em->persist($vote);
$location->votes->add($vote);
Be aware you should probably have a bunch of setters / helper functions
that are doing validation on your values to ensure you don't create a
VoteArticle and attempt to assign an article too it. Either way this should
probably work for you.
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.