Laravel 4 - A four digit year could not be found Data missing - database

I'm trying to update some data and I'm having the next message:
A four digit year could not be found Data missing
In this form, I have some data that I want to update in my 'Actividad', so I have created an array with every field I need to update.
But, one of the form fields, I don't want to put it in the same array, because I need it to update the data in a pivot table ('actividad_material').
By the moment, I'm not able to update any of them... :(
Here is my code:
public function update($id)
{
$input = array(
'titulo' => Input::get('titulo'),
'actividad' => Input::get('actividad'),
'datos' => Input::get('datos'),
'solucion' => Input::get('solucion'),
'minutos' => Input::get('minutos'),
'tipo_id' => Input::get('tipo_id'),
'asignatura_id' => Input::get('asignatura_id'),
'bloque_id' => Input::get('bloque_id'),
'contenido_id' => Input::get('contenido_id'),
'objetivo_id' => Input::get('objetivo_id'),
'nivel_id' => Input::get('nivel_id'),
'etapa_id' => Input::get('etapa_id'),
'm_desc' => Input::get('m_desc'),
'slug' => Str::slug(Input::get('titulo')),
'user_id' => Sentry::getUser()->id,
'_method'
);
$v = Validator::make($input, Actividad::$rules);
if($v->passes())
{
// Trying to update my pivot table...
$actividad = Actividad::find($id);
$material_id = array(
'material_id' => Input::get('material_id'));
$actividad->materials->sync($material_id);
Actividad::find($id)->update($input);
return Redirect::route('actividads.index');
}
return Redirect::back()->withErrors($v);
}
Any idea why I'm getting this error? Maybe the timestamps?
What am I doing wrong when updating?
Thank you very much in advance!!

Related

in_array() expects parameter 2 to be array, null given in Laravel Controller

I have two request sample and incomplete sample, I get the complete sample by submitting the form without ticking any checkbox, then get incomplete samples by clicking the checkbox.
I can get incomplete samples by ticking the checkbox. I get the error while getting the complete samples.
if(!in_array($sample, $request->pending)){
$tests = Session('tests');
$createSample = Sample::create([
'test_user_id' => $request->test_user_id,
'received_by' => (integer)$request->received_by,
'received_at' => $now,
'received_name' => $request->received_name,
'biobank' => $request->biobank,
'order_id' => $request->order_id,
'order_type' => $request->order_type,
'name' => $request->name,
]);
Its because if you don't check the checkbox, the value won't submit and hence the $request->pending will be null instead of an array. You can try to check if its null and then you can do whatever you want to do with it.
if($request->pending){
if(!in_array($sample, $request->pending)){
$tests = Session('tests');
$createSample = Sample::create([
'test_user_id' => $request->test_user_id,
'received_by' => (integer)$request->received_by,
'received_at' => $now,
'received_name' => $request->received_name,
'biobank' => $request->biobank,
'order_id' => $request->order_id,
'order_type' => $request->order_type,
'name' => $request->name,
]);
}
}

How to store two related arrays into one table row in a database on laravel controller?

I passed my formData object via Ajax into laravel controller that have "pic[]" which contain an array of uploaded images, and "desc[]" which contain an array of descriptive text that related to the corresponding index for each image on the pic[] array.
Normally, when I want to insert it into the database, I would do this
if ($request->hasFile('pic')) {
foreach($request->pic as $p)
{
$myRow = tableName::create([
'picture' => $p
]);
};
};
But now that I want to insert the picture description into the same row that I just created, nested loop surely won't work, and I'm not sure how to do it with double loop for the desc array.
If you have desc[] like: [0 => 'First', 1 => 'Second', ...], you can access index of description if is equal to index of picture.
foreach($request->pic as $index => $p)
{
$myRow = tableName::create([
'picture' => $p,
'description' => $request->desc[$index] // also try $request->get('desc.' .$index)
]);
};
I hope it was helpful. Good luck.
Follow this code to insert an image in your database by using ajax
foreach($request->pic as $index => $p)
{
$myRow = tableName::create([
'picture' => $p,
'description' => $request->desc[$index] // also try $request->get('desc.' .$index)
]);
};
I hope it was helpful to you.

Cakephp 3 Append $this->request->data using array() - Indirect modification of overloaded property data has no effect

I have been trying and trying a lot but i just don't understand how to achieve this .. Here is what i want to achieve by the way..
I am using cakephp 3.0 and i am trying to append $this->request->data by trying below code in my controller...
$this->data['Leaveregisters'] = array(
'name'=>'another',
'surname'=>'whatever'
);
echo '<pre>';
print_r($this->request->data);
exit;
but i am keep getting below error
Notice (8): Indirect modification of overloaded property App\Controller\LeaveregistersController::$data has no effect
However, i am able to append data using below code
$this->request->data['name'] = 'another';
$this->request->data['surname'] = 'whatever';
But i am just wondering why i am not able to achieve the same thing using array() or what should i do to achieve this ?
Array should be appened in the same array as well if i do $this->request->data currently if i use below code
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever asdfa'
]);
echo '<pre>';
print_r($this->request->data);
exit;
Its getting appeneded but with new key like below which i do not want
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[Leaveregisters] => Array
(
[name] => another
[surname] => whatever asdfa
)
)
I want something like this instead...
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[name] => another
[surname] => whatever asdfa
)
So that i can just save my data using $this->request->data
Can someone guide me for the same ?
Thanks
There is no Controller::$data property anymore in CakePHP 3.
Cookbook > Appendices > 3.X Migration Guide > 3.0 Migration Guide > Controller
The only correct way is to access the request object, and of course you can set nested data if you want to the same way, ie:
$this->request->data['Leaveregisters'] = [
'name' => 'another',
'surname' => 'whatever'
];
Alternatively use the data() method:
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever'
]);
See also
Cookbook > Request & Response Objects > Request Body Data
API > \Cake\Network\Request::data()

save is not working in cakeph when all is okay

My code:
$this->PackageCustomer->id = $customer_id;
$data['PackageCustomer'] = array(
'shipment' => 2,
'comments' => $this->request->data['Ticket']['content'],
'shipment_equipment' => $this->request->data['Ticket']['shipment_equipment'],
'shipment_note' => $this->request->data['Ticket']['shipment_note'],
'issue_id' => $this->request->data['Ticket']['issue_id']
);
pr($data); exit;
$this->PackageCustomer->save($data['PackageCustomer']);
//var_dump($this->PackageCustomer->invalidFields());
// pr($this->PackageCustomer->error);
echo $this->PackageCustomer->getLastQuery(); exit;
I inspect array $data. Data is being revived properly. And getLastQuery function is:
function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
Which is defined in appModel. I am using cakephp 2.6.9. But last query is :COMMIT which does not make any sense. I check My model convention. It is okay. Now what is the problem in my code?
Try this::
$this->PackageCustomer->id = $customer_id;
$data['PackageCustomer'] = array(
'shipment' => 2,
'comments' => $this->request->data['Ticket']['content'],
'shipment_equipment' => $this->request->data['Ticket']['shipment_equipment'],
'shipment_note' => $this->request->data['Ticket']['shipment_note'],
'issue_id' => $this->request->data['Ticket']['issue_id']
);
pr($data); exit;
$this->loadModel('PackageCustomer');
$this->PackageCustomer->save($data['PackageCustomer']);
//var_dump($this->PackageCustomer->invalidFields());
// pr($this->PackageCustomer->error);
echo $this->PackageCustomer->getLastQuery(); exit;
If the above code doesn't work I need the following answered to help further...
I need bit more information can you confirm the following:
What is the name of the table you are trying to save to?
What is the name of the class relating the to the table you are trying to save to?
Are you trying to edit or create a new record in this table?

CakePHP - Paginating an Array

Cake handles pagination of a model with a simple $this->paginate(), but what should I use if I want to paginate a array of values?
The Scenario is like this:
$this->set('sitepages', $this->paginate());
This code in my index() returns an array like
Array
(
[0] => Array
(
[Sitepage] => Array
(
[id] => 13
[name] => Home
[urlslug] => home
[parent_id] => 1
[page_title] => Welcome to KIAMS, Pune
[order] => 1
)
)
[1] => Array
(
[Sitepage] => Array
(
[id] => 26
[name] => About Us
[urlslug] => aboutus
[parent_id] => 1
[page_title] =>
[order] => 2
)
)
[2] => Array
(
[Sitepage] => Array
(
[id] => 27
[name] => Overview of KIAMS
[urlslug] => aboutus/overview
[parent_id] => 26
[page_title] =>
[order] => 2
)
)
I retrieved the same data using $this->Sitepage->find('all') and then performed some manipulations as required and form a array which is very similar to the above one, but the ordering gets changed. I want to paginate this new array and pass it to the view. I tried
$this->set('sitepages',$this->paginate($newarray))
But the data is not getting paginated. Can some one please help with paginating the $newarray in CakePHP?
To paginate in CakePHP you need to pass select conditions to the paginate() call.
Other data manipulation should be done in afterFind(), in your model file.
If you don't need these changes to be done in every single retrieval, you might as well consider creating a new model file pointing to the very same table as the current one, and adding an afterFind() method to that new file.
I've just dealt with this same problem...
I found the only way is to use the paginate() function to handle all the logic, rather than passing it a custom array. However, this isn't as bad as it seems:
$this->paginate = array(
'limit' => 2,
'order' => array(
'Report.name' => 'asc'
),
'conditions' => array(
'Account.id' => $this->foo()
),
);
$reports = $this->paginate();
In this example, I'm paginating Reports - but some Reports will not be included, depending on which Account they belong to (Account has some relationship with Report, hasmany, etc.).
By writing $paginate inside your action, you can use a custom callback for the conditions array. So function foo() can be written in your controller (or shoved to model) and returns an array of valid Account IDs.
I found I could easily rewrite my custom logic in this function - keeping both me and the Cake paginator happy.
Hope that helps!
I'm using cakephp version 1.3 and it seems this one is working:
at controller:
$result = $this->Paginate('Event');
$results = $this->Task->humanizeEvent($result);
$this->set('events', $results);
and it seems to display as a normal paginated array, at the view (setup your pagination in view as normal).
The humanizeEvent function just edits a field on the results array, to make it a sentence based on other fields inside the array, and it seems to work properly.
$this->paginate($newarray) is the wrong way to paginate. The first parameter cannot be an array. It must be a model name. You may want to study pagination setup from the manual. This will order alphabetically:
var $paginate = array(
'limit' => 25,
'order' => array(
'Sitepage.name' => 'asc'
)
);
$this->set('sitepages', $this->paginate('Sitepage'));
I create a component checking the paginator code..
Is not the best thing, but It work for me.....
Controller
$slicedArray = array_slice($fullArray,($page - 1) * $this->PaginatorArray->limit ,$this->PaginatorArray->limit)
$this->params['paging'] = $this->PaginatorArray->getParamsPaging('MyModel', $page, $total,count($slicedArray));
$this->helpers[] = 'Paginator';
Component
<?php
/* SVN FILE: $Id$ */
/**
* Pagination Array Component class file.
* #subpackage cake.cake.libs.view.helpers
*/
class PaginatorArrayComponent {
var $limit = 40;
var $step = 1;
function startup( & $controller){
$this->controller = & $controller;
}
function getParamsPaging($model, $page, $total, $current){
$pageCount = ceil($total / $this->limit);
$prevPage = '';
$nextPage = '';
if($page > 1)
$prevPage = $page - 1;
if($page + 1 <= $pageCount)
$nextPage = $page + 1;
return array(
$model => array(
'page' => $page,
'current' => $current,
'count' => $total,
'prevPage' => $prevPage,
'nextPage' => $nextPage,
'pageCount' => $pageCount,
'defaults' => array(
'limit' => $this->limit,
'step' => $this->step,
'order' => array(),
'conditions' => array(),
),
'options' => array(
'page' => $page,
'limit' => $this->limit,
'order' => array(),
'conditions' => array(),
)
)
);
}
}
?>
There was a bug in find("count") and it returned incorrect count if the query resulted in records for only in group. This has been fixed click here

Resources