CakePHP saveMany doesnt save - cakephp-2.5

I have the following Form Code.
<?php echo $this
->Form
->create(
'PagePhoto',
array(
'type' => 'file',
'url' => array(
'controller' => 'page_photos',
'action' => 'add'
)
)
); ?>
<div class="modal-body has-padding">
<div class="form-group">
<?php echo $this->Form->label('PagePhoto.0.filename', 'Photos:'); ?>
<br/><br/>
<?php echo $this->Form->file('PagePhoto.0.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.0.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.1.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.1.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.2.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.2.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.3.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.3.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Upload Photos</button>
</div>
Which returns the following array to the add action
array(
'PagePhoto' => array(
(int) 0 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpoIrbZ6',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 1 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpAcbXbC',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 2 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phppCeN8G',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 3 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/php8Ib3bO',
'error' => (int) 0,
'size' => (int) 94131
)
)
)
)
Add Action
public function admin_add() {
if ($this->request->is('post')) {
debug($this->request->data);
$this->PagePhoto->create();
if ($this->PagePhoto->saveMany($this->request->data)) {
$this->Session->setFlash(__('The gallery has been saved.'), 'admin/flash_success');
// return $this->redirect(array('action' => 'view', $this->request->data['Gallery']['album_id']));
} else {
$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'admin/flash_error');
}
} else {
// $this->request->data['Gallery']['album_id'] = $album_id;
}
}
Error:
FAILURE: The gallery could not be saved. Please, try again.

$this->PagePhoto->saveMany($this->request->data['PagePhoto']);
Note: When saving multiple records of same model the records arrays should be just numerically indexed without the model key.

Related

In Cakephp 3.6 How do I get the size and type of an image to send it by form?

How do I get the size and type of an image to send it by form?
Well that, I want to save in the table of images the size and type of an image that is uploaded through a form. With Ajax, I can recover those data, but to pass them to PHP I can only do when sending the form, also directly with PHP:
if ($this->request->is('post')) {
$isData = $this->request->getdata();
$imagene->imagen = $isData['image_path'];
$imagene->tipo = $isData['type']
$imagene->tamano = $isData['size'];
...
But I want to do it before sending the form, which is when the insertion is done in the database.
Form:
<?= $this->Form->create($imagene, ['novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<?= $this->Form->hidden('tipo', ['value' => $tipo, 'class' => 'tipo-addimage']); ?>
<?= $this->Form->hidden('$tipo', ['value' => $size, 'class' => 'tamano-addimage']); ?>
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
<?= $this->Form->end(); ?>
Now I see that if I do a debug of $isData, the field: "imagen" does not appear:
'tabla' => 'users',
'id_tabla' => '22',
'tipo' => '',
'tamano' => ''
UPDATING
I've changed things in the form and the controller:
form:
<?= $this->Form->create($imagene, ['enctype' => 'multipart/form-data', 'novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<div class="centrar-submit">
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
</div>
<?= $this->Form->end(); ?>
Controller:
public function add($table, $idTable) {
$imagene = $this->Imagenes->newEntity();
if ($this->request->is('post')) {
$isData = $this->request->getdata();
debug($this->request->getData('imagen')); // <---- Is null
debug($isData); // <---- Is empty
...
Why? I don't know.
You should mentioned what type of form you used. That is file type you should use
echo $this->Form->create($article, ['type' => 'file']);
Hope this help you.

get date in a controller in cakephp

I have a date form with datapicker and I want to send these dates to a controller to place these dates in a query, my problem is that I do not know how to capture them to insert them in the query, in the view I have:
<?php $this->end(); ?>
<?=$this->Form->create(false, array('type' => 'get', 'controller' => 'informes', 'action' => 'crm')); ?>
<div class="row">
<div class="col-sm-2">Fecha del Informe</div>
<div class="col-sm-4">
<div class="input-daterange input-group" id="datepicker">
<span class="input-group-addon">Desde</span>
<input type="text" class="input-sm form-control" name="start" id="start"/>
<span class="input-group-addon">hasta</span>
<input type="text" class="input-sm form-control" name="end" id="end"/>
</div>
</div>
<div class="col-sm-2">
</div>
<div class="col-sm-2"><button class="btn btn-xs btn-primary" type="submit">Generar Informe</button></div>
</div>
<?=$this->Form->end(); ?>
and in the controller :
// Loading different models
$this->loadModel('Centro');
$this->loadModel('Items');
$this->loadModel('Sesion');
$this->loadModel('Zona');
$this->loadModel('TratamientosZona');
$this->loadModel('tratamiento');
$this->loadModel('Paciente');
// My query with CakePHP ORM
$sesion_pagada = $this->Centro->find('all', array(
array('fields' => 'Centro.nombre', SUM(IF('Paciente.sexo' = 'F', 'Zona.valor_mujer', 'Zona.valor_hombre')) AS suma
'joins' => array(
array('alias' => 'Sesion', 'table' => 'sesions', 'type' => 'INNER',
'conditions' => 'Centro.id = Sesion.centro_id'),
array('alias' => 'Items', 'table' => 'items', 'type' => 'LEFT',
'conditions' => 'Sesion.id = Item.sesion_id'),
array('alias'), => 'Zona', 'table' => 'zonas', 'type' => 'INNER',
'conditions' => 'Item.zona_id = Zona.id',
array('alias' => 'TratamientosZona', 'table' => 'tratamientos_zonas', 'type' => 'INNER',
'conditions' => 'Zona.id = TratamientosZona.zona_id'),
array('alias' => 'Tratamiento', 'table' => 'tratamientos', 'type' => 'INNER',
'conditions' => 'Tratamiento_zona.tratamiento_id = Tratamiento.id'),
array('alias' => 'Paciente', 'table' => 'pacientes', 'type' => 'INNER',
'conditions' => 'Tratamiento.paciente_id = Paciente.id')
),
'conditions' => array(
'Sesion.estado_id =' => 5,
'Sesion.fecha BETWEEN ? and ?'=> array( $query['start'], date('Y-m-d'))
)
)));
You can use this code to get data from GET request:
// For cakePHP 2.x
$start = $this->request->query['start'];
$end = $this->request->query['end'];
// For cakePHP 3.x >= 3.4.0
$start = $this->request->getQuery('start');
$end = $this->request->getQuery('end');
// cakePHP 3.x <= 3.4.0
$start = $this->request->query('start');
$end = $this->request->query('end');

put submit button in Tabs::widget in yii2

I have a Tabs::widget that all settings are located in different tabs in a ActiveForm and admin can set config in each tab and once submit.(multiple forms in one widget )
in setting view :
<?php $form = ActiveForm::begin(); ?>
<?php
echo \yii\jui\Tabs::widget([
'headerOptions' => ['class' => 'tabs'],
'itemOptions' => ['tag' => 'div'],
'items' => [
[
'label' => 'serverSetting',
'content' => $this->render('serverSetting', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'emailSetting',
'content' => $this->render('emailSetting', ['model' => $model, 'form' => $form]),
],
[
'label' => 'smsSetting',
'content' => $this->render('smsSetting', ['model' => $model, 'form' => $form]),
],
],
]);
?>
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary', 'name' => ''])) ?>
</div>
<?php ActiveForm::end(); ?>
in view of one of the tabs (smsServer view):
<?php
use yii\helpers\Html;
use app\components\ActiveForm;
?>
<div class="user-form">
<?= $form->field($model, 'login')->textInput(['placeholder' => 'host']) ?>
<?= $form->field($model, 'password1')->textInput(['placeholder' => 'username']) ?>
<?= $form->field($model, 'wsdl')->textInput(['placeholder' => 'password']) ?>
<?= $form->field($model, 'from1')->textInput(['placeholder' => 'port']) ?>
</div>
in controller :
public function actionSetting()
{
$model = new Setting();
$model->setAttributes(Yii::$app->params, false);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
file_put_contents(Yii::getAlias('#app/config') . DIRECTORY_SEPARATOR . 'extra_params.php', base64_encode(serialize($model->attributes)));
}
return $this->render('setting', [
'model' => $model,
]);
}
My form and submit button do not work. where is my problem?
Seems you don't have an ActiveForm::end()
.......
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary'])) ?>
</div>
<?php ActiveForm::end(); ?>
and for debugging don't suppress the name of button
So I found my answer. My validation was false.
in rule():
[['login', 'wsdl', 'password','from1'], 'required',],
But I post form without filled all fields.
I changed to
[['login',], 'required',],
[['login', 'wsdl', 'password',from1'], 'safe',],

How to create icon inside form postlink with cakephp and twitter bootstrap

This gives me what I want:
<?php echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-edit')) . " Edit",
array('action' => 'edit', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
But when I create a Form postLink I don't know how to get the remove icon in front of it..
<?php echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')) . " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']), null, __('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
It gives me <i class="glyphicon glyphicon-remove"></i> Delete
You forgot add option escape to false
echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')). " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']),
array('escape'=>false),
__('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini')
);
using a button
<?php echo $this->Form->postLink(
'<button class="btn btn-danger">
<i class="icon-trash icon-white"></i>
</button>',
array(
'action' => 'delete', $post['Post']['id']
),
array(
'class' => 'tip',
'escape' => false,
'confirm' => 'Are you sure ?'
));
?>
Try this:
<?php
echo $this->Form->postLink(
'Delete',
array('controller'=>'Comments',
'class'=>'glyphicon glyphicon-remove','action' => 'delete',$comment['id']),
array('confirm' => 'Are you sure?')
);
?>
The below code will create Link Button for deleting Items with a Confirmation box.
$this->Form->postLink( 'Delete Item',
['action' => 'delete', 'paramId' => $item->id ],
['confirm' => __('Are you sure you want to delete this Item?'), 'class'=> 'btn btn-outline-danger']
)

Zend Framework 2 formInput or formElement ID tag not rendering

In Zend framework 2, when I use the view's formRow method like so
$this->formRow($form->get('product_name'));
it will generate HTML like this
<label for="product_name">
<span>Name</span>
<input type="text" id="product_name" name="product_name">
</label>
but if I use formInput
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls">
<?php echo $this->formInput($form->get('product_name')); ?>
</div>
</div>
$this->formInput($form->get('product_name'));
i don't get the id tag
<input type="" name="product_name">
I've tried with formElement with same results.
How can I get it to render just the input with all attributes and values?
This is how my Zend Framework 2 View looks like (simplified)
<?php echo $this->form()->openTag($form); ?>
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
<div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>
<?php echo $this->form()->closeTag(); ?>
and the Zend Framework 2 Form
<?php
namespace Product\Form;
use Zend\Form\Form;
class ProductForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('product');
$this->setAttribute('method', 'post');
$this->setAttribute('class','form-horizontal');
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
'class'=>'btn btn-success btn-large'
),
));
}
}
?>
Change:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
to:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
'id' => 'product_name',
),
'options' => array(
'label' => 'Name',
),
));
Actually this code:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'product_name',
'attributes' => array(
'id' => 'product_name',
'class' => 'span3',
),
'options' => array(
'label' => 'Your label',
),
));
could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:
<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">
which is more readable than the original formRow output (without id neither class attributes)