Created
November 21, 2011 13:31
-
-
Save raphaeldealmeida/1382623 to your computer and use it in GitHub Desktop.
Erro ao excluir Elemento do ArrayCollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Service; | |
use Application\Entity\Realizacao; | |
class CumprirTarefa { | |
/** | |
* | |
* @var Tarefa | |
*/ | |
protected $tarefa; | |
/** | |
* | |
* @var Usuario | |
*/ | |
protected $usuario; | |
public function __construct($tarefa, $usuario) { | |
$this->tarefa = $tarefa; | |
$this->usuario = $usuario; | |
} | |
public function cumprir() { | |
$realizacoes = $this->usuario->getRealizacoes(); | |
$realizacao=null; | |
foreach ($realizacoes as $r){ | |
if($r->getTarefa() == $this->tarefa){ | |
$realizacao = $r; | |
} | |
} | |
if($this->usuario->pagarEnergia($this->tarefa->getCusto())){ | |
if(is_null($realizacao)){ | |
$realizacao = new Realizacao(); | |
$realizacao->setTarefa($this->tarefa); | |
$this->usuario->addRealizacao($realizacao); | |
} | |
if(count($this->tarefa->getRequisitos()) > 0){ | |
$requisitos = $this->tarefa->getRequisitos(); | |
foreach ($requisitos as $requisito){ | |
$requisito->cumprir($this->usuario); | |
} | |
} | |
$realizacao->setExecucao($realizacao->getExecucao() + 1); | |
if(count($this->tarefa->getBeneficios()) > 0){ | |
$beneficios = $this->tarefa->getBeneficios(); | |
foreach ($beneficios as $beneficio){ | |
$beneficio->fornecerPara($this->usuario); | |
} | |
} | |
}else{ | |
throw new \Exception('Energia Insuficiente'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Entity; | |
/** | |
* @Entity | |
* @Table(name="inventario") | |
*/ | |
class Inventario{ | |
/** | |
* @Id | |
* @ManyToOne(targetEntity="Usuario", inversedBy="inventario") | |
* @var Usuario | |
*/ | |
protected $usuario; | |
/** | |
* @Id | |
* @ManyToOne(targetEntity="Item", fetch="EAGER") | |
* @var Item | |
*/ | |
protected $item; | |
/** | |
* @Column(type="integer") | |
* @var int | |
*/ | |
protected $quantidade = 1; | |
public function getUsuario() { | |
return $this->usuario; | |
}<?php | |
namespace Application\Entity; | |
/** | |
* @Entity | |
* @Table(name="inventario") | |
*/ | |
class Inventario{ | |
/** | |
* @Id | |
* @ManyToOne(targetEntity="Usuario", inversedBy="inventario") | |
* @var Usuario | |
*/ | |
protected $usuario; | |
/** | |
* @Id | |
* @ManyToOne(targetEntity="Item", fetch="EAGER") | |
* @var Item | |
*/ | |
protected $item; | |
/** | |
* @Column(type="integer") | |
* @var int | |
*/ | |
protected $quantidade = 1; | |
public function getUsuario() { | |
return $this->usuario; | |
} | |
public function setUsuario($usuario) { | |
$this->usuario = $usuario; | |
} | |
public function getItem() { | |
return $this->item; | |
} | |
public function setItem($item) { | |
$this->item = $item; | |
} | |
public function getQuantidade() { | |
return $this->quantidade; | |
} | |
public function setQuantidade($quantidade) { | |
$this->quantidade = $quantidade; | |
} | |
public function addQuantidade($quantidade) { | |
$this->quantidade += $quantidade; | |
} | |
public function subQuantidade($quantidade) { | |
if(($this->quantidade - $quantidade) <= 0){ | |
return false; | |
}else{ | |
$this->quantidade -= $quantidade; | |
} | |
} | |
} | |
public function setUsuario($usuario) { | |
$this->usuario = $usuario; | |
} | |
public function getItem() { | |
return $this->item; | |
} | |
public function setItem($item) { | |
$this->item = $item; | |
} | |
public function getQuantidade() { | |
return $this->quantidade; | |
} | |
public function setQuantidade($quantidade) { | |
$this->quantidade = $quantidade; | |
} | |
public function addQuantidade($quantidade) { | |
$this->quantidade += $quantidade; | |
} | |
public function subQuantidade($quantidade) { | |
if(($this->quantidade - $quantidade) <= 0){ | |
return false; | |
}else{ | |
$this->quantidade -= $quantidade; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Entity; | |
/** | |
* @Entity | |
* @Table(name="itens") | |
*/ | |
class Item{ | |
/** | |
* @Id @Column(type="integer") | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @Column(type="string") | |
*/ | |
protected $nome; | |
public function getId() { | |
return $this->id; | |
} | |
public function setId($id) { | |
$this->id = $id; | |
} | |
public function setNome($nome){ | |
$this->nome = $nome; | |
} | |
public function getNome(){ | |
return $this->nome; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Entity; | |
/** | |
* @Entity | |
* @Table(name="requisito_item") | |
*/ | |
class RequisitoItem extends Requisito{ | |
/** | |
* @ManyToOne(targetEntity="Item") | |
* @var Item | |
*/ | |
protected $item; | |
public function setItem(Item $item) { | |
$this->item = $item; | |
} | |
public function getItem() { | |
return $this->item; | |
} | |
/** | |
* | |
* @param Usuario $usuario | |
* @return int Key for Inventario | |
*/ | |
public function verificar(Usuario $usuario){ | |
$inventarios = $usuario->getInventario()->toArray(); | |
$inventario=null; | |
$key=null; | |
foreach ($inventarios as $k => $i){ | |
if($i->getItem()->getId() == $this->item->getId()){ | |
$inventario = $i; | |
$key = $k; | |
} | |
} | |
if(is_null($inventario)){ | |
throw new \Exception('Não possui o item: '.$this->item->getNome()); | |
} | |
return $key; | |
} | |
public function cumprir(Usuario $usuario){ | |
$key = $this->verificar($usuario); | |
$inventario = $usuario->getInventario()->get($key); | |
if(!$inventario->subQuantidade(1)){ | |
$usuario->getInventario()->removeElement($inventario); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Entity; | |
use \DateTime, | |
\DateInterval, | |
Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* @Entity | |
* @Table(name="usuarios") | |
*/ | |
class Usuario{ | |
/** | |
* @Id @Column(type="integer") | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @OneToMany(targetEntity="Inventario", mappedBy="usuario",cascade={"persist", "remove"}) | |
* @var ArrayCollection | |
*/ | |
protected $inventario; | |
public function __construct() | |
{ | |
$this->inventario = new ArrayCollection(); | |
} | |
public function getInventario() { | |
return $this->inventario; | |
} | |
public function addItem(Item $item) { | |
$inventario = new Inventario(); | |
$inventario->setItem($item); | |
$inventario->setUsuario($this); | |
$key = false; | |
foreach ($this->inventario as $k => $i){ | |
if($i->getItem()->getId() == $item->getId()){ | |
var_dump($key = $k); | |
break; | |
} | |
} | |
if($key === false){ | |
$this->inventario->add($inventario); | |
}else{ | |
$inventario = $this->inventario->get($key); | |
$inventario->addQuantidade(1); | |
} | |
} | |
public function getId() { | |
return $this->id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment