Skip to content

Commit

Permalink
Set foreign keys in Model, Views of Doctrine
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Oct 13, 2024
1 parent 5ff680c commit 7d4709d
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 19 deletions.
15 changes: 2 additions & 13 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
multi relations in model
fix incorrect return type in repository (no "\")
pagination template if bootstrap

------

/**
* @ManyToOne(targetEntity="User", cascade={"persist"})
* @JoinColumn(name="user_id", referencedColumnName="id", nullable=true, unique=false)
*
* @var \User|null
*/
protected $user = null;
multi-relation in view from wildfire
pagination template if bootstrap
1 change: 1 addition & 0 deletions phpstyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
$groups = [];
$groups[] = ['template', 'extends'];
$groups[] = ['deprecated', 'link', 'see', 'since', 'codeCoverageIgnore'];
$groups[] = ['ManyToOne', 'JoinColumn'];
$groups[] = ['property', 'property-read', 'property-write'];
$groups[] = ['method'];
$groups[] = ['author', 'copyright', 'license'];
Expand Down
64 changes: 62 additions & 2 deletions src/Template/Doctrine/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ protected function setMethods($cols)
{
foreach ($cols as $col)
{
// Add additional method for foreign keys --------
if ($col->isForeignKey())
{
$name = $col->getReferencedTable();
$name = Inflector::singular($name);

$method = 'get_' . $name;

$method = new Method($method);

$type = '\\' . ucfirst($name);

if ($col->isNull())
{
$type = $type . '|null';
}

$method->setReturn($type);

$fn = function ($lines) use ($name)
{
$lines[] = 'return $this->' . $name . ';';

return $lines;
};

$method->setCodeLine($fn);

$this->addMethod($method);
}
// -----------------------------------------------

$name = $col->getField();

$name = Inflector::snakeCase($name);
Expand Down Expand Up @@ -118,12 +150,14 @@ protected function setMethods($cols)

$method->setReturn($type);

$method->setCodeLine(function ($lines) use ($name)
$fn = function ($lines) use ($name)
{
$lines[] = 'return $this->' . $name . ';';

return $lines;
});
};

$method->setCodeLine($fn);

$this->addMethod($method);
}
Expand Down Expand Up @@ -220,6 +254,32 @@ protected function setProperties($cols)

$name = Inflector::snakeCase($name);

if ($col->isForeignKey())
{
$foreignTable = $col->getReferencedTable();
$foreignName = Inflector::singular($foreignTable);
$class = ucfirst($foreignName);
$foreign = $col->getReferencedField();

$this->addClassProperty($foreignName, $class, $isNull);

// Generate Doctrine annotations to foreign key -------
$lines = array();

$keys = array('targetEntity="' . $class . '"');
$keys[] = 'cascade={"persist"}';
$lines[] = '@ManyToOne(' . implode(', ', $keys) . ')';

$keys = array('name="' . $name . '"');
$keys[] = 'referencedColumnName="' . $foreign . '"';
$keys[] = 'nullable=' . ($isNull ? 'true' : 'false');
$keys[] = 'unique=' . ($isUnique ? 'true' : 'false');
$lines[] = '@JoinColumn(' . implode(', ', $keys) . ')';

$this->withComment($lines);
// ----------------------------------------------------
}

$type = $col->getDataType();

switch ($type)
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function setSetMethod($table)

$method = new Method('set');

$method->setReturn($model);
$method->setReturn('\\' . $model);
$method->addArrayArgument('data', 'array<string, mixed>');
$method->addClassArgument('entity', $model)
->withoutTypeDeclared();
Expand Down
12 changes: 12 additions & 0 deletions src/Template/TablePlate.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ protected function setRow($lines, $tab = '')
$field = $field . '()';
}

if ($this->type === self::TYPE_DOCTRINE && $col->isForeignKey())
{
$foreign = $col->getReferencedField();
$table = $col->getReferencedTable();
$table = Inflector::singular($table);

$method = 'get_' . strtolower($table) . '()';
$foreign = 'get_' . Inflector::snakeCase($foreign) . '()';

$field = $method . '->' . $foreign;
}

$name = str_replace('[FIELD]', $field, $name);

$plate = $space . '<td>' . $name . '</td>';
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixture/Plates/Doctrine/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class Post extends Model
*/
protected $updated_at = null;

/**
* @ManyToOne(targetEntity="User", cascade={"persist"})
* @JoinColumn(name="user_id", referencedColumnName="id", nullable=true, unique=false)
*
* @var \User|null
*/
protected $user = null;

/**
* @Column(name="user_id", type="integer", nullable=true, unique=false)
*
Expand Down Expand Up @@ -139,6 +147,14 @@ public function get_updated_at()
return $this->updated_at;
}

/**
* @return \User|null
*/
public function get_user()
{
return $this->user;
}

/**
* @return integer|null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<tr>
<td><?= $item->get_title() ?></td>
<td><?= $item->get_text() ?></td>
<td><?= $item->get_user_id() ?></td>
<td><?= $item->get_user()->get_id() ?></td>
<td><?= $item->get_created_at() ? $item->get_created_at()->format('Y-m-d H:i:s') : '' ?></td>
<td><?= $item->get_updated_at() ? $item->get_updated_at()->format('Y-m-d H:i:s') : '' ?></td>
<td><?= $item->get_deleted_at() ? $item->get_deleted_at()->format('Y-m-d H:i:s') : '' ?></td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<tr>
<td><?= $item->get_title() ?></td>
<td><?= $item->get_text() ?></td>
<td><?= $item->get_user_id() ?></td>
<td><?= $item->get_user()->get_id() ?></td>
<td><?= $item->get_created_at() ? $item->get_created_at()->format('Y-m-d H:i:s') : '' ?></td>
<td><?= $item->get_updated_at() ? $item->get_updated_at()->format('Y-m-d H:i:s') : '' ?></td>
<td><?= $item->get_deleted_at() ? $item->get_deleted_at()->format('Y-m-d H:i:s') : '' ?></td>
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/Plates/Doctrine/Repos/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function exists($data, $id = null)
* @param \User $entity
* @param integer|null $id
*
* @return User
* @return \User
*/
public function set($data, $entity, $id = null)
{
Expand Down

0 comments on commit 7d4709d

Please sign in to comment.