Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created March 11, 2012 17:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save patrickmaciel/2017219 to your computer and use it in GitHub Desktop.
saveAll não funciona - hasMany
<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>
<?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
)
)
)
<?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' => ''
)
);
}
<?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'));
}
...
<?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' => ''
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment