CakePHP: save() fails but there are no validationErrors - cakephp

I am trying to save an item through my model, but saving fails.
When I output validationErrors - I get empty array, so no validation problems seem to be available. What could be failing my save()?
function resave($wid, $kTime){
$this->contain();
$word = $this->getById($wid);
// Successfully tretrieved here
$word['ModelName']['column'] = $kTime;
if($this->save($word)){
return 'success';
}else{
// this returns empty array
return $this->validationErrors;
}
}

To save yourself some time in the future, if a save() isn't working, the first place to look is in your SQL log and errors.
You should try installing Debug Kit Toolbar for CakePHP (https://github.com/cakephp/debug_kit). It makes it easy to view your SQL log, along with a bunch of other useful stuff. Or, alternatively, you can just put this in your layout file to view SQL history/errors:
<?php echo $this->element('sql_dump'); ?>

It was a problem with float and array type. I investigated it with gettype() and figured it out.

Related

sqlsrv_execute returning cursor I cannot return from my method

We recently upgraded our PHP version from 5 to 7 so I had to chenge the classes used to access our MS SQL database.
I started using sqlsrv and I have a method in a class to execute stored procedures which pretty much does this
<?php
$stmt = sqlsrv_prepare($db, $sql, $procedure_params);
if (!sqlsrv_execute($stmt)) {
if( ($errors = sqlsrv_errors() ) != null)
{
foreach( $errors as $error)
{
echo "SQLSTATE: ".$error[ 'SQLSTATE']."\n";
echo "code: ".$error[ 'code']."\n";
echo "message: ".$error[ 'message']."\n";
die();
}
}
}
// (1) This works, I get all the rows with data, etc...
while($row = sqlsrv_fetch_array($stmt)){
print_r($row);
}
// (2) Returning the cursos won't work, I cannot fetch it when I return it as a result
return $stmt;
?>
Once I execute the stored procedure, I get the result and I can fetch it with no problems (1). However, if I return the cursos as a result of my method / function, when I get the cursos as a result of calling the method, I cannot fetch it, it's like if it was empty. (2)
Any ideas? I'm sorry if it's a dumb question, but I have been googling for this issue and I cannot see any posts talking about a similar issue.
Thanks!
First off, why do you want to return the SQL resource? If you do that you will have a harder time closing the connection as you will need to do sqlsrv_free_stmt( $stmt); in the function you are passing back to.
Second, and I'm not a PHP expert, but I believe you are using the sqlsrv_execute which is mainly made for the one off executions with parameters such as inserts, updates and deletes. If you want to return a cursor with rows in it you will want to use sqlsrv_query.

Passing Array data using SESSION

I am trying to pass a Single/Multi Dimensional array to a second page using SESSION. I read and tried all questions and answers but can not get it done. Here is the 1st page:
<?php
// SendIt.php
SESSION_START();
$iA=array('A1','A2','B2','B1','B3','A3');
print_r($iA);
$_session['iA'] = $iA;
echo '<br>Click to send the array.';
?>
Here is the 2nd Page:
<?php
// GetIt.php
SESSION_START();
$iB = $_SESSION['iA'];
print_r($iB);
echo 'I am Here...';
?>
I do not get any of the iA array in the second page.
I must be missing something simple. Please check it out. Thx
I got help from a different PHP forum site and was advised that: Like any other PHP variable, _SESSION is case sensitive. So the problem is fixed by matching _SESSION variables and making them all upper case.

Sql in Drupal 7 Tpl file

Can any one suggest me how to write SQL query in .tpl file?
I tried to write db_query("select * from table_name "); But it is not working.
Please give me a solution to write sql query in tpl file
.tpl files are for the template layout and should not have much more logic than if a region has content. For anything requiring more logic I'd recommend either adding an applicable hook in your template.php or look into creating your own module.
What is the goal you are trying to achieve it may be solvable in another way instead of using a custom query. Likely you can create a view for your page?
Otherwise, try testing your query first through your database - if you have phpmyadmin or similar set up to see if it is actually returning a result.
And see https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7 for more information on how to use that particular function.
I have tried below code and getting the records
$result = db_query("SELECT * from node");
$records = $result->fetchAll();
print_r($records);
Can you check the tpl file is correct or not, OR can you share the code.
Better to write a function in your module and return the data from the query to the tpl file.
// function inside your module
function your_custom_function(){
$result = db_query("SELECT * from node");
$records = $result->fetchAll();
retun $records;
}
Then you can call this function from tpl file
// calling the function from tpl
$data = your_custom_function();
foreach($data as $value){
// do your template logic here
}

drupal 7 file field formatter on custom node for javascript (to set up jQuizMe)

I have set up a module with custom node type (I called jquizme, after the javascript jQuizMe that I really like using). I set up two fields for the javascript files I need to supply to make it work (after the general jQuizMe-2.2.js file you need to add another two javascript files - one for settings and one for the quiz content).
Drupal saves the files as myjavascriptfile.js.txt - I tested them and they still work to make the jQuizMe interface - ok. the problem is, I want to add these files on the node page... the files will be different for each node. how can I access the files for the drupal_add_js() function so they will load the files for the node in question?
I tried setting up custom field formatters, but I don't know how to access the uri for the files of a given node automatically to put in the drupal_add_js() function (I can add a static file and it loads fine ... I did this with hook_node_view ( jquizme_node_view ).
So I just need a way to access the info for the files... how are they linked to each node? I can't find the connection.
As you probably noticed, I am a module writing newbie, and I probably won't understand much related to object oriented programming sorry, haven;t progressed to that level yet), but I am open to any answer. I am sure I left out important info, but this it already getting too long.
I also set up a special page earlier on to just see if I could get jQuizMe to work in Drupal so that is still in the code.
I have tried many answers (last six hours or so... too much to say here), the latest of which is using tokens, but that is not working. Here is what I have so far:
function jquizme_node_view($node, $view_mode, $langcode) {
switch ($node->type) {
case 'jquizme':
$items = field_get_items('node', $node, 'field_myfield', $node->language);
drupal_add_css(drupal_get_path('module', 'jquizme') . '/jQuizMe.css', >array('scope' => 'header'));
drupal_add_js(drupal_get_path('module', 'jquizme') . '/alert.js');
drupal_add_js(drupal_get_path('module', 'jquizme') . '/jQuizMe-2.2.js', >array('scope' => 'header'));
//drupal_add_js($tokens['node']['jquizme_js1_field'], array('scope' => >'header'));
//drupal_add_js($tokens['node']['jquizme_js2'], array('scope' => 'header'));
break;
}
}
Thanks in advance!
Can you try this?
// Let me assume that the field names of your two file fields
// are jquizme_js1_field and jquizme_js2_field for convenience..
function jquizme_node_view($node, $view_mode, $language) {
$items = field_get_items('node', $node, 'jquizme_js1_field');
_jquizme_add_js_from_filefield($items);
$items = field_get_items('node', $node, 'jquizme_js2_field');
_jquizme_add_js_from_filefield($items);
}
// Given the values of a filefield attached to some entity,
// adds them as JS files to the page.
function _jquizme_add_js_from_filefield($items = array()) {
foreach ($items as $item) {
$fid = &$item['fid'];
$file = file_load($fid);
if (!$file) {
continue; // Maybe the file got deleted..
}
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$path = $wrapper->realpath();
// Ensure that the path exists and that it is a Javascript file..
if (file_exists($path) && preg_match('\.js$', $path)) {
drupal_add_js($path, array('type' => 'file'));
}
}
}

how to force drupal function to not use DB cache?

i have a module and i am using node_load(array('nid' => arg(1)));
now the problem is that this function keep getting its data for node_load from DB cache.
how can i force this function to not use DB cache?
Example
my link is http://mydomain.com/node/344983
now:
$node=node_load(array('nid'=>arg(1)),null,true);
echo $node->nid . " -- " arg(1);
output
435632 -- 435632
which is a randomly node id (available on the system)
and everytime i ctrl+F5 my browser i get new nid!!
Thanks for your help
Where are you calling this? For example, are you using it as part of your template.php file, as part of a page, or as an external module?
Unless you have this wrapped in a function with its own namespace, try naming the variable differently than $node -- for example, name it $my_node. Depending on the context, the 'node' name is very likely to be accessed and modified by Drupal core and other modules.
If this is happening inside of a function, try the following and let me know what the output is:
$test_node_1 = node_load(344983); // Any hard-coded $nid that actually exists
echo $test_node_1->nid;
$test_node_2 = node_load(arg(1)); // Consider using hook_menu loaders instead of arg() in the future, but that's another discussion
echo $test_node_2->nid;
$test_node_3 = menu_get_object(); // Another method that is better than arg()
echo $test_node_3->nid;
Edit:
Since you're using hook_block, I think I see your problem -- the block itself is being cached, not the node.
Try setting BLOCK_NO_CACHE or BLOCK_CACHE_PER_PAGE in hook_block, per the documentation at http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_block/6
You should also try to avoid arg() whenever possible -- it's a little bit of a security risk, and there are better ways to accomplish just about anything arg() would do in a module environment.
Edit:*
Some sample code that shows what I'm referring to:
function foo_block ($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => 'I am a block!',
'status' => 1,
'cache' => BLOCK_NO_CACHE // Add this line
);
return $block;
case 'view':
.....
}
}
node_load uses db_query, which uses mysql_query -- so there's no way to easily change the database's cache through that function.
But, node_load does use Drupal's static $nodes cache -- It's possible that this is your problem instead of the database's cache. You can have node_load clear that cache by calling node_load with $reset = TRUE (node_load($nid, NULL, TRUE).
Full documentation is on the node_load manual page at http://api.drupal.org/api/drupal/modules--node--node.module/function/node_load/6
I have had luck passing in the node id to node_load not in an array.
node_load(1);
According to Druapl's api this is acceptable and it looks like if you pass in an array as the first variable it's loaded as an array of conditions to match against in the database query.
The issue is not with arg(), your issue is that you have caching enabled for anonymous users.
You can switch off caching, or you can exclude your module's menu items from the cache with the cache exclude module.
edit: As you've now explained that this is a block, you can use BLOCK_NO_CACHE in hook_block to exclude your block from the block cache.

Resources