Skip to content

Instantly share code, notes, and snippets.

@ReSpawN ReSpawN/Account.php
Created Dec 18, 2018

Embed
What would you like to do?
SfCasts
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
*/
class Account
{
/**
* @ORM\Column(type="float")
*/
private $balance = 0;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Bank", inversedBy="accounts")
*/
private $bank;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $deleted = false;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $digital = true;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Filter", mappedBy="account")
*/
private $filters;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Iban()
*/
private $iban;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Import", mappedBy="account", orphanRemoval=true)
*/
private $imports;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PiggyBank", mappedBy="account")
*/
private $piggyBanks;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Record", mappedBy="account")
*/
private $records;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Schedule", mappedBy="account", orphanRemoval=true)
*/
private $schedules;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Transaction", mappedBy="account")
*/
private $transactions;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="accounts")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
public function __construct()
{
$this->transactions = new ArrayCollection();
$this->filters = new ArrayCollection();
$this->records = new ArrayCollection();
$this->schedules = new ArrayCollection();
$this->piggyBanks = new ArrayCollection();
$this->imports = new ArrayCollection();
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getBank(): ?Bank
{
return $this->bank;
}
public function setBank(?Bank $bank): self
{
$this->bank = $bank;
return $this;
}
/**
* @return Collection|Transaction[]
*/
public function getTransactions(): Collection
{
return $this->transactions;
}
public function addTransaction(Transaction $transaction): self
{
if (!$this->transactions->contains($transaction)) {
$this->transactions[] = $transaction;
$transaction->setAccount($this);
}
return $this;
}
public function removeTransaction(Transaction $transaction): self
{
if ($this->transactions->contains($transaction)) {
$this->transactions->removeElement($transaction);
// set the owning side to null (unless already changed)
if ($transaction->getAccount() === $this) {
$transaction->setAccount(null);
}
}
return $this;
}
public function __toString()
{
return sprintf('%s (%s, %d)', $this->getName(), $this->getIBAN(), $this->getId());
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getIBAN(): ?string
{
return $this->iban;
}
public function setIBAN(string $iban): self
{
$this->iban = $iban;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|Filter[]
*/
public function getFilters(): Collection
{
return $this->filters;
}
public function addFilter(Filter $filter): self
{
if (!$this->filters->contains($filter)) {
$this->filters[] = $filter;
$filter->setAccount($this);
}
return $this;
}
public function removeFilter(Filter $filter): self
{
if ($this->filters->contains($filter)) {
$this->filters->removeElement($filter);
// set the owning side to null (unless already changed)
if ($filter->getAccount() === $this) {
$filter->setAccount(null);
}
}
return $this;
}
/**
* @return Collection|Record[]
*/
public function getRecords(): Collection
{
$criteria = (Criteria::create())
->where(Criteria::expr()->eq('user', $this->user));
return $this->records->matching($criteria);
}
public function addRecord(Record $record): self
{
if (!$this->records->contains($record)) {
$this->records[] = $record;
$record->setAccount($this);
}
return $this;
}
public function removeRecord(Record $record): self
{
if ($this->records->contains($record)) {
$this->records->removeElement($record);
// set the owning side to null (unless already changed)
if ($record->getAccount() === $this) {
$record->setAccount(null);
}
}
return $this;
}
public function getBalance(): ?float
{
return $this->balance;
}
public function setBalance(float $balance): self
{
$this->balance = $balance;
return $this;
}
public function getDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(?bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
/**
* @return Collection|Schedule[]
*/
public function getSchedules(): Collection
{
return $this->schedules;
}
public function addSchedule(Schedule $schedule): self
{
if (!$this->schedules->contains($schedule)) {
$this->schedules[] = $schedule;
$schedule->setAccount($this);
}
return $this;
}
public function removeSchedule(Schedule $schedule): self
{
if ($this->schedules->contains($schedule)) {
$this->schedules->removeElement($schedule);
// set the owning side to null (unless already changed)
if ($schedule->getAccount() === $this) {
$schedule->setAccount(null);
}
}
return $this;
}
public function mutateBalance(float $mutation): self
{
$this->balance += $mutation;
return $this;
}
/**
* @return Collection|PiggyBank[]
*/
public function getPiggyBanks(): Collection
{
return $this->piggyBanks;
}
public function addPiggyBank(PiggyBank $piggyBank): self
{
if (!$this->piggyBanks->contains($piggyBank)) {
$this->piggyBanks[] = $piggyBank;
$piggyBank->setAccount($this);
}
return $this;
}
public function removePiggyBank(PiggyBank $piggyBank): self
{
if ($this->piggyBanks->contains($piggyBank)) {
$this->piggyBanks->removeElement($piggyBank);
// set the owning side to null (unless already changed)
if ($piggyBank->getAccount() === $this) {
$piggyBank->setAccount(null);
}
}
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|Import[]
*/
public function getImports(): Collection
{
return $this->imports;
}
public function addImport(Import $import): self
{
if (!$this->imports->contains($import)) {
$this->imports[] = $import;
$import->setAccount($this);
}
return $this;
}
public function removeImport(Import $import): self
{
if ($this->imports->contains($import)) {
$this->imports->removeElement($import);
// set the owning side to null (unless already changed)
if ($import->getAccount() === $this) {
$import->setAccount(null);
}
}
return $this;
}
public function getDigital(): ?bool
{
return $this->digital;
}
public function setDigital(bool $digital): self
{
$this->digital = $digital;
return $this;
}
}
<?php
// Assuming I've got the Transaction stack in a loop
/** @var Transaction[] $transactions */
foreach($transactions as $transaction) {
// Fetches filters via Transaction.account_id to Filter.account_id, does not work
$transaction->getFilters();
// What does work is the following, but I find this to be the long way around
$transaction->getAccount()->getFilters();
// I realise that I can store all filters based on their account_id in a variable, stack them up and fetch them as needed, but that
// way I won't learn anything so I want to know if it can be done with some kind of JoinColumn or JoinTable logic.
}
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\FilterRepository")
*/
class Filter implements UpdatableEntityInterface
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="filters")
*/
private $account;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="filter")
*/
private $category;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="json_array")
*/
private $matches;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Shop", inversedBy="filters")
*/
private $shop;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updated;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="filters")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
public function __construct()
{
$this->created = new \DateTime;
$this->category = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getAccount(): ?Account
{
return $this->account;
}
public function setAccount(?Account $account): self
{
$this->account = $account;
return $this;
}
public function getShop(): ?Shop
{
return $this->shop;
}
public function setShop(?Shop $shop): self
{
$this->shop = $shop;
return $this;
}
/**
* @return Collection|Category[]
*/
public function getCategory(): Collection
{
return $this->category;
}
public function addCategory(Category $category): self
{
if (!$this->category->contains($category)) {
$this->category[] = $category;
$category->setFilter($this);
}
return $this;
}
public function removeCategory(Category $category): self
{
if ($this->category->contains($category)) {
$this->category->removeElement($category);
// set the owning side to null (unless already changed)
if ($category->getFilter() === $this) {
$category->setFilter(null);
}
}
return $this;
}
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getUpdated(): ?\DateTimeInterface
{
return $this->updated;
}
public function setUpdated(?\DateTimeInterface $updated): self
{
$this->updated = $updated;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getMatches()
{
return $this->matches;
}
public function setMatches($matches): self
{
$this->matches = $matches;
return $this;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TransactionRepository")
* @ORM\Table(indexes={@ORM\Index(name="hash", columns={"hash"})})
*/
class Transaction implements UpdatableEntityInterface
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="transactions")
* @ORM\JoinColumn(nullable=false)
*/
private $account;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $body;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Import", inversedBy="transactions")
* @ORM\JoinColumn(referencedColumnName="account_id", name="account_id")
*/
private $filters;
/**
* @ORM\Column(type="string", length=32)
*/
private $hash;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Import", inversedBy="transactions")
* @ORM\JoinColumn(nullable=false)
*/
private $import;
/**
* @ORM\Column(type="float")
*/
private $mutation;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $processed;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Record", mappedBy="transaction", cascade={"persist", "remove"})
*/
private $record;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $target;
/**
* @ORM\Column(type="date")
*/
private $transacted;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updated;
public function __construct()
{
$this->created = new \DateTime;
}
public function getId(): ?int
{
return $this->id;
}
public function getAccount(): ?Account
{
return $this->account;
}
public function setAccount(?Account $account): self
{
$this->account = $account;
return $this;
}
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getRecord(): ?Record
{
return $this->record;
}
public function setRecord(?Record $record): self
{
$this->record = $record;
// set (or unset) the owning side of the relation if necessary
$newTransaction = $record === null ? null : $this;
if ($newTransaction !== $record->getTransaction()) {
$record->setTransaction($newTransaction);
}
return $this;
}
public function getProcessed(): ?\DateTimeInterface
{
return $this->processed;
}
public function setProcessed(?\DateTimeInterface $processed): self
{
$this->processed = $processed;
return $this;
}
public function getUpdated(): ?\DateTimeInterface
{
return $this->updated;
}
public function setUpdated(?\DateTimeInterface $updated): self
{
$this->updated = $updated;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(string $hash): self
{
$this->hash = $hash;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description ?: null;
return $this;
}
public function getTarget(): ?string
{
return $this->target;
}
public function setTarget(?string $target): self
{
$this->target = $target;
return $this;
}
public function getMutation(): ?float
{
return $this->mutation;
}
public function setMutation(float $mutation): self
{
$this->mutation = $mutation;
return $this;
}
public function getBody(): ?string
{
return $this->body;
}
public function setBody(?string $body): self
{
$this->body = $body ?: null;
return $this;
}
public function getTransacted(): ?\DateTimeInterface
{
return $this->transacted;
}
public function setTransacted(\DateTimeInterface $transacted): self
{
$this->transacted = $transacted;
return $this;
}
public function getImport(): ?Import
{
return $this->import;
}
public function setImport(?Import $import): self
{
$this->import = $import;
return $this;
}
/**
* @return mixed
*/
public function getFilters()
{
return $this->filters;
}
/**
* @param mixed $filters
*/
public function setFilters($filters): void
{
$this->filters = $filters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.