But it will give return same data from tab[1] only..
i want to get data from tab[1] and tab[0] distinctly
$db = ConnectionManager::getDataSource('default');
$tab = $db->listSources();
echo '<br>';
$this->Form->useTable=$tab[1];
print_r($this->Form->find(`all'));
echo '<br>';
$this->Form->use Table=$tab[0];
print_r($this->Form->find('all'));
Changing Model->useTable at runtime does not work properly because once a model has been initialised, CakePHP caches the schema of the database-table.
To switch to another table and clear the cached schema, use Model->setSource('tablename')
Documentation; http://api.cakephp.org/2.3/source-class-Model.html#1100-1125
Your example will then look like this;
echo '<br>';
$this->Form->setSource($tab[1]);
print_r($this->Form->find(`all'));
echo '<br>';
$this->Form->setSource($tab[0]);
print_r($this->Form->find('all'));
Also, please use debug() to output results for debugging in stead of print_r(). This will output the results properly formatted. (You'll need to set debug to 1 or higher inside your app/Config/core.php configuration for debug() to work)
however
Switching the sourcetable of a Model is generally bad practice and will only apply to very specific cases. I would strongly suggest to create a separate model for each database table.
Related
I saw this snippet of data in my database, and I need to manipulate it, however I don't know what kind of array is this. It's surely not JSON though.
a:3:{s:7:"address";s:37:"Budapest I. kerület Kék golyó utca";s:3:"lat";s:10:"47.4996733";s:3:"lng";s:17:"19.02209300000004";}
https://ru.functions-online.com/unserialize.html
Think that is PHP serialized output. Try to do something like this
//php
$string = "a:3:{s:7:"address";s:37:"Budapest I. kerület Kék golyó utc.....";
$var = unserialize($string);
echo "<pre>";
var_dump($var);
echo "</pre>";
I have a website with very simple news system (posting, editting, deleting etc). All my html pages are saved in UTF-8 formatting, everything displayes correctly.
I specify using UTF in every header:
For saving news to database, I use simple scripts like (all values come from a html form):
$newsTitel = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
$submitDate = $date = date('Y/m/d');
$content = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
include 'includes/dbconnect.php';
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$query = mysql_query("INSERT INTO news SET date='$submitDate',subject='$newsTitel',news='$content'");
The data get saved to database but in a weird format (coding). There are characters like à ¡ Ä etc which makes the content almost unreadable. Other problem is that when loading this content back to html forms (for editting news) it displays in this weird coding. When I looked into the specification of the database I use, it says that it saves data in UTF-8.
I use phpMyAdmin to access the MYSQL database.
So to sum it up:
Pages: saved in UTF8, all have correct header
Database: interaction with the server: utf8_czech_ci, tables in the same format
What I do not understand at all is this strange bevaior:
1) I save the data into the database using the script above
2) I take a look into phpMyAdmin and see broken encoding
3) I load the data back into my website and display them using this:
<?php
include 'includes/dbconnect.php';
$data = mysql_query("SELECT * FROM news ORDER BY id DESC limit 20") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo '<article><h3> '.$info['subject'].'</h3><div id="date">'.$info['date'].'</div>';
echo '<p>'.$info['news']. '</p></article>';
}
?>
The encoding is correct and no weird characters are displayed.
4) I load the exact same data into a html form (for edition purposes) and see the same broken encoding as in the database.
What happened? I really dont get it. I tried fixing this by re-saving everything in utf8, alterign tables and changing their encodings into different utf8 versions etc...
This is example of a data I pass to the database (it is in czech with html tags):
<p>Vařila myšička kašičku</p>
<img src="someImage.jpg">
<p>Další text</p>
Thanks for any help...
The commands for specifying the character set should be:
set names 'utf8';
If you check the result returned from your queries at the moment, what does it say? If I try it in the monitor I get the following:
mysql> set names 'UTF-8';
ERROR 1115 (42000): Unknown character set: 'UTF-8'
Have you tried using set names 'utf8' before connecting for the SELECT as well? The characters you're saying are output make me think you're getting back the correct bytes for UTF-8, but they're being interpreted as ISO-8859-1.
You are not escaping single quotes or some other html chars.
Use mysql_real_escape_string.
$newsTitel = isset($_POST['title']) ? mysql_real_escape_string($_POST['title']) : 'Untitled';
I'm using Symfony2, and are having trouble getting array values stored in a session, without putting them in a variable or object.
Possible something like:
echo $app['session']->get('shop')->get('name');
Currently I'm achieving it by doing this, but I would like to avoid it for the cause of simplicity:
$temp = $app['session']->get('shop');
echo $temp['name'];
Is it possible?
Thanks in advance
The session object is just a "parameter bag", an object that holds keys & values.
If you want to create another level of that mechanism you would have to instantiate your own bag.
$shop = new \Symfony\Component\HttpFoundation\ParameterBag;
$shop->set('name', 'Fantastic Warehouse');
$app['session']->set('shop', $shop);
// next request
echo $app['session']->get('shop')->get('name');
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.
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.