public
Created

saveAll não funciona - hasMany

  • Download Gist
ImagemTipoImovel.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
<?php
App::uses('AppModel', 'Model');
/**
* ImagemTipoImovel Model
*
* @property TipoImovel $TipoImovel
*/
class ImagemTipoImovel extends AppModel {
 
public $displayField = 'imagem';
 
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'imagem_tipo_imoveis';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'tipo_imovel_id' => array(
// 'numeric' => array(
// 'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
// ),
),
'imagem' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
)
);
 
//The Associations below have been created with all possible keys, those that are not needed can be removed
 
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'TipoImovel' => array(
'className' => 'TipoImovel',
'foreignKey' => 'tipo_imovel_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
TipoImoveisController.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php
App::uses('AppController', 'Controller');
/**
* TipoImovels Controller
*
* @property TipoImovel $TipoImovel
*/
 
class TipoImoveisController extends AppController {
 
public $uses = array('TipoImovel');
 
...
 
 
/**
* admin_add method
*
* @return void
*/
public function admin_add() {
if ($this->request->is('post')) {
$this->TipoImovel->create();
if ($this->TipoImovel->saveAll($this->request->data)) {
$this->Session->setFlash(__('The tipo imovel has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The tipo imovel could not be saved. Please, try again.'));
}
}
 
$empreendimentos = $this->TipoImovel->Empreendimento->find('list');
$tipos = $this->TipoImovel->Tipo->find('list');
$imagens = $this->TipoImovel->ImagemTipoImovel->find('list');
$this->set(compact('empreendimentos', 'tipos', 'imagens'));
}
 
 
...
TipoImovel.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<?php
App::uses('AppModel', 'Model');
/**
* TipoImoveis Model
*
* @property Empreendimento $Empreendimento
* @property Tipo $Tipo
*/
class TipoImovel extends AppModel {
 
public $useTable = 'tipo_imoveis';
public $actsAs = array(
// 'Containable',
'Imagem' => array(
'ImagemTipoImovel' => array(
'model' => 'ImagemTipoImovel',
'field' => 'imagem',
'resize' => array(
'width' => 470,
'height' => 350,
'fit' => 'outside'
),
'crop' => array(
'width' => 224,
'height' => 160,
'fit' => 'outside'
),
'folder' => 'tipo_imoveis'
),
'imagem' => array(
'field' => 'imagem',
'quantity' => 3,
'resize' => array(
'width' => 470,
'height' => 350,
'fit' => 'outside'
),
'crop' => array(
'width' => 224,
'height' => 160,
'fit' => 'outside'
),
'folder' => 'tipo_imoveis'
),
'imagem_principal' => array(
'field' => 'imagem_principal',
'quantity' => 1,
'resize' => array(
'width' => 470,
'height' => 350,
'fit' => 'outside'
),
'crop' => array(
'width' => 224,
'height' => 160,
'fit' => 'outside'
),
'folder' => 'tipo_imoveis'
),
'banner' => array(
'field' => 'banner',
'quantity' => 1,
'crop' => array(
'width' => 224,
'height' => 160,
'fit' => 'outside'
),
'folder' => 'tipo_imoveis'
)
));
 
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
 
//The Associations below have been created with all possible keys, those that are not needed can be removed
 
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Empreendimento' => array(
'className' => 'Empreendimento',
'foreignKey' => 'empreendimento_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Tipo' => array(
'className' => 'Tipo',
'foreignKey' => 'tipo_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
 
 
public $hasMany = array(
'ImagemTipoImovel' => array(
'className' => 'ImagemTipoImovel',
'foreignKey' => 'tipo_imovel_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
admin_add.ctp
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<div class="tipoImoveis form">
<?php echo $this->Form->create('TipoImovel', array('type' => 'file'));?>
<fieldset>
<legend><?php echo __('Add Tipo Imovel'); ?></legend>
<?php
echo $this->Form->input('empreendimento_id');
echo $this->Form->input('tipo_id');
echo $this->Form->input('chamada1');
echo $this->Form->input('chamada2');
echo $this->Form->input('chamada3');
echo $this->Form->input('descricao');
echo $this->Form->input('youtube_link', array('class' => 'mceNoEditor'));
echo $this->Form->input('vimeo_embed', array('class' => 'mceNoEditor'));
 
echo $this->Html->link('Adicionar imagem', '#', array('title' => 'Adicionar Imagens', 'class' => 'adicionar-imagem'));
?>
 
<div class="imagens">
</div>
 
<?php
echo $this->Form->input('ativo', array('checked' => true));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
 
<li><?php echo $this->Html->link(__('List Tipo Imovels'), array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Empreendimentos'), array('controller' => 'empreendimentos', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Empreendimento'), array('controller' => 'empreendimentos', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Tipos'), array('controller' => 'tipos', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Tipo'), array('controller' => 'tipos', 'action' => 'add')); ?> </li>
</ul>
</div>
 
<script type="text/javascript">
var link = "<?php echo Router::url(array('controller' => 'imagem_tipo_imoveis', 'action' => 'nova_imagem', 'admin' => true)); ?>";
</script>
debug.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
// debug após sair do ImagemBehavior - responsável pelo upload
 
Array
(
[TipoImovel] => Array
(
[empreendimento_id] => 2
[tipo_id] => 1
[chamada1] => <pre><span class="na">saveAssociated</span></pre>
[chamada2] => <pre><span class="na">saveAssociated</span></pre>
[chamada3] => <pre><span class="na">saveAssociated</span></pre>
[descricao] => <pre><span class="na">saveAssociated</span></pre>
[youtube_link] => saveAssociated
[vimeo_embed] => saveAssociated
[ativo] => 1
)
 
[ImagemTipoImovel] => Array
(
[0] => Array
(
[tipo_imovel_id] =>
[imagem] => 65d7adc2c7ed519e489789fc3eb60e8a.jpg
)
 
[1] => Array
(
[tipo_imovel_id] =>
[imagem] => f95bcebaecad405b2f7a50a2535d4c4b.jpg
)
 
)
 
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.