Skip to content

Instantly share code, notes, and snippets.

@alpha1125
Created November 1, 2017 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpha1125/2fda174807fdd36d6b74d18132b5086f to your computer and use it in GitHub Desktop.
Save alpha1125/2fda174807fdd36d6b74d18132b5086f to your computer and use it in GitHub Desktop.
Create one to many relationship, this is a template, to generate the code needed to make oneToMany relationships. Change $A and $B accordingly.
<?php
$A = 'Product';
$B = 'Feature';
echo <<< EOD
<pre>
/**
* @See: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
* as of 2017-11-01
*
* \$A = one
* \$B = many
*
* ONE to Many
* \$A => \$B
* $A => $B
*
*/
</pre>
EOD;
$Bs = $B . 's';
$a = lcfirst($A);
$b = lcfirst($B);
$bs = lcfirst($Bs);
$string = <<< EOD
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity */
class $A
{
// ...
/**
* One $A has Many $Bs.
* @ORM\OneToMany(targetEntity="$B", mappedBy="$a")
*/
protected \$$bs;
// ...
public function __construct()
{
\$this->$bs = new ArrayCollection();
}
// ... GETTERS ...
// ... SETTERS ...
}
/** @Entity */
class $B
{
// ...
/**
* Many $Bs have One $A.
* @ORM\ManyToOne(targetEntity="$A", inversedBy="$bs")
*/
protected \$$a;
// ...
// ... GETTERS ...
// ... SETTERS ...
}
EOD;
ini_set("highlight.comment", "#008000");
ini_set("highlight.default", "#000000");
ini_set("highlight.html", "#808080");
ini_set("highlight.keyword", "#0000BB; font-weight: bold");
ini_set("highlight.string", "#DD0000");
highlight_string($string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment