FRAMEWORK MIOLO - modulo de exemplo - Parte II
Autor: Sandra Martins | Publicado: 2011-05-31T08:24:00-03:00

Vamos agora fazer as classes do domínio. Elas implementam as regras de negócio e encapsulam o acesso à base de dados. As regras de negócio são implementadas através dos métodos do objeto, geralmente através da interação com outros objetos. O acesso à base de dados pode ser feito através da camada DAO do Miolo (através das classes MDatabase, MSQL, MQuery entre outras) ou através da camada de persistência de objetos. 

Todas as classes de domínio herdam de MBusiness e são persistentes. 

As definições de classes ficam no diretório equipamento/classes. 

Os mapas XML usados pela camada de persistência ficam em equipamento/classes/map.



O código a seguir mostra o mapeamento XML - Categoria.
/equipamento/classes/map/categoria.xml

&lt;?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?&gt;
&lt;map&gt;
&nbsp;&nbsp;&nbsp;&lt;moduleName&gt;equipamento&lt;/moduleName&gt;
&nbsp;&nbsp;&nbsp;&lt;className&gt;categoria&lt;/className&gt;
&nbsp;&nbsp;&nbsp;&lt;tableName&gt;categoria&lt;/tableName&gt;
&nbsp;&nbsp;&nbsp;&lt;databaseName&gt;equipamento&lt;/databaseName&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;idcategoria&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;idcategoria&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;key&gt;primary&lt;/key&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;idgenerator&gt;seq_categoria&lt;/idgenerator&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;nome&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;nome&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;

&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;categoria&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;


&nbsp;&nbsp;&nbsp;&lt;association&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toClassModule&gt;equipamento&lt;/toClassModule&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toClassName&gt;categoria&lt;/toClassName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;cardinality&gt;oneToMany&lt;/cardinality&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;target&gt;material&lt;/target&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;retrieveAutomatic&gt;false&lt;/retrieveAutomatic&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;saveAutomatic&gt;false&lt;/saveAutomatic&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;entry&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fromAttribute&gt;idcategoria&lt;/fromAttribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toAttribute&gt;idcategoria&lt;/toAttribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/entry&gt;
&nbsp;&nbsp;&nbsp;&lt;/association&gt; 
&lt;/map&gt;





O código a seguir mostra a definição da classe Categoria
/equipamento/classes/categoria.class

&lt;?php

// As classes de dominio herdam de MBusiness e sao persistentes
// O nome da classe de dominio segue o padrao Business
// para permitir que classes com o mesmo nome sejam usadas em modulos diferentes
class BusinessEquipamentoCategoria extends MBusiness
{
// atributos
public $idCategoria;
public $nome;
public $materiais;

// método construtor 
// No Miolo as classes sao instanciadas através
// de $MIOLO-&gt;getBusiness(modulo,classe,parametro)
// se o parametro for um objeto, o construtor chama o metodo setData(parametero)
// se o parametro for um valor, o construtor chama o metodo getById(parametro)
function __construct($data=NULL)
{
// executa o construtor da classe pai; 'equipamento' é o nome da 
// configuracao do banco de dados
// definida no arquivo miolo.conf
parent::__construct('equipamento',$data);
// equipamento de inicializacao de um atributo
$this-&gt;nome = '';
}

// acessa o banco de dados para recuperar o objeto com id fornecido
// e retorna o proprio objeto, já com os atributos preenchidos
function getById($id)
{
$this-&gt;idCategoria = $id;
$this-&gt;retrieve();
return $this;
}

// exemplos de metodos setter/getter
function setNome($nome)
{
$this-&gt;nome = $nome;
}

function getNome()
{
return $this-&gt;nome;
}

// recebe um "transfer object" e preenche os atributos
function setData($data)
{
$this-&gt;idCategoria = $data-&gt;idCategoria;
$this-&gt;nome = $data-&gt;nome;
}

// recupera os registros no banco de dados e retorna um objeto MQuery
function listAll()
{
$criteria =  $this-&gt;getCriteria();
$criteria-&gt;addOrderAttribute('nome');
return $criteria-&gt;retrieveAsQuery();
}

// recupera os registros no banco de dados segundo um criterio
// e retorna um objeto MQuery
function listByNome($nome)
{
$criteria =  $this-&gt;getCriteria();
$criteria-&gt;addCriteria('nome','LIKE', "$nome");
$criteria-&gt;addOrderAttribute('nome');
return $criteria-&gt;retrieveAsQuery();
}

// recupera um array de os objetos "material" associados a este curso
// e preeche o atributo $materiais com este array
function getMatriculados()
{
$this-&gt;retrieveAssociation('materiais');
}
}
?&gt;



O código a seguir mostra o mapeamento XML - Material.
/equipamento/classes/map/material.xml

&lt;?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?&gt;
&lt;map&gt;
&nbsp;&nbsp;&nbsp;&lt;moduleName&gt;equipamento&lt;/moduleName&gt;
&nbsp;&nbsp;&nbsp;&lt;className&gt;material&lt;/className&gt;
&nbsp;&nbsp;&nbsp;&lt;tableName&gt;material&lt;/tableName&gt;
&nbsp;&nbsp;&nbsp;&lt;databaseName&gt;equipamento&lt;/databaseName&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;idmaterial&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;idmaterial&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;key&gt;primary&lt;/key&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;idgenerator&gt;seq_material&lt;/idgenerator&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;nome&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;nome&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;marca&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;marca&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;preco&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;preco&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;idcategoria&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;columnName&gt;idcategoria&lt;/columnName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;
&nbsp;&nbsp;&nbsp;&lt;attribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;attributeName&gt;categoria&lt;/attributeName&gt;
&nbsp;&nbsp;&nbsp;&lt;/attribute&gt;

&nbsp;&nbsp;&nbsp;&lt;association&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toClassModule&gt;equipamento&lt;/toClassModule&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toClassName&gt;material&lt;/toClassName&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;cardinality&gt;oneToOne&lt;/cardinality&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;target&gt;material&lt;/target&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;retrieveAutomatic&gt;false&lt;/retrieveAutomatic&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;saveAutomatic&gt;false&lt;/saveAutomatic&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;entry&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fromAttribute&gt;idcategoria&lt;/fromAttribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;toAttribute&gt;idcategoria&lt;/toAttribute&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/entry&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/association&gt;
&lt;/map&gt;


O código a seguir mostra a definição da classe Material
/equipamento/classes/material.class

&lt;?php

class BusinessEquipamentoMaterial extends MBusiness
{
var $idMaterial;
var $nome;
var $marca;
var $preco;
var $idCategoria;
var $categoria;

function __construct($data=NULL)
{
parent::__construct('equipamento',$data);
$this-&gt;nome = '';
$this-&gt;marca = '';
$this-&gt;preco = '';
$this-&gt;idCategoria = ':null';
}

function getById($id)
{
$this-&gt;idMaterial = $id;
$this-&gt;retrieve();
return $this;
}

function setIdMaterial($idMaterial)
{
$this-&gt;idMaterial = $idMaterial;
}

function getIdMaterial()
{
return $this-&gt;idMaterial;
}

function setNome($nome)
{
$this-&gt;nome = $nome;
}

function getNome()
{
return $this-&gt;nome;
}

function setPreco($preco)
{
$this-&gt;preco = $preco;
}
function getPreco()
{
return $this-&gt;preco;
}

function setMarca($marca)
{
$this-&gt;marca = $marca;
}

function getMarca()
{
return $this-&gt;marca;
}

function setData($data)
{
$this-&gt;setIdMaterial($data-&gt;idMaterial);
$this-&gt;setNome($data-&gt;nome);
$this-&gt;setPreco($data-&gt;preco);
$this-&gt;setMarca($data-&gt;marca);
}

function listByNome($nome)
{
$criteria =  $this-&gt;getCriteria();
$criteria-&gt;addCriteria('nome','LIKE', "$nome");
$criteria-&gt;addOrderAttribute('nome');
return $criteria-&gt;retrieveAsQuery();
}

function cadastrar($categoria)
{
}
}

?&gt;