2
I’m working on a real estate project and in it I have the add, edit and delete, everything is normal. In real estate I have tables cidade and bairro, that when I register there are two <option> but when I go to edit the real estate, the city and the neighborhood, they don’t take the place of the option, that is, the customer has to search the city and the neighborhood again, wanted to set the city and the neighborhood in the edit.
These are the options:
echo $this->Form->input('cidade', array('label' => 'Cidade', '', 'empty' => 'Selecione', 'options' => $Cidades));
echo $this->Form->input('bairro', array('label' => 'Bairro', 'empty' => 'Selecione o Bairro', 'options' => array() ));
This is my Immovelscontroller, the edit function:
public function editar($id = null) {
if (!$id) return $this->redirect(array('action' => 'index'));
$imovel = $this->Imovel->read(null, $id);
if ($imovel['Imovel']['creator'] != $this->Session->read('Auth.User.Imobiliaria.id')) return $this->redirect(array('action' => 'index'));
if (empty($this->request->data)) {
$this->set('imovel', $imovel);
$id_cidade = $imovel['Imovel']['cidade'];
include 'banco.php';
$sql = mysql_query("SELECT * FROM cidades WHERE id = '$id_cidade' ");
while($result = mysql_fetch_array($sql)){
$id = $result['id'];
$nomeCidade = $result['nome'];
}
$this->loadModel('Bairro');
$bairrosRioClaro = $this->Bairro->find('list', array('fields' => array('id', 'bairro'), 'conditions' => array('cidade' => $nomeCidade)));
//print_r($bairrosRioClaro);
$this->set('bairrosRioClaro', $bairrosRioClaro);
$especificacoes = array();
$tipo = $imovel['Imovel']['tipo'];
if ($tipo == 'residencial') {
$especificacoes = array(
1 => 'Casa',
2 => 'Apartamento',
3 => 'Condomínio',
4 => 'Chácara',
5 => 'Kitnet',
6 => 'Temporada'
);
} elseif ($tipo == 'comercial') {
$especificacoes = array(
7 => 'Barracão',
8 => 'Salas comerciais',
9 => 'Ponto comercial',
);
} elseif ($tipo == 'empreendimento') {
$especificacoes = array(
10 => 'Comercial',
11 => 'Residencial',
);
} elseif ($tipo == 'terreno') {
$especificacoes = array(
12 => 'Comercal',
13 => 'Residencial',
14 => 'Comercial/Residencial',
);
} elseif ($tipo == 'rural') {
$especificacoes = array(
15 => 'Fazenda',
16 => 'Sítio',
17 => 'Chácara',
);
} else {
$especificacoes = null;
}
$this->set('especificacoes', $especificacoes);
$this->set('data', $imovel);
} else {
$data = $this->request->data;
if ($data['Imovel']['opcoes']) $data['Imovel']['opcoes'] = implode(';', $data['Imovel']['opcoes']);
$data['Imovel']['modifier'] = $this->Session->read('Auth.User.Imobiliaria.id');
$data['Imovel']['especificacao'] = $data['Imovel']['imovel'];
if ($data['Imovel']['bairro']) {
$bairro = $this->Cep->find('first', array('conditions' => array('id' => $data['Imovel']['bairro'])));
$data['Imovel']['bairroNome'] = $bairro['Cep']['bairro'];
}
if ($data['Imovel']['cidade']) {
$cidade = $this->Cidade->find('first', array('conditions' => array('id' => $data['Imovel']['cidade'])));
$data['Imovel']['cidadeNome'] = $cidade['Cidade']['nome'];
}
if ($this->Imovel->save($data)) return $this->redirect(array('action' => 'index'));
$this->set('data', $data);
}
}
He’s returning the real estate data:
array(
'Imovel' => array(
'id' => '150',
'codigo' => 'TESTEIMOVEL',
'placa' => 'TESTEIMO',
'tipo' => 'residencial',
'cidade' => '9566',
'cidadeNome' => 'Rio Claro',
'bairro' => '578008',
'bairroNome' => 'Jardim Donângela',
'intencao' => '1',
'especificacao' => '1',
'endereco' => 'TESTEIMOVEL',
'valor' => '1',
'condominio' => 'TESTEIMOVEL',
'salas' => '0',
'dormitorios' => '0',
'suites' => '0',
'banheiros' => '0',
'garagem' => '0',
'complemento' => 'TESTEIMOVEL',
'areaConstruida' => 'TESTEIMOVEL',
'areaTotal' => 'TESTEIMOVEL',
'descricao' => 'TESTEIMOVEL',
'geolocalizacao' => 'Sob Consulta',
'video' => null,
'foto1' => '',
'foto2' => '',
'foto3' => '',
'foto4' => '',
'foto5' => '',
'foto6' => '',
'legendaFoto1' => '',
'legendaFoto2' => '',
'legendaFoto3' => '',
'legendaFoto4' => '',
'legendaFoto5' => '',
'legendaFoto6' => '',
'pageview' => '0',
'opcoes' => '',
'destaque' => '0',
'ativo' => '1',
'ativoPeloAdm' => '1',
'created' => '2014-02-26 09:53:13',
'creator' => '110',
'modified' => '2014-02-26 09:53:13',
'modifier' => '110'
)
)
A question, in your view all cities and neighborhoods are shown, however, the city that had already been chosen is not marked, this is the problem?
– Erlon Charles
No, I wanted the city of real estate, I was already there in the countryside, without having the need to change the city and the neighborhood again, for example, I will change the real estate "a", but I will only change if she has garage, there I do not need to change the city and the neighborhood...got......
– Furlan
Now I got confused more, come on. You have a real estate agent who is already registered, when you will edit it the saved options are already selected?
– Erlon Charles
Bairrois a relation table? Where does that come from$Cidades?– Beterraba
include 'database.php'; $sql = mysql_query("SELECT * FROM cities WHERE id = '$id_city' "); while($result = mysql_fetch_array($sql)){ $id = $result['id']; $nameCity = $result['name']; } First of all, this snippet should not even be written there. Anyway, basically it doesn’t make sense to write this if you’re using Cakephp, it takes care of CRUD itself.
– Daniel Omine
At the end of the day I’m thinking there’s something wrong with Models
– Erlon Charles
@Danielomine yes, I know, but do not know cakephp, only pure php, the modifications I was adapting to php
– Furlan
@Erloncharles as so selected? when I go edit, the city and the neighborhood are not set, I have to search the city and the neighborhood again
– Furlan
@Furlan, read this chapter a little Retrieving Your Data
– Erlon Charles
@Furlan, do the following for me put the following code in your controller
debug($imovel);, insert this immediately after$imovel = $this->Imovel->read(null, $id);after that add feedback to the question– Erlon Charles
@Erloncharles ready, take a look at the question, is returning the real estate data..
– Furlan
Wonder now I can give my answer
– Erlon Charles