save is not working in cakeph when all is okay - cakephp

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?

Related

Passing parameters into a model/table function

I am trying to pass a value called schoolYear which right now is 2019 lets say or whatever the user chooses really. Below is executed once the user selected submit and it executed a if post request then proceeds onto the edit controller via screen 2. on screen 2 I have a edit function with schoolYear being passed into it and a echo returning 2019 the correct value i choose. Great! Now i am struggling with passing that particular parameter into the table function so it can be used as shown. Can someone point me in the right direction, Thanks
Right now i get no error messages but if i take a look at the sql being passed, it is showing FiscalYear = 0 in the sql being executed which means $schoolyear is not being set from my controller hmmmmmm
return $this->redirect([
'controller' => 'MassUpdateCalender',
'action' => 'edit',
$schoolYear,
$allElementary,
]);
public function edit($schoolYear)
{
$InformationCalenderTable = $this->loadModel('MMSDvSchoolFromCalendar');
echo $schoolYear;
$elementarySchools = $InformationCalenderTable->getSchoolsByLevel('0%', $schoolYear);
public function getSchoolsByLevel($schoolYear, string $code = '0', string $isSummerSchool = '0')
{
$query = $this->find()
->where([
'FiscalYear' => $schoolYear,
'DistrictCode' => 'MA',
'summerSchool' => $isSummerSchool,
'SchoolCode like' => $code,
]);
return $query->toArray();
}
The outcome will return a list of all the schools with that where criteria.
From what I understand, you are trying to get schools based on what year they are in. For that, do this:
function edit($schoolYear) {
$this->loadModel('MMSDvSchoolFromCalendar');
$elementarySchools = $this->MMSDvSchoolFromCalendar->find('all', array('conditions' => array('MMSDvSchoolFromCalendar.FiscalYear' => $schoolYear)));
}
However, if you are trying to execute the query in your getSchoolsByLevel function, do this:
function edit($schoolYear) {
$this->loadModel('MMSDvSchoolFromCalendar');
$code = $isSummerSchool = 0;
$elementarySchools = $this->MMSDvSchoolFromCalendar->find('all', array('conditions' => array('FiscalYear' => $schoolYear, 'DistrictCode' => 'MA', 'summerSchool' => $isSummerSchool, 'SchoolCode LIKE' => $code)));
}
If you want to break out your function, like in your example, do this:
function edit($schoolYear) {
$this->loadModel('MMSDvSchoolFromCalendar');
$elementarySchools = $this->_getSchoolsByLevel($schoolYear);
}
function _getSchoolsByLevel($schoolYear, string $code = '0', string $isSummerSchool = '0') {
return $this->MMSDvSchoolFromCalendar->find('all', array('conditions' => array('FiscalYear' => $schoolYear, 'DistrictCode' => 'MA', 'summerSchool' => $isSummerSchool, 'SchoolCode LIKE' => $code)));
}
Notes:
Put var $uses = array('MMSDvSchoolFromCalendar'); at the top of your controller class, so you don't have to keep loading the model.
The underscore before the function denotes a private function. Use this so that users can't go to the MMSDvSchoolFromCalendar/getSchoolsByLevel page.

Is there an alternative to neighbors in Cakephp

I am coverting my app over to cakephp 3.0 and I am having trouble finding an alternative to using neighbors in the find method.
I need to find the next record in the associated table and neighbors was a great way to do it.
//Open courses
$options = [
'conditions' => ['Employees.user_id' => 1, 'CoursesEmployees.completed' => false],
'limit' => 3,
'contain' => 'Employees'
];
$recentOpen = $this->CoursesEmployees->find('all', $options)->toArray();
// get next module for each open course
foreach ($recentOpen as $key => &$value) {
$currentModule = $value['CourseModule']['id'];
$neighbors = $this->CoursesEmployees->CourseModules->find(
'neighbors',
['field' => 'id', 'value' => $currentModule]
);
$value['CourseModule']['next_module'] = $neighbors['next']['CourseModule']['name'];
};
Another issue with the code I discovered is that $this->CoursesEmployees->find('all', $options)->toArray(); seems to return a complex array with everything cakephp uses to query the table and not the actual results like I got with cakephp 2. I added the ->toArray() as recommended with 3.0
Because I loathe "Answers" that simply point to a URL where you may or may not be able to decipher a half answer today, but could be gone tomorrow, here is my replacement custom finder:
// In src/Models/Table/ExampleTable.php
/**
* Find neighbors method
*/
public function findNeighbors(Query $query, array $options) {
$id = $options['id'];
$previous = $this->find()
->select('id')
->order(['id' => 'DESC'])
->where(['id <' => $id])
->first();
$next = $this->find()
->select('id')
->order(['id' => 'ASC'])
->where(['id >' => $id])
->first();
return ['prev' => $previous['id'], 'next' => $next['id']];
}
Called simply in the Controller:
// In src/Controller/ExamplesController.php
public function view($id = null) {
...
$neighbors = $this->Examples->find('neighbors', ['id' => $id]);
....
}
As explained here
there is no neighbors find method in cakephp 3.
But if you follow the flow of the issue you will find a custom finder to accomplish it, maybe it will work for you.

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

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!!

Returning MySQL data to cakephp view

I am pretty new to cakephp and I am banging my head against the wall trying to write a pretty basic statement.
In my view file, I want to say if the is_open column in the events table is true, echo something. If it is not true, echo something else.
<?php
if ($response['data']['Event']['is_open'] == true) {
echo "Yes";
} else {
echo "No";
}
?>
I am having trouble working backwards within the controller to get the data in the first place.
In my controller I have something this:
public function some_function() {
$events = $this->Event->find('all');
}
In my view file, I get this error:
Notice (8): Undefined index: Event [View/Applications/agreement.ctp, line 21]
Can anyone point out what I am doing wrong?
I have been going through the blog tutorial and it's clear in some places to me and not clear in others. Where I am still having trouble is displaying anything in the view.
After further reading I want to do something like this in the controller:
$myVariable = $this->Event->find('first',
array( 'fields' => 'Event.is_open ',
'conditions' => array('Event.id =' => '400') ));
What I am hoping to say is grab the is_open value from the events table where the events.id = 400 (later on this value will be dynamic) Does this look even remotely correct? – mmalv just now edit
How did you come up with $response['data']?
In your EventsController you correctly set the return of $this->Event->find('all') to $events. In order for this variable to be available to your view you need to call Controller::set on it like this:
$this->set('events', $events);
Or even simpler, in one step, you can just do:
$this->set('events', $this->Event->find('all'));
Then in your Event view (perhaps someplace like View/Events/index.ctp) access the variable by the name $events. It should be in the format
Array(
[0] => Array(
'Event' => Array(
'id' => 1,
'name' => 'An open name!',
'is_open' => true
)
),
[1] => Array(
'Event' => Array(
'id' => 1,
'name' => 'Another event but closed',
'is_open' => false
)
)
)
So now all you need in your view is to run a loop like:
foreach ($events as $key => $value) {
if ($value['Event']['is_open']) {
echo "Yes";
} else {
echo "No";
}
}
All this is thoroughly explained in the cookbook, just go through the blog tutorial.

Drupal 7 Rules custom action assign return data to a replacement pattern

How can I create a custom Rule-Action which will successfully save a value as a replacement pattern for use in the other actions?
I got some very good help here on retrieving Product-Display information from a Product-Order.
As I said, the linked answer helped a great deal but the returned path data for the Product-Display comes back in the http://www.mysite/node/77 format. However, I really just need the numeric value only so I can load the node by performing a Fetch entity by id action supplying the numeric value and publishing the Product-Display node etc.
So, I implemented a custom action which will take the Product-Display URL(node/77) and return 77.
I copied the Fetch entity by id code and modified it so my returned numeric value can be saved and used in other Actions. The code is below:
function my_custom_action_info(){
$actions['publish_product_display_node'] = array(
'label' => t('Fetch product-display id'),
'parameter' => array(
'type' => array(
'type' => 'uri',
'label' => t('My Action'),
'options list' => 'rules_entity_action_type_options2',
'description' => t('Specifies the product-display url.'),
),
),
'provides' => array(
'entity_fetched' => array('type' => 'integer', 'label' => t('Fetched entity')),
),
'group' => t('Entities'),
'access callback' => 'rules_entity_action_access',
);
return $actions;
}
function publish_product_display_node($path = null){
$parts = explode('node/', $path);
return $parts[1];
}
function rules_entity_action_type_options2($element, $name = NULL) {
// We allow calling this function with just the element name too. That way
// we ease manual re-use.
$name = is_object($element) ? $element->getElementName() : $element;
return ($name == 'entity_create') ? rules_entity_type_options2('create') : rules_entity_type_options2();
}
function rules_entity_type_options2($key = NULL) {
$info = entity_get_info();
$types = array();
foreach ($info as $type => $entity_info) {
if (empty($entity_info['configuration']) && empty($entity_info['exportable'])) {
if (!isset($key) || entity_type_supports($type, $key)) {
$types[$type] = $entity_info['label'];
}
}
}
return $types;
}
function rules_action_entity_createfetch_access2(RulesAbstractPlugin $element) {
$op = $element->getElementName() == 'entity_create' ? 'create' : 'view';
return entity_access($op, $element->settings['type']);
}
As I said I copied the modified code so I don't claim to thoroughly understand all the functions aside from publish_product_display_node.
My code modifications work as far as setting the Product-Display URL token as the argument and also setting an entity variable label(Display NID) and value(display_nid).
The problem is when I check display_nid in newly created actions, the value is empty.
I need help figuring out the how to successfully save my entity value so I can use it in following Actions.
in the function publish_product_display_node, can you verify that you don't need to be returning $parts[0], instead of $[parts[1]?
It's just that Drupal paths are frequently in the form 'node/7' or 'taxonomy/term/6', and if you explode with 'node/' as the separator, you'd only have a single value which would start at index 0 for nodes...
So, just wondering if that would solve your issue...

Resources